<分区>
我目前正在尝试为 tic tac toe 实现 minimax 算法,但我不确定在获得所有 game_states 的最小/最大值后如何找出如何着手。我知道您应该查看哪条路径的获胜次数最多,但我不知道从哪里开始。
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