Made solver work
This commit is contained in:
@@ -6,21 +6,39 @@ from . import board
|
||||
|
||||
|
||||
class Action:
|
||||
_before_state: int = 0
|
||||
_after_state: int = 0
|
||||
|
||||
def _apply(self, action_board: board.Board) -> None:
|
||||
pass
|
||||
|
||||
def _undo(self, action_board: board.Board) -> None:
|
||||
pass
|
||||
|
||||
def apply(self, action_board: board.Board) -> None:
|
||||
pass
|
||||
#print(f"Applying {str(self)}")
|
||||
assert self._before_state == 0
|
||||
assert self._after_state == 0
|
||||
self._before_state = action_board.state_identifier
|
||||
self._apply(action_board)
|
||||
self._after_state = action_board.state_identifier
|
||||
|
||||
def undo(self, action_board: board.Board) -> None:
|
||||
pass
|
||||
#print(f"Undoing {str(self)}")
|
||||
assert action_board.state_identifier == self._after_state
|
||||
self._undo(action_board)
|
||||
assert action_board.state_identifier == self._before_state
|
||||
|
||||
|
||||
@dataclass
|
||||
class GoalAction:
|
||||
class GoalAction(Action):
|
||||
"""Move card from field to goal"""
|
||||
|
||||
card: board.NumberCard
|
||||
source_id: int
|
||||
source_position: board.Position
|
||||
|
||||
def apply(self, action_board: board.Board) -> None:
|
||||
def _apply(self, action_board: board.Board) -> None:
|
||||
"""Do action"""
|
||||
if self.source_position == board.Position.Field:
|
||||
assert action_board.field[self.source_id][-1] == self.card
|
||||
@@ -35,7 +53,7 @@ class GoalAction:
|
||||
else:
|
||||
raise RuntimeError("Unknown position")
|
||||
|
||||
def undo(self, action_board: board.Board) -> None:
|
||||
def _undo(self, action_board: board.Board) -> None:
|
||||
"""Undo action"""
|
||||
assert action_board.goal[self.card.suit] == self.card.number
|
||||
if self.source_position == board.Position.Field:
|
||||
@@ -49,15 +67,13 @@ class GoalAction:
|
||||
|
||||
|
||||
@dataclass
|
||||
class BunkerizeAction:
|
||||
class BunkerizeAction(Action):
|
||||
"""Move card from bunker to field"""
|
||||
|
||||
card: board.Card
|
||||
bunker_id: int
|
||||
field_id: int
|
||||
to_bunker: bool
|
||||
_before_state: int = 0
|
||||
_after_state: int = 0
|
||||
|
||||
def _move_from_bunker(self, action_board: board.Board) -> None:
|
||||
assert action_board.bunker[self.bunker_id] == self.card
|
||||
@@ -70,14 +86,14 @@ class BunkerizeAction:
|
||||
action_board.bunker[self.bunker_id] = self.card
|
||||
action_board.field[self.field_id].pop()
|
||||
|
||||
def apply(self, action_board: board.Board) -> None:
|
||||
def _apply(self, action_board: board.Board) -> None:
|
||||
"""Do action"""
|
||||
if self.to_bunker:
|
||||
self._move_to_bunker(action_board)
|
||||
else:
|
||||
self._move_from_bunker(action_board)
|
||||
|
||||
def undo(self, action_board: board.Board) -> None:
|
||||
def _undo(self, action_board: board.Board) -> None:
|
||||
"""Undo action"""
|
||||
if self.to_bunker:
|
||||
self._move_from_bunker(action_board)
|
||||
@@ -86,14 +102,12 @@ class BunkerizeAction:
|
||||
|
||||
|
||||
@dataclass
|
||||
class MoveAction:
|
||||
class MoveAction(Action):
|
||||
"""Moving a card from one field stack to another"""
|
||||
|
||||
cards: List[board.Card]
|
||||
source_id: int
|
||||
destination_id: int
|
||||
_before_state: int = 0
|
||||
_after_state: int = 0
|
||||
|
||||
def _shift(
|
||||
self,
|
||||
@@ -102,21 +116,18 @@ class MoveAction:
|
||||
dest: int) -> None:
|
||||
"""Shift a card from the field id 'source' to field id 'dest'"""
|
||||
|
||||
print(f"Incoming shift from {source} to {dest}")
|
||||
print(
|
||||
*
|
||||
list(itertools.zip_longest(
|
||||
action_board.field[source][::-1],
|
||||
self.cards[::-1]))[::-1],
|
||||
sep="\n")
|
||||
print("--- GO ---")
|
||||
|
||||
for stack_offset, card in enumerate(
|
||||
self.cards, start=-len(self.cards)):
|
||||
assert action_board.field[source][stack_offset] == card
|
||||
|
||||
if action_board.field[dest]:
|
||||
dest_card = action_board.field[dest][-1]
|
||||
action_board.field[source] = action_board.field[
|
||||
source][:-len(self.cards)]
|
||||
action_board.field[dest].extend(self.cards)
|
||||
|
||||
def _apply(self, action_board: board.Board) -> None:
|
||||
"""Do action"""
|
||||
if action_board.field[self.destination_id]:
|
||||
dest_card = action_board.field[self.destination_id][-1]
|
||||
if not all(isinstance(x, board.NumberCard) for x in self.cards):
|
||||
raise AssertionError()
|
||||
if not isinstance(dest_card, board.NumberCard):
|
||||
@@ -127,49 +138,22 @@ class MoveAction:
|
||||
raise AssertionError()
|
||||
if dest_card.number != self.cards[0].number + 1:
|
||||
raise AssertionError()
|
||||
|
||||
print(
|
||||
*
|
||||
itertools.zip_longest(
|
||||
action_board.field[source],
|
||||
action_board.field[dest]),
|
||||
sep="\n")
|
||||
action_board.field[source] = action_board.field[
|
||||
source][:-len(self.cards)]
|
||||
action_board.field[dest].extend(self.cards)
|
||||
print()
|
||||
print(
|
||||
*
|
||||
itertools.zip_longest(
|
||||
action_board.field[source],
|
||||
action_board.field[dest]),
|
||||
sep="\n")
|
||||
print()
|
||||
print()
|
||||
|
||||
def apply(self, action_board: board.Board) -> None:
|
||||
"""Do action"""
|
||||
self._before_state = action_board.state_identifier
|
||||
self._shift(action_board, self.source_id, self.destination_id)
|
||||
self._after_state = action_board.state_identifier
|
||||
|
||||
def undo(self, action_board: board.Board) -> None:
|
||||
def _undo(self, action_board: board.Board) -> None:
|
||||
"""Undo action"""
|
||||
print("Undo shift")
|
||||
assert action_board.state_identifier == self._after_state
|
||||
self._shift(action_board, self.destination_id, self.source_id)
|
||||
assert action_board.state_identifier == self._before_state
|
||||
|
||||
|
||||
@dataclass
|
||||
class DragonKillAction:
|
||||
class DragonKillAction(Action):
|
||||
"""Removing four dragons from the top of the stacks to a bunker"""
|
||||
|
||||
dragon: board.SpecialCard
|
||||
source_stacks: List[Tuple[board.Position, int]]
|
||||
destination_bunker_id: int
|
||||
|
||||
def apply(self, action_board: board.Board) -> None:
|
||||
def _apply(self, action_board: board.Board) -> None:
|
||||
"""Do action"""
|
||||
assert (
|
||||
action_board.bunker[self.destination_bunker_id] is None
|
||||
@@ -188,7 +172,7 @@ class DragonKillAction:
|
||||
raise RuntimeError("Can only kill dragons in field and bunker")
|
||||
action_board.bunker[self.destination_bunker_id] = (self.dragon, 4)
|
||||
|
||||
def undo(self, action_board: board.Board) -> None:
|
||||
def _undo(self, action_board: board.Board) -> None:
|
||||
"""Undo action"""
|
||||
assert action_board.bunker[self.destination_bunker_id] == (
|
||||
self.dragon, 4)
|
||||
@@ -204,12 +188,12 @@ class DragonKillAction:
|
||||
|
||||
|
||||
@dataclass
|
||||
class HuaKillAction:
|
||||
class HuaKillAction(Action):
|
||||
"""Remove the flower card"""
|
||||
|
||||
source_field_id: int
|
||||
|
||||
def apply(self, action_board: board.Board) -> None:
|
||||
def _apply(self, action_board: board.Board) -> None:
|
||||
"""Do action"""
|
||||
assert not action_board.flower_gone
|
||||
assert (action_board.field[self.source_field_id][-1]
|
||||
@@ -217,12 +201,8 @@ class HuaKillAction:
|
||||
action_board.field[self.source_field_id].pop()
|
||||
action_board.flower_gone = True
|
||||
|
||||
def undo(self, action_board: board.Board) -> None:
|
||||
def _undo(self, action_board: board.Board) -> None:
|
||||
"""Undo action"""
|
||||
assert action_board.flower_gone
|
||||
action_board.field[self.source_field_id].append(board.SpecialCard.Hua)
|
||||
action_board.flower_gone = False
|
||||
|
||||
|
||||
Action = Union[MoveAction, DragonKillAction,
|
||||
HuaKillAction, BunkerizeAction, GoalAction]
|
||||
|
||||
Reference in New Issue
Block a user