This repository has been archived on 2026-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
2026-rff_mp/SobolevNS/docs/data/task2_maze/demo.py
2026-05-22 13:48:08 +03:00

55 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
demo.py - короткая демонстрация всех паттернов на маленьком лабиринте.
"""
from maze_solver import (
TextFileMazeBuilder, MazeSolver, ConsoleView,
BFSStrategy, DFSStrategy, AStarStrategy, DijkstraStrategy,
Player, MoveCommand, CommandHistory,
)
def main():
print("=== Builder: загружаем small_10x10.txt ===")
builder = TextFileMazeBuilder()
maze = builder.build_from_file("mazes/small_10x10.txt")
view = ConsoleView(verbose=True)
view.update({"type": "maze_loaded", "maze": maze})
print("\nСам лабиринт:")
print(maze.render_text())
print("\n=== Strategy: пробуем все 4 алгоритма ===")
solver = MazeSolver(maze)
solver.attach(view)
for cls in (BFSStrategy, DFSStrategy, AStarStrategy, DijkstraStrategy):
solver.set_strategy(cls())
stats = solver.solve()
print(f"--- {stats['strategy']} путь длиной {stats['path_length']} ---")
print(maze.render_text(path=stats['path']))
print()
print("=== Command: пройдёмся вручную и сделаем undo ===")
player = Player(maze.start)
history = CommandHistory()
print(f"стартовая позиция: ({player.x},{player.y})")
# Несколько шагов вправо
for d in "DDDD":
ok = history.do(MoveCommand(maze, player, d))
print(f" move {d}: {'ok' if ok else 'blocked'} -> ({player.x},{player.y})")
print("Откатываем 2 хода (undo, undo):")
history.undo()
history.undo()
print(f" теперь игрок в ({player.x},{player.y})")
print("\nЛабиринт с игроком:")
print(maze.render_text(player=player))
if __name__ == "__main__":
main()