diff --git a/shenzhen_solitaire/clicker/__init__.py b/shenzhen_solitaire/clicker/__init__.py index 9089c27..61c176c 100644 --- a/shenzhen_solitaire/clicker/__init__.py +++ b/shenzhen_solitaire/clicker/__init__.py @@ -78,7 +78,7 @@ def handle_action( (field_x + (size_x - field_x) // 2, field_y + (size_y - field_y) // 2), offset, ) - time.sleep(0.5) + time.sleep(1) return if isinstance(action, board_actions.GoalAction): dst_x, dst_y, _, _ = adjustment.get_square( @@ -100,12 +100,6 @@ def handle_action( return raise AssertionError("You forgot an Action type") -def automatic(action: board_actions.Action) -> bool: - if isinstance(action, board_actions.HuaKillAction): - return True - if isinstance(action, board_actions.GoalAction) and action.obvious: - return True - return False def handle_actions( actions: List[board_actions.Action], @@ -115,9 +109,10 @@ def handle_actions( automatic_count = 0 for action in actions: print(action) - if automatic(action): + if action.automatic(): automatic_count += 1 else: time.sleep(0.5 * automatic_count) automatic_count = 0 handle_action(action, offset, conf) + time.sleep(0.5 * automatic_count) diff --git a/shenzhen_solitaire/solver/board_actions.py b/shenzhen_solitaire/solver/board_actions.py index da79f5f..d6e0e5b 100644 --- a/shenzhen_solitaire/solver/board_actions.py +++ b/shenzhen_solitaire/solver/board_actions.py @@ -31,6 +31,13 @@ class Action: self._undo(action_board) assert action_board.state_identifier == self._before_state + def automatic(self) -> bool: + if isinstance(self, HuaKillAction): + return True + if isinstance(self, GoalAction) and self.obvious: + return True + return False + @dataclass class GoalAction(Action): diff --git a/shenzhen_solitaire/solver/board_possibilities.py b/shenzhen_solitaire/solver/board_possibilities.py index d6259c0..537b512 100644 --- a/shenzhen_solitaire/solver/board_possibilities.py +++ b/shenzhen_solitaire/solver/board_possibilities.py @@ -125,7 +125,6 @@ def possible_goal_move_actions( ] top_cards = field_cards + bunker_cards - result: List[board_actions.GoalAction] = [] for source, index, card in top_cards: if not (card.number == search_board.getGoal(card.suit) + 1): continue @@ -145,9 +144,6 @@ def possible_goal_move_actions( ) break - result = sorted(result, key=lambda x: not x.obvious) - yield from iter(result) - def _can_stack(bottom: board.Card, top: board.Card) -> bool: if not isinstance(bottom, board.NumberCard): @@ -216,11 +212,19 @@ def possible_field_move_actions( ) -def possible_actions(search_board: board.Board) -> Iterator[board_actions.Action]: +def possible_actions(search_board: board.Board) -> List[board_actions.Action]: """Enumerate all possible actions on the current search_board""" - yield from possible_huakill_action(search_board) - yield from possible_dragonkill_actions(search_board) - yield from possible_goal_move_actions(search_board) - yield from possible_debunkerize_actions(search_board) - yield from possible_field_move_actions(search_board) - yield from possible_bunkerize_actions(search_board) + result: List[board_actions.Action] = [ + *list(possible_huakill_action(search_board)), + *list(possible_dragonkill_actions(search_board)), + *list(possible_goal_move_actions(search_board)), + *list(possible_debunkerize_actions(search_board)), + *list(possible_field_move_actions(search_board)), + *list(possible_bunkerize_actions(search_board)), + ] + + for x in result: + if x.automatic(): + return [x] + + return result diff --git a/shenzhen_solitaire/solver/solver.py b/shenzhen_solitaire/solver/solver.py index 2b33830..0f2f531 100644 --- a/shenzhen_solitaire/solver/solver.py +++ b/shenzhen_solitaire/solver/solver.py @@ -2,52 +2,55 @@ import typing from typing import Iterator, List, Optional import time - +from dataclasses import dataclass from ..board import Board from . import board_actions from .board_actions import DragonKillAction, GoalAction, HuaKillAction, MoveAction from .board_possibilities import possible_actions +@dataclass +class ActionStackFrame: + iterator: Iterator[board_actions.Action] + action: Optional[board_actions.Action] + state: int + + def next(self) -> Optional[board_actions.Action]: + """Get next iteration of top action iterator""" + try: + self.action = next(self.iterator) + except StopIteration: + return None + return self.action + + class ActionStack: """Stack of chosen actions on the board""" - iterator_stack: List[Iterator[board_actions.Action]] - action_stack: List[Optional[board_actions.Action]] - index_stack: List[int] - state_stack: List[int] - def __init__(self) -> None: - self.iterator_stack = [] - self.index_stack = [] - self.action_stack = [] - self.state_stack = [] + self.frames: List[ActionStackFrame] = [] def push(self, board: Board) -> None: """Append another board state to stack""" - self.iterator_stack.append(possible_actions(board)) - self.action_stack.append(None) - self.index_stack.append(0) - self.state_stack.append(board.state_identifier) + self.frames.append( + ActionStackFrame( + iterator=iter(possible_actions(board)), + action=None, + state=board.state_identifier, + ) + ) - def get(self) -> Optional[board_actions.Action]: + @property + def top(self) -> ActionStackFrame: """Get next iteration of top action iterator""" - try: - self.action_stack[-1] = next(self.iterator_stack[-1]) - except StopIteration: - return None - self.index_stack[-1] += 1 - return self.action_stack[-1] + return self.frames[-1] def pop(self) -> None: """Pop one action from stack""" - self.action_stack.pop() - self.iterator_stack.pop() - self.index_stack.pop() - self.state_stack.pop() + self.frames.pop() def __len__(self) -> int: - return len(self.index_stack) + return len(self.frames) def solve( @@ -61,27 +64,30 @@ def solve( def _limit_stack_size(stack_size: int) -> None: if len(stack) == stack_size: stack.pop() - assert stack.action_stack[-1] is not None - stack.action_stack[-1].undo(board) + assert stack.top.action is not None + stack.top.action.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 stack.top.action is not None + stack.top.action.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 + if not isinstance(action, MoveAction): + return False + for frame in stack.frames[-2::-1]: + if not isinstance(frame.action, MoveAction): + continue + if frame.action.cards == action.cards: + return True return False iter_start = time.time() count = 0 while stack: + count += 1 if count > 5000: count = 0 @@ -92,10 +98,10 @@ def solve( # _limit_stack_size(80) - assert board.state_identifier == stack.state_stack[-1] - action = stack.get() + assert board.state_identifier == stack.top.state + action = stack.top.next() - if not action: + if action is None: _backtrack_action() continue @@ -105,14 +111,11 @@ def solve( action.apply(board) if board.solved(): - yield stack.action_stack + assert all(x.action is not None for x in stack.frames) + yield [typing.cast(board_actions.Action, x.action) for x in stack.frames] iter_start = time.time() - stack.action_stack[-1].undo(board) - while isinstance( - stack.action_stack[-1], (GoalAction, HuaKillAction, DragonKillAction) - ): - stack.pop() - stack.action_stack[-1].undo(board) + action.undo(board) + assert board.state_identifier in state_set continue if board.state_identifier in state_set: