Я пытаюсь реализовать алгоритм minmax для подключения четырех (я уменьшил плату, чтобы было легче видеть). Однако я застрял на обратном переходе по дереву игры. У меня есть дерево игр в списке в порядке DFS вместе с изображением, чтобы было понятнее:
.
def DFS(state, depth_limit):
game_tree = []
stack = []
game_tree.append(state)
potential_moves = state.generate_next_states()#get move states
for move in potential_moves:
move_fn = AbstractGameState(snapshot = move.snapshot,
is_game_over_fn = is_game_over_connectfour,
generate_next_states_fn = next_boards_connectfour,
endgame_score_fn = endgame_score_connectfour_faster)
stack.insert(0,move_fn)
while stack != []:
node = stack.pop(0)
if node not in game_tree:
game_tree.append(node)
if node.is_game_over_fn(node.snapshot):
pass
elif node.snapshot.count_pieces() < depth_limit:#set the depth limit
potential_moves = node.generate_next_states()#create next board states
for move in potential_moves:
move_state = AbstractGameState(snapshot = move.snapshot,
is_game_over_fn = is_game_over_connectfour,
generate_next_states_fn = next_boards_connectfour,
endgame_score_fn = endgame_score_connectfour_faster)
stack.insert(0,move_state)#append to the stack
return game_tree
BOARD_EMPTY = ConnectFourBoard(board_array =
(
( 0,0,0),
( 0,0,0),
( 0,0,0 ),
),
players = ['Luke', 'Leia'],
whose_turn = 'Luke')
state = AbstractGameState(snapshot = BOARD_EMPTY,
is_game_over_fn = is_game_over_connectfour,
generate_next_states_fn = next_boards_connectfour,
endgame_score_fn = endgame_score_connectfour_faster)
game_tree = DFS(state,3)
for board in game_tree:
print(board)