Made solver work
This commit is contained in:
@@ -9,16 +9,19 @@ class ActionStack:
|
||||
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 = []
|
||||
|
||||
def push(self, board: Board) -> None:
|
||||
self.iterator_stack.append(possible_actions(board))
|
||||
self.action_stack.append(None)
|
||||
self.index_stack.append(0)
|
||||
self.state_stack.append(board.state_identifier)
|
||||
|
||||
def get(self) -> Optional[board_actions.Action]:
|
||||
try:
|
||||
@@ -32,6 +35,7 @@ class ActionStack:
|
||||
self.action_stack.pop()
|
||||
self.iterator_stack.pop()
|
||||
self.index_stack.pop()
|
||||
self.state_stack.pop()
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self.index_stack)
|
||||
@@ -51,27 +55,29 @@ class SolitaireSolver:
|
||||
self.stack.push(self.search_board)
|
||||
|
||||
def solve(self) -> List[board_actions.Action]:
|
||||
applied = True
|
||||
max_goal = 0
|
||||
action = None
|
||||
while self.stack:
|
||||
last_action = action
|
||||
assert (self.search_board.state_identifier ==
|
||||
self.stack.state_stack[-1])
|
||||
action = self.stack.get()
|
||||
if not action:
|
||||
if last_action and applied:
|
||||
last_action.undo(self.search_board)
|
||||
assert (self.search_board.state_identifier
|
||||
in self.state_set)
|
||||
self.stack.pop()
|
||||
self.stack.action_stack[-1].undo(self.search_board)
|
||||
assert (self.search_board.state_identifier
|
||||
in self.state_set)
|
||||
continue
|
||||
action.apply(self.search_board)
|
||||
print(f"Applying {str(action)}")
|
||||
applied = True
|
||||
if self.search_board.solved():
|
||||
return self.stack.action_stack
|
||||
if self.search_board.state_identifier in self.state_set:
|
||||
action.undo(self.search_board)
|
||||
applied = False
|
||||
assert self.search_board.state_identifier in self.state_set
|
||||
continue
|
||||
self.state_set.add(self.search_board.state_identifier)
|
||||
self.stack.push(self.search_board)
|
||||
if sum(self.search_board.goal.values()) > max_goal:
|
||||
max_goal = sum(self.search_board.goal.values())
|
||||
print(self.search_board.goal)
|
||||
|
||||
return self.stack.action_stack
|
||||
|
||||
Reference in New Issue
Block a user