【发布时间】:2020-11-02 11:17:29
【问题描述】:
我正在尝试制作 tic tac toe AI,它通过使用 minimax 算法以最佳方式玩游戏。我让它工作只是注意到它没有做出最佳动作并且将它与自己对抗结果总是为'X'玩家获胜(它应该导致平局)。 这是我的算法代码:
def getBestMove(state, player):
'''
Minimax Algorithm
'''
winner_loser , done = check_current_state(state)
if done == "Done" and winner_loser == 'O': # If AI won
return 1
elif done == "Done" and winner_loser == 'X': # If Human won
return -1
elif done == "Draw": # Draw condition
return 0
moves = []
empty_cells = []
for i in range(3):
for j in range(3):
if state[i][j] is ' ':
empty_cells.append(i*3 + (j+1))
for empty_cell in empty_cells:
move = {}
move['index'] = empty_cell
new_state = copy_game_state(state)
play_move(new_state, player, empty_cell)
if player == 'O': # If AI
result = getBestMove(new_state, 'X') # make more depth tree for human
move['score'] = result
else:
result = getBestMove(new_state, 'O') # make more depth tree for AI
move['score'] = result
moves.append(move)
# Find best move
best_move = None
if player == 'O': # If AI player
best = -infinity
for move in moves:
if move['score'] > best:
best = move['score']
best_move = move['index']
else:
best = infinity
for move in moves:
if move['score'] < best:
best = move['score']
best_move = move['index']
return best_move
我可以在这里做些什么来解决它?
【问题讨论】:
-
我不是 Python 人,但
return best_move- 不应该返回分数吗? -
什么分数?你是说
return move['score'] -
在例如
if move['score'] > best、move['score']似乎是一个数值,但使用return best_move您似乎返回了一个移动(而不是分数),而它又在递归调用之后被存储。这对我来说似乎是错误的,但也许 Python 在这里隐含地做了一些我不知道的事情。