Closed. This question needs to be more focused. It is not currently accepting answers. Learn more。
想改进这个问题吗?更新问题,使其只关注一个问题editing this post。
我目前正在尝试对tic-tac-toe实现min i max算法,但是我不确定如何在获得所有游戏状态的min/max之后找到移动的方法。我知道你应该看看哪条路有最多的胜利,但我不知道从这里开始。
def minimax(game_state):
if game_state.available_moves():
return evaluate(game_state)
else:
return max_play(game_state)
def evaluate(game_state):
if game_state.has_won(game_state.next_player):
return 1
elif game_state.has_won(game_state.opponent()):
return -1
else:
return 0
def min_play(game_state):
if game_state.available_moves() == []:
return evaluate(game_state)
else:
moves = game_state.available_moves()
best_score = -1
for move in moves:
clone = game_state.make_move(move)
score = max_play(clone)
if score < best_score:
best_move = move
best_score = score
return best_score
def max_play(game_state):
if game_state.available_moves() == []:
return evaluate(game_state)
else:
moves = game_state.available_moves()
best_score = 1
for move in moves:
clone = game_state.make_move(move)
score = min_play(clone)
if score > best_score:
best_move = move
best_score = score
return best_score
最佳答案
在顶层非常简单-您只需要记住当前搜索深度的最佳移动,如果您完全计算深度,则将所有最佳移动设置为该深度的最佳;然后尝试使用较深的树重新计算。顺便说一句,赢的最多并不重要,赢就是赢。
本案伪代码:
bestest_move = None
try:
for depth in range(1, max_depth):
best_score = float('-inf')
for move in possible_moves:
score = evaluate(move)
if score > best_score:
best_move = move
best_score = score
bestest_move = best_move
except Timeout:
pass
move(bestest_move)
关于python - 如何获得minimax算法返回实际 Action ? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28798249/
10-12 17:50