我正在尝试为零和游戏实现Minimax算法。我有3种功能:一种选择最佳移动,一种最大化实用程序,另一种最小化实用程序。 get_moves()
返回一个元组列表,每个元组中都有列和行。 apply_move()
采取了一项举措,并将其应用于当前的董事会状态并生成了新的董事会状态。 utility_value
返回当前玩家和对手之间的得分差。
MIN方法:
def min_node(board, color):
sys.setrecursionlimit(100000)
# define player
player = 1 if color == 'dark' else 2
utility = math.inf
# get possible moves.
possible_moves = get_moves(board, player)
best_utility = math.inf
if len(possible_moves) > 0:
for move in possible_moves:
new_board = apply_move(board, player, move[0], move[1])
max_color = 'dark' if color == 'light' else 'light'
utility = max_node(new_board, max_color)
if utility < best_utility:
best_utility = utility
else:
return utility_value(board, color)
return best_utility
MAX方法:
def max_node(board, color):
sys.setrecursionlimit(100000)
# define player
player = 1 if color == 'dark' else 2
best_utility = -math.inf
# get possible moves.
possible_moves = get_moves(board, player)
if len(possible_moves) > 0:
for move in possible_moves:
new_board = apply_move(board, player, move[0], move[1])
min_color = 'light' if color == 'dark' else 'dark'
utility = min_node(new_board, min_color)
if utility > best_utility:
best_utility = utility
else:
return utility_value(board, color)
return best_utility
选择最佳动作:
def select_move(board, color):
player = 1 if color == 'dark' else 2
best_move = (0,0)
best_utility = -math.inf
possible_moves = get_moves(board, player)
if len(possible_moves) > 0:
best_move = possible_moves[0]
print(best_move)
for move in possible_moves:
new_board = apply_move(board, player, move[0], move[1])
utility = min_node(new_board, color)
if utility > best_utility:
best_move = move
best_utility = utility
return best_move
运行代码后,我没有得到想要的结果。除了我在这里发布的方法之外,问题可能在程序中的其他地方,但是我想知道上述方法对于解决minimax问题是否正确。