Python 使用alphabeta Tictaoe查找最佳移动
试图找到最好的移动以及分数。我已经让我的程序正确返回游戏的分数,但我希望它也能返回移动。我如何更改代码,使其能够执行此操作? 类似于和。查看我的失败代码,如果游戏结束,返回的Python 使用alphabeta Tictaoe查找最佳移动,python,python-3.x,tic-tac-toe,alpha-beta-pruning,Python,Python 3.x,Tic Tac Toe,Alpha Beta Pruning,试图找到最好的移动以及分数。我已经让我的程序正确返回游戏的分数,但我希望它也能返回移动。我如何更改代码,使其能够执行此操作? 类似于和。查看我的失败代码,如果游戏结束,返回的None应该是移动 def alphabeta(game_state, alpha, beta, our_turn=True): if game_state.is_gameover(): return game_state.score() if our_turn: score
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
def alphabeta(游戏状态,alpha,beta,我们的回合=True):
如果游戏状态为“\u gameover()”:
返回游戏状态。分数()
如果我们转向:
分数=-9999
用于在游戏状态下移动。获取可能的移动()
child=游戏状态。获取下一个状态(移动,真)
temp_max=alphabeta(子项、alpha、beta、False)
如果温度>最大值>分数:
分数=最高温度
阿尔法=最大值(阿尔法,分数)
如果beta您可以跟踪到目前为止的最佳移动,例如:
if game_state.is_gameover():
return game_state.score(), None
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) # _ to disregard the returned move
if temp_max > score:
score = temp_max
best_move = move
alpha = max(alpha, score)
if beta <= alpha:
break
return score, best_move
if game\u state.is\u gameover():
返回游戏状态。得分(),无
如果我们转向:
分数=-9999
用于在游戏状态下移动。获取可能的移动()
child=游戏状态。获取下一个状态(移动,真)
temp_max,u=alphabeta(child,alpha,beta,False)#忽略返回的移动
如果温度>最大值>分数:
分数=最高温度
最佳移动=移动
阿尔法=最大值(阿尔法,分数)
如果beta是,但当我想返回分数时,如果游戏状态为\u gameover()
,则最佳移动配对尚未定义。定义为None
神圣的shnikes,15年前你就是我。开发一款无可匹敌的井字游戏是我进入编程的门户。我似乎记得一棵奇妙的树,上面写着我从未真正投入工作的“如果..那么”语句。这是我第一次学到可读代码的重要性。编辑:哦,等等,阿尔法-贝塔修剪?没关系,你比我早多了。哈哈!两年前就开始了,明年就上高中了!:)