Esempio n. 1
0
 def __init__(self, game_time, penalty_score):
     AbstractPlayer.__init__(
         self, game_time, penalty_score
     )  # keep the inheritance of the parent's (AbstractPlayer) __init__()
     self.board = None
     self.pos = None
     self.minimax = SearchAlgos.MiniMax(utility=self.utility,
                                        succ=self.succ,
                                        perform_move=None,
                                        goal=self.goal,
                                        heuristic=self.heuristic)
     self.players_score = None
Esempio n. 2
0
 def __init__(self, game_time, penalty_score):
     AbstractPlayer.__init__(
         self, game_time, penalty_score
     )  # keep the inheritance of the parent's (AbstractPlayer) __init__()
     # TODO: initialize more fields, if needed, and the Minimax algorithm from SearchAlgos.py
     self.board = None  # player1 is my player
     self.pos_players = None  # tuple [0]-player1 pos, [1] -player2 pos
     self.strategy = sa.MiniMax(utility=self.utility,
                                succ=utils.State.succ,
                                retriveLast=utils.State.retriveLast,
                                goal=utils.State.goal)
     self.num_of_turns = (0, 0
                          )  # tuple [0]-player1 turns, [1] -player2 turns
     self.fruits_on_board = None
     self.little_edge = 0
Esempio n. 3
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
Esempio n. 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
        """
        # print("start computing minimax move")  # printing for self test
        if self.first_player == -1:
            self.first_player = True

        state = utils.State(copy.deepcopy(self.board), self.pos,
                            self.rival_pos, players_score, self.penalty_score,
                            self.turns_till_fruit_gone,
                            self.fruits_on_board_dict, self.first_player,
                            time.time() + time_limit - .015)
        search_algo = SearchAlgos.MiniMax(utils.utility, utils.succ,
                                          utils.perform_move, utils.goal)
        depth = 1
        best_move = None, None

        while depth <= self.max_turns:
            try:
                best_move = search_algo.search(state, depth, True)
            except TimeoutError:
                break
            depth += 1

        if best_move[1] is None:
            # print("something went wrong,.. im out")  # printing for self test
            exit(0)
        # print("depth is : ", depth-1)  # printing for self test
        # print("minmax choose the move: ", best_move)  # printing for self test
        self.board[self.pos] = -1
        tmp1 = best_move[1]
        self.pos = (self.pos[0] + tmp1[0], self.pos[1] + tmp1[1])
        self.board[self.pos] = 1
        self.turns_till_fruit_gone -= 1
        return best_move[1]
Esempio n. 5
0
 def __init__(self, game_time, penalty_score):
     AbstractPlayer.__init__(self, game_time, penalty_score)  # keep the inheritance of the parent's (AbstractPlayer) __init__()
     self.minimax = SearchAlgos.MiniMax(self.utility, self.succ, self.perform_move, self.goal, self.heuristic_function, self.revert_move)