Refactored solver loop

This commit is contained in:
Lukas Wölfer
2019-06-20 23:25:09 +02:00
parent 101c5c7edb
commit 824c2b46db

View File

@@ -56,6 +56,29 @@ def solve(board: Board) -> Iterator[List[board_actions.Action]]:
stack = ActionStack() stack = ActionStack()
stack.push(board) stack.push(board)
def _limit_stack_size(stack_size: int) -> None:
if len(stack) == -1:
stack.pop()
assert stack.action_stack[-1] is not None
stack.action_stack[-1].undo(board)
assert (board.state_identifier
in state_set)
def _backtrack_action() -> None:
stack.pop()
assert stack.action_stack[-1] is not None
stack.action_stack[-1].undo(board)
assert (board.state_identifier
in state_set)
def _skip_loop_move(action: board_actions.Action) -> bool:
if isinstance(action, MoveAction):
for prev_action in stack.action_stack[-2::-1]:
if isinstance(prev_action, MoveAction):
if prev_action.cards == action.cards:
return True
return False
count = 0 count = 0
while stack: while stack:
count += 1 count += 1
@@ -63,31 +86,17 @@ def solve(board: Board) -> Iterator[List[board_actions.Action]]:
count = 0 count = 0
print(f"{len(stack)} {sum(board.goal.values())}") print(f"{len(stack)} {sum(board.goal.values())}")
if len(stack) == -1: # _limit_stack_size(80)
stack.pop()
stack.action_stack[-1].undo(board)
assert (board.state_identifier
in state_set)
assert (board.state_identifier == assert (board.state_identifier ==
stack.state_stack[-1]) stack.state_stack[-1])
action = stack.get() action = stack.get()
if not action: if not action:
stack.pop() _backtrack_action()
stack.action_stack[-1].undo(board)
assert (board.state_identifier
in state_set)
continue continue
if isinstance(action, MoveAction): if _skip_loop_move(action):
drop = False
for prev_action in stack.action_stack[-2::-1]:
if isinstance(prev_action, MoveAction):
if prev_action.cards == action.cards:
drop = True
break
if drop:
continue continue
action.apply(board) action.apply(board)