From 8aa0d0ee0e16eb4621c43986598b2851d13618a6 Mon Sep 17 00:00:00 2001 From: tseremonnikovaaa Date: Sun, 24 May 2026 20:14:31 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B0=D1=82=D1=82=D0=B5=D1=80=D0=BD=20Co?= =?UTF-8?q?mmand=20(=D0=BF=D0=BE=D1=88=D0=B0=D0=B3=D0=BE=D0=B2=D0=BE=D0=B5?= =?UTF-8?q?=20=D0=B4=D0=B2=D0=B8=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=20?= =?UTF-8?q?=D0=BE=D1=82=D0=BC=D0=B5=D0=BD=D0=BE=D0=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tseremonnikovaaa/lab2/docs/data/main2.py | 70 +++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/tseremonnikovaaa/lab2/docs/data/main2.py b/tseremonnikovaaa/lab2/docs/data/main2.py index df260a4..2206e54 100644 --- a/tseremonnikovaaa/lab2/docs/data/main2.py +++ b/tseremonnikovaaa/lab2/docs/data/main2.py @@ -261,4 +261,72 @@ class MazeSolverWithObserver(MazeSolver): path_length=len(path), algorithm=self.strategy.__class__.__name__ ) - return path, stats \ No newline at end of file + return path, stats + +class Command(ABC): + @abstractmethod + def execute(self): + pass + + @abstractmethod + def undo(self): + pass + + +class MoveCommand(Command): + def __init__(self, player, direction, maze): + self.player = player + self.direction = direction + self.maze = maze + self.prev_pos = None + + def execute(self): + self.prev_pos = self.player.current_cell + dx, dy = self.direction + nx, ny = self.player.current_cell.x + dx, self.player.current_cell.y + dy + new_cell = self.maze.get_cell(nx, ny) + if new_cell and new_cell.is_passable(): + self.player.current_cell = new_cell + return True + return False + + def undo(self): + if self.prev_pos: + self.player.current_cell = self.prev_pos + return True + return False + + +class Player: + def __init__(self, start_cell): + self.current_cell = start_cell + + +def interactive_move_demo(maze, path): + """Демонстрация движения с отменой последнего шага""" + if not path: + print("Путь не найден, демонстрация движения невозможна.") + return + player = Player(maze.start) + command_history = [] + print("\n Интерактивное движение по найденному пути") + print("Текущая позиция: старт") + for step, cell in enumerate(path): + if cell == maze.start: + continue + prev = path[step-1] + dx = cell.x - prev.x + dy = cell.y - prev.y + cmd = MoveCommand(player, (dx, dy), maze) + cmd.execute() + command_history.append(cmd) + print(f"Шаг {step}: перемещение на ({dx},{dy}), позиция ({player.current_cell.x},{player.current_cell.y})") + if cell == maze.exit: + print("Достигнут выход!") + break + if command_history: + print("\nДемонстрация отмены последнего шага") + cmd = command_history[-1] + cmd.undo() + print(f"Отменён последний шаг, позиция: ({player.current_cell.x},{player.current_cell.y})") +