The Minimax function returns 对井字棋所有可能走法的相同评价

The Minimax function returns the same evaluation for all possible moves in TicTacToe

在主程序循环中,for 循环遍历 TicTacToe 棋盘中的每个空位置,然后在每个位置上应用 minimax 函数以给出该特定着法的评估。然而,对于基于输入的每个特定电路板配置,它 returns 对所有空白点进行相同的评估。


board= [ "X", 1, 2, 
         3, "O", 5, 
         6 , 7, 8]

human = "O"
robot= "X"

def evaluation(board):
    if (winning(board, robot)):
        return +10
    elif (winning(board, human)):
        return -10
    else:
        return 0


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

    if maximizing_player:
        maxEval= -1000000
        empty_spots=list(filter(lambda spot: (spot!="O" and spot!="X"), board))
        # print(f'testing empty spots from maximizing_player{empty_spots}')

        #for the child of that position
        for spot in empty_spots:
            board[spot]=robot
            eval = minimax(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 maxEval is {maxEval}')
        # print(f'The maxEval is {maxEval}')
        return maxEval

    else:
        minEval= +1000000
        empty_spots=list(filter(lambda spot: (spot!="O" and spot!="X"), board))
        # print(f'testing empty spots from minimizing_player{empty_spots}')

        #for each child of that position
        for spot in empty_spots:
            board[spot]=human
            eval= minimax(empty_spots, depth-1, True)

            #then remove the spot
            board[spot]=spot
            minEval=min(minEval, eval)
        # print(f'The minEval is {minEval}')
        return minEval



#the main program loop
while(True):

    while(True):
        try:    
            human_input=int(input("Please enter an integer from 0 to 8: "))
            #this should only take in empty spots
            if(human_input>=0 and human_input<=8):
                break
        except ValueError:
            print("Input must be an integer value.")    

    board[human_input]= human

    #returns a list of empty positions in the array 
    empty_spots=list(filter(lambda spot: (spot!="O" and spot!="X"), board))
    print(empty_spots)

    moves= []
    for spot in empty_spots:
        spot_eval= minimax(empty_spots, len(empty_spots), True)
        print(f'The spot eval for {spot} is {spot_eval}')
        moves.append(spot_eval)

    print(f'This is the moves array {moves}')
    #go through the moves array and pick out the best
    best_move= empty_spots[0]
    for move in moves:
        best_move= max(best_move, move)
    print(f'The best move is {best_move}')



我希望移动数组的输出为 [10,-10,0 ..],对于每个空点依此类推。我的输出是,例如:

让我们看看程序中的这个循环:

for spot in empty_spots:
    spot_eval = minimax(empty_spots, len(empty_spots), True)
    print(f'The spot eval for {spot} is {spot_eval}')
    moves.append(spot_eval)

请注意,对 minimax 函数的调用在 for 循环的所有迭代中都是相同的,因为它不依赖于 spot 变量。所以基本上你在这个循环中对 minimax 函数进行 len(empty_spots) 调用(它们都是一样的)。结果,moves 数组填充了相同的值,等于返回的值 minimax(empty_spots, len(empty_spots), True)