2026-rff_mp/smirnovad/lab2/docs/data/command.py
2026-05-17 16:50:47 +03:00

48 lines
1.2 KiB
Python

from abc import ABC, abstractmethod
from maze_model import Cell, Maze
class Command(ABC):
@abstractmethod
def execute(self) -> None:
...
@abstractmethod
def undo(self) -> None:
...
class Player:
def __init__(self, start_cell: Cell):
self.current_cell = start_cell
def move_to(self, cell: Cell) -> None:
self.current_cell = cell
def __repr__(self):
return f"Player@({self.current_cell.x},{self.current_cell.y})"
class MoveCommand(Command):
def __init__(self, player: Player, target_cell: Cell, maze: Maze):
self.player = player
self.target_cell = target_cell
self.maze = maze
self.previous_cell = player.current_cell # для undo
def execute(self) -> None:
self.previous_cell = self.player.current_cell
if not self.target_cell.is_passable():
print("Нельзя идти в стену!")
return
self.player.move_to(self.target_cell)
def undo(self) -> None:
self.player.move_to(self.previous_cell)
print(f"Ход отменён. Игрок вернулся в ({self.previous_cell.x}, {self.previous_cell.y})")