[2]lab2 #306

Merged
AlexanderVah merged 9 commits from tseremonnikovaaa/2026-rff_mp:tseremonnikovaaa-lab2 into develop 2026-05-30 11:32:44 +00:00
Showing only changes of commit 8aa0d0ee0e - Show all commits

View File

@ -261,4 +261,72 @@ class MazeSolverWithObserver(MazeSolver):
path_length=len(path),
algorithm=self.strategy.__class__.__name__
)
return path, stats
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})")