Tic Tac Toe 的 minimax 算法输出不正确

incorrect output from minimax algorithm for Tic Tac Toe

代码执行没有错误,但是minimax算法的输出不正确,请看一下,` AI_makemove 函数从主循环调用,board_state 是实际板的副本。 函数 AI_makemove 应该是 return 计算机对用户的最佳着法,board_state 是棋盘的当前状态,深度是棋盘中填充的位置数,check_if_won 函数 return 如果状态是当前玩家的胜利状态则为真。

def AI_makemove(board_state , isAI , depth):

temp_board = copy.deepcopy(board_state)

depth+=1
print(temp_board , depth , isAI)

if isAI:
    bestVal = -9999
    a = b = 0
    for i in range(0,3):
        for j in range(0,3):
            if temp_board[i][j] == 0:
                temp_board1  = copy.deepcopy(temp_board)
                temp_board1[i][j] = 2
                if check_if_won(2,temp_board1):
                    return [1 , i, j]
                if depth == 9:
                    return [bestVal , a ,b]
                l = AI_makemove(temp_board1,False,depth)
                if int(l[0]) > bestVal:
                    bestVal = int(l[0])
                    a = int(l[1])
                    b = int(l[2])

else:
    bestVal = +9999
    a = b = 0
    for i in range(0, 3):
        for j in range(0, 3):
            if temp_board[i][j] == 0:
                temp_board1  = copy.deepcopy(temp_board)
                temp_board1[i][j] = 1
                if check_if_won(1,temp_board1):
                    return [-1 , i, j]
                if depth == 9:
                    return [bestVal , a ,b]
                l = AI_makemove(temp_board1,True,depth)
                if int(l[0]) < bestVal:
                    bestVal = int(l[0])
                    a = int(l[1])
                    b = int(l[2])


return [bestVal , a ,b]

我尝试了几次调试代码,但无法修复它,所以我用不同的方法再次编写了代码并且成功了。 Here 的代码