This commit is contained in:
Lukas Wölfer
2019-04-09 01:47:15 +02:00
parent 208c2c83a4
commit 8d5eeda2dc
4 changed files with 267 additions and 75 deletions

View File

@@ -7,6 +7,7 @@ import board
@dataclass
class GoalAction:
"""Move card from field to goal"""
card: board.NumberCard
source_id: int
source_position: board.Position
@@ -40,49 +41,89 @@ class GoalAction:
@dataclass
class RestoreAction:
class BunkerizeAction:
"""Move card from bunker to field"""
card: board.Card
source_id: int
destination_id: int
to_bunker: bool
def _move_from_bunker(self, action_board: board.Board) -> None:
assert action_board.bunker[self.source_id] == self.card
action_board.bunker[self.source_id] = None
action_board.field[self.destination_id].append(self.card)
def _move_to_bunker(self, action_board: board.Board) -> None:
assert action_board.field[self.source_id][-1] == self.card
assert action_board.bunker[self.destination_id] is None
action_board.bunker[self.destination_id] = self.card
action_board.field[self.source_id].pop()
def apply(self, action_board: board.Board) -> None:
"""Do action"""
assert action_board.bunker[self.source_id] == self.card
action_board.bunker[self.source_id] = None
if self.to_bunker:
self._move_to_bunker(action_board)
else:
self._move_from_bunker(action_board)
@dataclass
class StoreAction:
"""Move card from field to bunker"""
card: board.Card
source_id: int
destination_id: int
def undo(self, action_board: board.Board) -> None:
"""Undo action"""
if self.to_bunker:
self._move_from_bunker(action_board)
else:
self._move_to_bunker(action_board)
@dataclass
class MoveAction:
"""Moving a card from one field stack to another"""
card: board.Card
cards: List[board.Card]
source_id: int
destination_id: int
def _shift(self, action_board: board.Board, source: int, dest: int) -> None:
"""Shift a card from the field id 'source' to field id 'dest'"""
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]
if not isinstance(dest_card, board.NumberCard):
raise AssertionError()
if dest_card.suit != self.cards[0].suit:
raise AssertionError()
if dest_card.number + 1 == self.cards[0].number:
raise AssertionError()
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"""
self._shift(action_board, self.source_id, self.destination_id)
def undo(self, action_board: board.Board) -> None:
"""Undo action"""
self._shift(action_board, self.destination_id, self.source_id)
@dataclass
class DragonKillAction:
"""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:
"""Do action"""
assert (action_board.bunker[self.destination_bunker_id] is None or
action_board.bunker[self.destination_bunker_id] == self.dragon)
assert (
action_board.bunker[self.destination_bunker_id] is None
or action_board.bunker[self.destination_bunker_id] == self.dragon
)
assert len(self.source_stacks) == 4
for position, index in self.source_stacks:
if position == board.Position.Field:
@@ -94,13 +135,11 @@ class DragonKillAction:
action_board.bunker[index] = None
else:
raise RuntimeError("Can only kill dragons in field and bunker")
action_board.bunker[self.destination_bunker_id] = board.KilledDragon(
self.dragon)
action_board.bunker[self.destination_bunker_id] = (self.dragon, 4)
def undo(self, action_board: board.Board) -> None:
"""Undo action"""
assert action_board.bunker[self.destination_bunker_id] == board.KilledDragon(
self.dragon)
assert action_board.bunker[self.destination_bunker_id] == (self.dragon, 4)
assert len(self.source_stacks) == 4
for position, index in self.source_stacks:
if position == board.Position.Field:
@@ -115,6 +154,7 @@ class DragonKillAction:
@dataclass
class HuaKillAction:
"""Remove the flower card"""
source_field_id: int
def apply(self, action_board: board.Board) -> None:
@@ -131,5 +171,4 @@ class HuaKillAction:
action_board.flowerGone = False
Action = Union[MoveAction, DragonKillAction,
HuaKillAction, StoreAction, RestoreAction, GoalAction]
Action = Union[MoveAction, DragonKillAction, HuaKillAction, BunkerizeAction, GoalAction]