在主程序循环中,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}')
其中,对于每个空点,我期望move数组的输出为[10,-10,0..],依此类推。我的输出是,例如:
让我们看看程序中的循环:
注意,对
minimax
函数的调用在for循环的所有迭代中都是相同的,因为它不依赖于spot
变量。所以基本上你在这个循环中对len(empty_spots)
函数进行minimax
调用(它们都是相同的)。结果,moves
数组填充了相同的值,该值等于minimax(empty_spots, len(empty_spots), True)
相关问题 更多 >
编程相关推荐