2026-rff_mp/ivanchenkoam/maze_project/commands.py

34 lines
943 B
Python
Raw Normal View History

"""Паттерн Command - команды для управления игроком"""
from abc import ABC, abstractmethod
from models import Cell, Player
class Command(ABC):
"""Интерфейс команды"""
@abstractmethod
def execute(self) -> None:
pass
@abstractmethod
def undo(self) -> None:
pass
class MoveCommand(Command):
"""Команда перемещения игрока"""
def __init__(self, player: Player, new_cell: Cell):
self._player = player
self._new_cell = new_cell
self._old_cell = player.current_cell
def execute(self) -> None:
"""Выполнение перемещения"""
if self._player.can_move_to(self._new_cell):
self._player.move_to(self._new_cell)
def undo(self) -> None:
"""Отмена перемещения"""
self._player.move_to(self._old_cell)