minimax 函数似乎没有为电路板输出正确的评估

The minimax function does not seem to be outputting the correct evaluation for the board

我正在使用 Python 使用 MiniMax 算法创建无与伦比的 TicTacToe 游戏。我的代码似乎没有为每个位置输出正确的评估。据我了解,该算法应该 "test" 每个位置,假设人类玩家发挥最佳。但是,评估始终 return 是 NoneType。

我尝试在 else 的评估函数中使用其他数字。但是,这样做似乎return 不是正确的评估,或者至少,我认为这是不正确的。

board= ["O", 1, "X", 
        "X", 4, "X", 
        6, "O", "O"]

human = "O"
robot= "X"


#winning board combinations
def winning(board, player):
        #horizontal tests
    if((board[0] == player and board[1]==player and board[2]==player) or
        (board[3]==player and board[4]==player and board[5]==player) or
        (board[6]==player and board[7]==player and board[8]==player) or
        #diagonal tests
        (board[0]==player and board[4]==player and board[8]==player) or
        (board[2]==player and board[4]==player and board[6]==player) or
        #vertical tests
        (board[0]==player and board[3]==player and board[6]==player) or
        (board[1]==player and board[4]==player and board[7]==player) or
        (board[2]==player and board[5]==player and board[8]==player)
        ):
        return True
    else:
        return False


#evaluates the score of the game
def evaluation(board):
    if (winning(board, human)):
        return +10
    elif (winning(board, robot)):
        return -10
    elif(len(empty_spots)==0):
        return 0
    #trying to get another value
    # else:
        # return 2


#this is to find the empty spots on the board 
def empty_spots_func(board):
    for i in board:
        if(i!="X" and i!="O"):
            empty_spots.append(i)


def minimax(spot, empty_spots, depth, maximizing_player):
    if depth<=0:
        eval= evaluation(board)
        print(f'The evaluation function returns: {evaluation(board)}')
        return eval

    if maximizing_player:
        maxEval= -1000000
        #for each move in all possible moves
        for spot in empty_spots:

            #make the move
            board[spot]=robot

            #evaluate the outcome of the move
            eval = minimax(spot, empty_spots, depth-1, False)

            #then remove the move and replace it with the number
            board[spot]=spot

            maxEval= max(maxEval, eval)

        # print(f'The maximum evaluation {maxEval}')
        return maxEval

    else:
        minEval= +1000000
        #for each move in all possible moves
        for spot in empty_spots:

            #make the move 
            board[spot]=human

            #evaluate the outcome of the move
            eval= minimax(spot, empty_spots, depth-1, True)

            #then remove the spot
            board[spot]=spot

            #figure out the minimal evaluation
            minEval=min(minEval, eval)

        # print(f'The minimal evaluation is {minEval}')
        return minEval


现在测试主函数中的一个位置:

    test_spot_eval=minimax(0, empty_spots, len(empty_spots), True)
    empty_spots_func(board)
    print(f'The spot eval is {test_spot_eval}')

没有评估中的最后一个 "else",minimax 只是 return 一个 NoneType。

我做了一个单独的程序来检查评估和获胜组合。事实证明,我犯的错误是:

  1. 在接受用户输入后,我没有在板上放置人工输入

  2. 我没有注意到我错误地将评估函数 return 设置为人类获胜的正值和机器人获胜的负值,而本应相反.

可能还有更多错误我还没有注意到。

错误 1 ​​导致我的评估函数 return NoneType,因为没有人工输入,算法无法评估获胜位置。