尝试找到最佳走法和得分。我已经让我的程序可以正确返回游戏的得分,但我希望它也能够返回走法。我该如何修改我的代码来实现这一点?类似于这个和这个。请参见我失败的代码此处,如果游戏结束,应该返回走法而不是
None
。def alphabeta(game_state, alpha, beta, our_turn=True):
if game_state.is_gameover():
return game_state.score()
if our_turn:
score = -9999
for move in game_state.get_possible_moves():
child = game_state.get_next_state(move, True)
temp_max = alphabeta(child, alpha, beta, False)
if temp_max > score:
score = temp_max
alpha = max(alpha, score)
if beta <= alpha:
break
return score
else:
score = 9999
for move in game_state.get_possible_moves():
child = game_state.get_next_state(move, False)
temp_min = alphabeta(child, alpha, beta, True)
if temp_min < score:
score = temp_min
beta = min(beta, score)
if beta <= alpha:
break
return score