def make_move(self, time_limit, players_score):
        """Make move with this Player.
        input:
            - time_limit: float, time limit for a single turn.
        output:
            - direction: tuple, specifing the Player's movement, chosen from self.directions
        """
        def find_player_positions(player_index):
            if player_index == 1:
                return self.pos
            pos = np.where(self.board == player_index)
            # convert pos to tuple of ints
            return tuple(ax[0] for ax in pos)

        start_time = time()
        player_positions = (find_player_positions(1), find_player_positions(2))
        self.board[self.pos] = -1

        curr_state = SearchAlgos.State(self.board.copy(), tuple(players_score),
                                       player_positions, 0, self.penalty_score,
                                       (0, 0), self.lifetime, self.initial_pos)

        heavy_player = SearchAlgos.AlphaBeta(minimax_utility, minimax_succ,
                                             None, start_time, time_limit,
                                             minimax_heuristic)
        depth = 4
        value, direction = heavy_player.search(curr_state, depth, 1)

        i = self.pos[0] + direction[0]
        j = self.pos[1] + direction[1]
        self.pos = (i, j)
        self.board[self.pos] = 1

        self.lifetime += 1
        return direction
Beispiel #2
0
def minimax_succ(state):
    pos = state.player_positions[state.curr_player]
    succ_states = []
    board = state.board.copy()
    board[pos] = -1
    min_fruit_time = min(len(board[0]), len(board))
    if state.lifetime >= 2 * min_fruit_time:
        board = np.where(board >= 3, 0, board)
    for d in utils.get_directions():
        i = pos[0] + d[0]
        j = pos[1] + d[1]

        if 0 <= i < len(board) and 0 <= j < len(board[0]) and (
                board[i][j] not in [-1, 1, 2]):  # then move is legal

            new_pos = (i, j)
            fruit_score = board[new_pos]
            players_score = list(state.players_score)
            players_score[state.curr_player] += fruit_score

            player_positions = list(state.player_positions)
            player_positions[state.curr_player] = new_pos

            old_board_value = board[new_pos]
            board[new_pos] = (state.curr_player + 1)
            yield SearchAlgos.State(board.copy(), tuple(players_score),
                                    tuple(player_positions),
                                    1 - state.curr_player, state.penalty, d,
                                    state.lifetime + 1, state.initial_pos)

            # reset the board: positions + scores
            board[new_pos] = old_board_value
            players_score[state.curr_player] -= fruit_score
    def make_move(self, time_limit, players_score):
        """Make move with this Player.
        input:
            - time_limit: float, time limit for a single turn.
        output:
            - direction: tuple, specifing the Player's movement, chosen from self.directions
        """
        global play_simple_player

        def find_player_positions(player_index):
            if player_index == 1:
                return self.pos
            pos = np.where(self.board == player_index)
            return tuple(ax[0] for ax in pos)

        start_time = time()
        player_positions = (find_player_positions(1), find_player_positions(2))
        self.board[self.pos] = -1

        if not play_simple_player:
            play_simple_player = not is_enemy_reachable(
                self.board, self.pos) and self.lifetime >= 2 * min(
                    len(self.board[0]), len(self.board))

        curr_state = SearchAlgos.State(self.board.copy(), tuple(players_score),
                                       player_positions, 0, self.penalty_score,
                                       (0, 0), self.lifetime)

        time_limit = self.calculate_turn_time_limit(
            int(self.lifetime / 2) - 1 + self.lifetime % 2)
        global_alphabeta = SearchAlgos.AlphaBeta(minimax_utility, minimax_succ,
                                                 None, start_time, time_limit,
                                                 minimax_heuristic)

        depth = 1
        value, direction = global_alphabeta.search(curr_state, depth, 1)

        while not global_alphabeta.developed_whole_tree:
            try:
                global_alphabeta.developed_whole_tree = True
                value, direction = global_alphabeta.search(
                    curr_state, depth, 1)
                depth += 1
            except SearchAlgos.Interrupted:
                break
        i = self.pos[0] + direction[0]
        j = self.pos[1] + direction[1]
        self.pos = (i, j)
        self.board[self.pos] = 1

        self.lifetime += 1
        return direction
Beispiel #4
0
    def make_move(self, time_limit, players_score):
        """Make move with this Player.
        input:
            - time_limit: float, time limit for a single turn.
        output:
            - direction: tuple, specifing the Player's movement, chosen from self.directions
        """
        def find_player_positions(player_index):
            if player_index == 1:
                return self.pos
            pos = np.where(self.board == player_index)
            # convert pos to tuple of ints
            return tuple(ax[0] for ax in pos)

        start_time = time()
        player_positions = (find_player_positions(1), find_player_positions(2))
        self.board[self.pos] = -1

        curr_state = SearchAlgos.State(self.board.copy(), tuple(players_score),
                                       player_positions, 0, self.penalty_score,
                                       (0, 0), self.lifetime, self.initial_pos)

        minimax = SearchAlgos.MiniMax(minimax_utility, minimax_succ, None,
                                      start_time, time_limit,
                                      minimax_heuristic)
        depth = 1
        value, direction = minimax.search(curr_state, depth, 1)

        while not minimax.developed_whole_tree:
            try:
                minimax.developed_whole_tree = True
                value, direction = minimax.search(curr_state, depth, 1)
                depth += 1
            except SearchAlgos.Interrupted:
                break

        i = self.pos[0] + direction[0]
        j = self.pos[1] + direction[1]
        self.pos = (i, j)
        self.board[self.pos] = 1

        self.lifetime += 1
        return direction