Minimax函数为TicTacToe中的所有可能移动返回相同的评估

问题描述 投票:-1回答:1

在主程序循环中,for循环遍历TicTacToe板中的每个空位置,然后在每个位置上应用minimax函数以评估该特定移动。但是,对于基于输入的某个时刻的每个特定电路板配置,它会为所有空白点返回相同的评估。


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 ..]等等,每个空位。我的输出是,例如:

enter image description here

python python-2.7 tic-tac-toe minimax
1个回答
2
投票

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

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变量。所以基本上你在这个循环中做len(empty_spots)调用minimax函数(都是一样的)。结果,moves数组填充了相同的值,该值等于 minimax(empty_spots, len(empty_spots), True) 返回的值

© www.soinside.com 2019 - 2024. All rights reserved.