Alpha Beta Checkers-同样的玩家总是获胜

时间:2015-12-17 06:00:45

标签: python artificial-intelligence minimax alpha-beta-pruning

我正在使用alpha-beta(minimax)搜索实现一个简单的检查器AI并完成实现。我有两个玩家,1和2,但无论我用于搜索的深度(即使1的深度> 2的深度),玩家2似乎都获胜。我似乎无法发现我做错了什么。你有什么想法可能导致这个?我不确定我的alpha-beta搜索的实现是完全正确的,还是我误解了它。游戏运行正常,只是结果不如预期。如果我能以任何方式澄清这一点,请告诉我。非常感谢您的帮助!为了解决这个问题,我已经删除了GUI,但它不会影响任何内容。非常感谢!

Board是一个具有board属性的类,该属性是一个8x8的int数组。 0空格

1白片

2件红色

3白王

4红王

class Player:
    def __init__(self, id, depth):
        self.id = id
        self.depth = depth

    def board_eval(self, board):
        if len(board.get_moves(self.id)) == 0:
            return float('inf')*-1
        else:
            red = 0
            redK = 0
            white = 0
            whiteK = 0
            for r in range(len(board.board[0])):
                for c in range(len(board.board[0])):
                    if board.board[r][c]>0:
                        if board.board[r][c]==1:
                            white+=1
                        elif board.board[r][c]==2:
                            red+=1
                        elif board.board[r][c]==3:
                            whiteK+=1
                        elif board.board[r][c]==4:
                            redK+=1
            if self.id == 1:
                return white+whiteK-red-redK
            else:
                return red+redK-white-whiteK

    def minimax (self, board, depth, alpha, beta, player):
        board_value = self.board_eval(board)
        if depth == 0 or board_value == float('inf') or board_value == float('inf')*-1:
            return board_value
        if self.id == player:
            for poss_board in board.get_moves(player):
                alpha = max(alpha, self.minimax(poss_board, depth - 1, alpha, beta, (player%2)+1))
                if (alpha >= beta):
                    break
            return alpha
        else:
            for poss_board in board.get_moves(player): #TODO: should this be the other player? pretty sure no
                beta = min(beta, self.minimax(poss_board, depth - 1, alpha, beta, (player%2)+1))
                if (beta <= alpha):
                    break
            return beta


    def update_board(self, board):
        best_move = None
        best_score = float('inf') * -1
        for poss_move in board.get_moves(self.id):
            alpha = self.minimax(poss_move, self.depth-1, best_score, float('inf'), (self.id%2)+1)
            if best_move is None or alpha > best_score:
                best_move = poss_move
                best_score = alpha
            elif alpha == best_score and random.random() <= 0.3: #arbitrary randomness if multiple best moves
                best_move = poss_move
        return best_move

class Game:
    def __init__(self):
        self.board = Board()
        self.p1 = Player(1, 4) #id, depth
        self.p2 = Player(2, 2) 
        self.height=800
        self.width=800
        self.win= GraphWin("Checkerboard", self.width, self.height)
        self.lag_win = GraphWin("Prev Board", self.width, self.height)
        self.lag_board = copy.deepcopy(self.board)
        self.cell_size = 50     # Height and width of checkerboard squares.
        self.offset_x = (self.width - 8*self.cell_size) / 2
        self.offset_y = (self.height - 8*self.cell_size) / 2

    def play(self):
        while True:
            self.board = self.p1.update_board(self.board)
            if self.board is None: #print("Game over, player 2 wins\n")
                return 2
            self.board = self.p2.update_board(self.board)
            if self.board is None: #print("Game over, player 1 wins\n")
                return 1

game = Game()
winner = game.play()

1 个答案:

答案 0 :(得分:-1)

你的board_eval似乎正在返回相对于玩家的分数,但代码的minimax部分似乎是假设绝对分数。如果您消除./app.pyifboard_eval中的return white+whiteK-red-redK会发生什么?