Refactored solver loop
This commit is contained in:
@@ -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,32 +86,18 @@ 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
|
continue
|
||||||
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
|
|
||||||
|
|
||||||
action.apply(board)
|
action.apply(board)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user