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})") +