예제 #1
0
    def is_end(self):
        bd_flatten = [x for x in set().union(*self.board)]
        if len([x for x in bd_flatten if x == butils.PLAYER_1]) == 0:
            return True
        if len([x for x in bd_flatten if x == butils.PLAYER_2]) == 0:
            return True

        #test for player's options
        bs = butils.Board(iterable=self.board)
        potentials = butils.get_potentials(bs, butils.PLAYER_1)
        potentials.extend(butils.get_potentials(bs, butils.PLAYER_2))

        if len(potentials) == 0:
            return True
        return False
예제 #2
0
    def step(self, action):
        self.board = butils.apply_move(self.board, butils.PLAYER_1, action)
        while not self.is_end():
            opponents_actions = butils.get_potentials(self.board,
                                                      butils.PLAYER_2)
            if len(opponents_actions) == 0:
                break

            opponents_move = self.opponent(self.board, opponents_actions)
            self.board = butils.apply_move(self.board, butils.PLAYER_2,
                                           opponents_move)

            if len(butils.get_potentials(self.board, butils.PLAYER_1)) > 0:
                break
        return None, float(0 if not self.is_end() else butils.get_final_result(
            self.board, butils.PLAYER_1)), self.is_end(), None
예제 #3
0
        def move(self, board):
            board_normal = butils.normalize(board, self.my_color,
                                            self.opponent_color,
                                            self.blank_color)
            print(board_normal)
            board_set = butils.Board(iterable=board_normal)
            potentials = butils.get_potentials(board_set, butils.PLAYER_1)

            if len(potentials) == 0:
                return False

            return func(board=board_set, moves=potentials)
예제 #4
0
    def alpha_beta(board,
                   player,
                   depth=1,
                   alpha=None,
                   beta=None,
                   cancellation_token=None):
        if alpha is None:
            alpha = -max_value
        if beta is None:
            beta = max_value

        if depth == max_depth or (not cancellation_token is None
                                  and cancellation_token.is_cancelled()):
            return (ranking_function(board), None)

        potentials = butils.get_potentials(board, player)
        if not potentials:
            #passes game to other player
            #or ends the game
            res = butils.get_final_result(board, player)
            if res is not False:
                sign = 1 if res == player else -1
                return (max_value * sign, None)
            else:
                #passes to the other player
                value = ranking_function(player)
                return (value, None)

        best_move = None
        for potential in potentials:
            #emulate the potential
            board_s = butils.apply_move(board, player, potential)
            value = -alpha_beta(board_s, butils.get_opponent(player), depth +
                                1, -beta, -alpha, cancellation_token)[0]
            if value > alpha:
                alpha = value
                best_move = potential
            if beta <= alpha:
                break

        return (alpha, best_move)
예제 #5
0
 def actions(self):
     return butils.get_potentials(self.board, butils.PLAYER_1)