2026-rff_mp/ivantsovma/maze/visualization/command.py

54 lines
1.6 KiB
Python
Raw Normal View History

from abc import ABC, abstractmethod
class Command(ABC):
#Выполняет команду
@abstractmethod
def execute(self):
pass
#Отменяет команду
@abstractmethod
def undo(self):
pass
class MoveCommand(Command):
def __init__(self, player, direction, maze, notifier=None):
self.player = player
self.direction = direction # (dx, dy)
self.maze = maze
self.notifier = notifier
self.previous_cell = player.current_cell
#Перемещает игрока в указанном направлении
def execute(self):
dx, dy = self.direction
new_x = self.player.current_cell.x + dx
new_y = self.player.current_cell.y + dy
new_cell = self.maze.get_cell(new_x, new_y)
# Проверяем, можно ли пройти
if new_cell and new_cell.is_passable():
self.player.move_to(new_cell)
return True
return False
#возвращает на предыдущую клетку
def undo(self):
if self.previous_cell:
self.player.move_to(self.previous_cell)
return True
return False
class Player:
#Игрок в лабиринте
def __init__(self, start_cell):
self.current_cell = start_cell
self.start_cell = start_cell
#Перемещает игрока на новую клетку
def move_to(self, cell):
self.current_cell = cell
#Сбрасывает игрока на стартовую позицию
def reset(self):
self.current_cell = self.start_cell