Example #1
0
    def __init__(self, board, move, token, is_max=True):
        """

        :param board: a Board object
        :param move: a Move to be made by the opponent first to get to this node
        :param token: the Player token that is currently moving
        :param is_max: True if this row is a Max row
        """
        self._board = copy.deepcopy(board)
        self._token = token
        self._move = move
        self._is_max = is_max
        if token is isolation.Board.RED_TOKEN:
            self._opponent = isolation.Board.BLUE_TOKEN
        else:
            self._opponent = isolation.Board.RED_TOKEN

        if move:
            self._board.make_move(self._opponent, move)

        token = self._token  # if is_max else self._opponent
        neighbors = self._board.neighbor_tiles(
            self._board.token_location(token))
        current_location = self._board.token_location(token)
        push_out_squares = self._board.push_outable_square_ids()
        push_out_squares.add(current_location)
        self._available = [
            isolation.Move(idm, idt)
            for idm, idt in itertools.product(neighbors, push_out_squares)
            if idm != idt
        ]

        # self._last_move = last_move
        self.allchildren = None
    def take_turn(self, board):
        """
        Make a move on the isolation board
        :param board: an Board object
        :return: Return a Move object
        """

        if self._script_index < len(self._script):
            # Use the next move in the script
            move = self._script[self._script_index]
            print(self._token, move)
            self._script_index += 1
        else:
            # Prompt for a move. No validation is done.
            print('Your turn, {}:'.format(self._name))
            print(board)
            space_id = board.token_location(self.token())
            print('Current location:', space_id)
            move_to_spaces = board.neighbor_tiles(space_id)
            to_space_id = int(input('Moves {}: '.format(move_to_spaces)))
            push_out_space_ids = board.tile_square_ids()
            push_out_space_ids.add(space_id)
            push_out_space_ids.discard(to_space_id)
            push_out_space_id = int(input('Tiles {}: '.format(push_out_space_ids)))
            move = isolation.Move(to_space_id, push_out_space_id)

        return move
Example #3
0
    def early_game_strategy(self, board):
        # move towards the middle of the board
        # pop the tile 2 away from

        move_to_make = self.move_towards_middle_early_strat(board)
        punch_out_tile = self.punch_out_early_strat(board, move_to_make)
        return isolation.Move(move_to_make, punch_out_tile)
    def take_turn(self, board):
        """
        Make a move on the isolation board
        :param board: an Board object
        :return: Return a Move object
        """

        print("\n{} taking turn: ".format(self._name), end='')

        # Collect board state info to generate a move from
        space_id = board.token_location(self._token)
        neighbors = board.neighbor_tiles(space_id)
        print('possible moves:', neighbors)
        tiled_spaces = board.push_outable_square_ids()

        # Select a square to move to and a tile to push out.
        # Once a neighbor square is chosen to move to,
        # that square can no longer be pushed out, but
        # the square vacated might be able to be pushed out
        to_space_id = random.choice(list(neighbors))
        tiled_spaces.discard(to_space_id)
        # if space_id not in board.start_squares():
        #     tiled_spaces.add(space_id)
        tiled_spaces.add(space_id)
        print('possible push outs:', tiled_spaces)
        push_out_space_id = random.choice(list(tiled_spaces))

        # print('    Moving to', to_space_id, 'and pushing out', push_out_space_id)

        move = isolation.Move(to_space_id, push_out_space_id)
        print('   ', move)
        return move
Example #5
0
    def take_turn(self, board):
        """
        Make a move on the isolation board
        :param board: an Board object
        :return: Return a Move object
        """

        print("\n{} taking turn: ".format(self._name), end='')

        # Collect board state info to generate a move from
        space_id = board.token_location(self._token)
        neighbors = board.neighbor_tiles(space_id)
        print('possible moves:', neighbors)

        # Select a square to move to and a tile to push out.
        # Once a neighbor square is chosen to move to,
        # that square can no longer be pushed out, but
        # the square vacated might be able to be pushed out

        to_space_id, push_out_space_id = PlayerAgent.move(self, board)

        move = isolation.Move(to_space_id, push_out_space_id)
        print('   ', move)

        return move
Example #6
0
    def take_turn(self, board):

        if not self.OTHER_PLAYER_TOKEN:
            self.OTHER_PLAYER_TOKEN = board.RED_TOKEN if self.token(
            ) == board.BLUE_TOKEN else board.BLUE_TOKEN

        move_to = self.getNextLocation(board)
        knockout_id = self.find_knockout_square(board, move_to)
        move = isolation.Move(to_square_id=move_to,
                              pushout_square_id=knockout_id)
        return move
Example #7
0
 def early_move_poke_out(self, board):
     space_id = board.token_location(self._token)
     poke_out = [42, 2, 34, 26, 18, 10]
     pushed_out = list(board.pushed_out_square_ids())
     pushed_out.append(space_id)
     opponent = board.token_location(self._opponent)
     while poke_out != []:
         move_dest = self.h(board, space_id)
         for i in range(len(poke_out)):  # - 1
             if poke_out[len(poke_out) - 1] in pushed_out or poke_out[len(poke_out) - 1] == opponent or poke_out[len(poke_out) - 1] == space_id:
                 poke_out.pop()
             else:
                 choice = poke_out[len(poke_out) - 1]
                 poke_out.pop()
                 if move_dest[0] == choice:
                     move = isolation.Move(move_dest[1], choice)
                     print(move_dest, choice)
                     return move
                 else:
                     move = isolation.Move(move_dest[0], choice)
                     print(move_dest, choice)
                     return move
     poke_out_late = self.push_h2o(board, opponent)
     move_dest_late = self.push_h2o(board, space_id)
     if poke_out_late == 0:
         opponents_neighbors = list(board.neighbor_tiles(opponent))
         poke_out_late = opponents_neighbors[0]
     if move_dest_late == 0:
         blah = list(board.neighbor_tiles(space_id))
         move_dest_late = blah[0]
     if poke_out_late == move_dest_late or move_dest_late == poke_out_late:
         push_outable = list(board.push_outable_square_ids())
         push_outable.remove(move_dest_late)
         push_outable. append(space_id)
         poke_out_late = push_outable[0]
         move = isolation.Move(move_dest_late, poke_out_late)
         return move
     move = isolation.Move(move_dest_late, poke_out_late)
     print(move)
     return move
    def __init__(self, name, token, script=[]):
        """
        Initialize a new instance
        :param name: this player's name
        :param token: either RED_TOKEN or BLUE_TOKEN
        :param script: a list of Move objects or a string
                       that is a CSV file name
        """
        super().__init__(name, token)

        if type(script) is list:
            self._script = script
        else:
            with open(script) as script_file:
                reader = csv.DictReader(script_file)
                moves = [row for row in reader]
            if token is isolation.Board.BLUE_TOKEN:
                # Blue moves first
                self._script = [isolation.Move(int(row['move']), int(row['push'])) for row in moves[0::2]]
            else:
                self._script = [isolation.Move(int(row['move']), int(row['push'])) for row in moves[1::2]]

        self._script_index = 0
Example #9
0
    def take_turn(self, board):
        """
        Fetch a move from the server
        :param board:
        :return:
        """

        print(f'{self._color} [remote] taking turn')

        # Fetch the move to the server
        move_number = len(board.moves()) + 1
        response = _server_request('fetchmove.py',
                                   matchid=self._match_id,
                                   movenumber=move_number,
                                   token=self._color)
        nbr_tries = 1
        while nbr_tries < 6 and response['status'] == 'NO MOVE':
            # Problem so wait a bit and try up to three more times
            print(
                f'*** PROBLEM FETCHING MOVE {move_number} FROM SERVER ({nbr_tries})\n    {response["error"]}'
            )
            nbr_tries += 1
            time.sleep(2.0)
            response = _server_request('fetchmove.py',
                                       matchid=self._match_id,
                                       movenumber=len(board.moves()),
                                       token=self._color)
        if nbr_tries > 5:
            reply = input('No move on server. Try again? [Y/n]').strip() or 'Y'
            while reply[0].upper() == 'Y':
                response = _server_request('fetchmove.py',
                                           matchid=self._match_id,
                                           movenumber=len(board.moves()),
                                           token=self._color)
                if response['status'] == 'OK':
                    reply = 'N'  # nasty code to leave this loop!
                else:
                    reply = input('No move on server. Try again? [Y/n]').strip(
                    ).upper() or 'Y'

        if response['status'] == 'OK':
            move = isolation.Move(response['to'], response['pushout'])
        else:
            move = None

        print(f'    {move}')

        return move
Example #10
0
    def take_turn(self, board):
        """
        Make a move on the isolation board
        :param board: an Board object
        :return: Return a Move object
        """
        print("\n{} taking turn: ".format(self._name), end='')
        tiled_spaces = board.push_outable_square_ids()
        my_space_id = board.token_location(self._token)
        to_space_id = PlayerAgent.self_heuristic(self, board)
        tiled_spaces.discard(to_space_id)
        tiled_spaces.add(my_space_id)

        neighbors = board.neighbor_tiles(my_space_id)

        print('possible moves:', neighbors)

        push_out_space_id = PlayerAgent.push_out_heuristic(self, board, to_space_id)


        move = isolation.Move(to_space_id, push_out_space_id)
        print('   ', move)
        return move
Example #11
0
    def take_turn(self, board):

        if len(board.pushed_out_square_ids()) <= 9:
            print("EARLY GAME")

            # give locations for start and goal tiles
            start = board.token_location(self._token)
            goal = board.token_location(self._opponent)

            new_path = bfs_shortest_path(board, start, goal)
            if len(new_path) > 1:
                if new_path[1] == goal:
                    space_id = board.token_location(self._token)
                    neighbors = board.neighbor_tiles(space_id)
                    to_space_id = random.choice(list(neighbors))
                    for x in neighbors:
                        if len(board.neighbor_tiles(x)) > len(board.neighbor_tiles(to_space_id)):
                            to_space_id = x
                else:
                    to_space_id = new_path[1]
                tiled_spaces = board.push_outable_square_ids()
                tiled_spaces.discard(to_space_id)
                tiled_spaces.add(start)
                opponent_edge_pieces = list(set(tiled_spaces).intersection(board.neighbors(board.token_location(self._opponent))))
                if len(opponent_edge_pieces) > 0:
                    pop_space_id = random.choice(opponent_edge_pieces)
                else:
                    pop_space_id = random.choice(list(tiled_spaces))
                move = isolation.Move(to_space_id, pop_space_id)
                print("Move to ", to_space_id, "Push out ", pop_space_id)

            else:
                print("NO PATH FOUND")
                # since no path is found, it switches to the late game strategy
                # Move to neighbor tile with the most neighbors
                space_id = board.token_location(self._token)
                possible_moves = board.neighbor_tiles(space_id)
                to_space_id = random.choice(list(possible_moves))
                for move in possible_moves:
                    if len(board.neighbor_tiles(move)) > len(board.neighbor_tiles(to_space_id)):
                        to_space_id = move

                # Remove best_move from tiles you can push out
                # Add space_id to tiles you can push out
                tiled_spaces = board.push_outable_square_ids()
                tiled_spaces.discard(to_space_id)
                tiled_spaces.add(space_id)

                # Find token to pop
                other_token = board.token_location(self._opponent)
                possible_pops = board.neighbor_tiles(other_token)

                if to_space_id in possible_pops:
                    possible_pops.discard(to_space_id)
                if len(possible_pops) > 0:
                    best_pop = random.choice(list(possible_pops))
                    for pop in possible_pops:
                        if len(board.neighbor_tiles(pop)) > len(board.neighbor_tiles(best_pop)):
                            best_pop = pop
                else:
                    best_pop = random.choice(list(tiled_spaces))

                push_out_space_id = best_pop

                print('    Moving to', to_space_id, 'and pushing out', push_out_space_id)
                if to_space_id == push_out_space_id:
                    print('same tile')
                move = isolation.Move(to_space_id, push_out_space_id)
                print("Move to ", to_space_id, "Push out ", push_out_space_id)
            return move

        if len(board.pushed_out_square_ids()) > 9:

            print("LATE GAME")
            # Move to neighbor tile with the most neighbors
            space_id = board.token_location(self._token)
            possible_moves = board.neighbor_tiles(space_id)
            to_space_id = random.choice(list(possible_moves))
            for move in possible_moves:
                if len(board.neighbor_tiles(move)) > len(board.neighbor_tiles(to_space_id)):
                    to_space_id = move

            # Move to the neighbor tile with the neighbor that has the most neighbors
            # space_id = board.token_location(self._token)
            # tiles_two_away = set(board.squares_at_radius(space_id, 2))
            # pushed_out = set(board.pushed_out_square_ids())
            # moveable_tiles_two = set()
            # for tile in tiles_two_away:
            #     if tile not in pushed_out:
            #         moveable_tiles_two.add(tile)
            # if len(moveable_tiles_two) == 0:
            #     moveable_tiles_two = set(board.neighbor_tiles(space_id))
            # best_tile = random.choice(list(moveable_tiles_two))
            # for tile in moveable_tiles_two:
            #     if len(board.neighbor_tiles(tile)) > len(board.neighbor_tiles(best_tile)):
            #         best_tile = tile
            #
            # path = bfs_shortest_path(board, space_id, best_tile)
            # to_space_id = path[1]

            # Remove best_move from tiles you can push out
            # Add space_id to tiles you can push out
            tiled_spaces = board.push_outable_square_ids()
            tiled_spaces.discard(to_space_id)
            tiled_spaces.add(space_id)

            # Find token to pop
            other_token = board.token_location(self._opponent)
            possible_pops = board.neighbor_tiles(other_token)

            if to_space_id in possible_pops:
                possible_pops.discard(to_space_id)
            if len(possible_pops) > 0:
                best_pop = random.choice(list(possible_pops))
                for pop in possible_pops:
                    if len(board.neighbor_tiles(pop)) > len(board.neighbor_tiles(best_pop)):
                        best_pop = pop
            else:
                best_pop = random.choice(list(tiled_spaces))

            push_out_space_id = best_pop

            print('    Moving to', to_space_id, 'and pushing out', push_out_space_id)
            if to_space_id == push_out_space_id:
                print('same tile')
            move = isolation.Move(to_space_id, push_out_space_id)
            return move