python alpha beta_python – 使用alphabeta TicTacToe找到最佳移动
試圖找到最佳動(dòng)作以及得分.我已經(jīng)讓我的程序正確地返回游戲的分?jǐn)?shù),但我希望它也能返回移動(dòng).如何更改我的代碼以便它執(zhí)行此操作?
與
this和
this類似.請(qǐng)參閱我的失敗代碼
here,如果游戲結(jié)束則返回?zé)o應(yīng)該是移動(dòng).
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
最佳答案 到目前為止,您可以跟蹤最佳移動(dòng),例如:
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
和其他情況類似
總結(jié)
以上是生活随笔為你收集整理的python alpha beta_python – 使用alphabeta TicTacToe找到最佳移动的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: had oop 链接mysql_php
- 下一篇: python代码读取外部变量_关于pyt