【发布时间】:2016-12-12 23:08:39
【问题描述】:
试图找到最佳着法以及得分。我已经让我的程序正确地返回游戏的分数,但我希望它也能返回移动。如何更改我的代码以使其执行此操作?
类似于this 和this。看我失败的代码here,如果游戏结束返回的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
【问题讨论】:
-
天哪,你是 15 年前的我。开发一款无与伦比的井字游戏是我进入编程领域的入门药物。我似乎记得一个奇妙的树,其中包含我从未完全开始工作的 if..then 语句。这是我了解可读代码重要性的第一课。编辑:哦等等,alpha-beta-pruning?没关系,你比我早了很多。
-
大声笑! 2年前入的,明年高中! :)
标签: python python-3.x tic-tac-toe alpha-beta-pruning