2026-rff_mp/pogodinda/lab2/tests/test_observer_command.py

45 lines
1.3 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.

from src.maze_builder import TextFileMazeBuilder
from src.maze_solver import MazeSolver
from src.pathfinding import AStarStrategy
from src.observer import ConsoleView
from src.commands import Player, MoveCommand
# Загружаем лабиринт
builder = TextFileMazeBuilder()
maze = builder.build_from_file("demo_maze.txt")
print("=" * 50)
print("ТЕСТ OBSERVER")
print("=" * 50)
solver = MazeSolver(maze, AStarStrategy())
console = ConsoleView()
solver.add_observer(console)
stats = solver.solve("demo_maze")
console.render_stats(stats)
print(f"\nСобытия: {console.events}")
print("\n" + "=" * 50)
print("ТЕСТ COMMAND")
print("=" * 50)
player = Player(maze.start)
print(f"Начальная позиция: {player}")
console.render(maze, player.current_cell)
cmd1 = MoveCommand(player, maze, 'S')
success = cmd1.execute()
print(f"Движение S: {'успешно' if success else 'не удалось'}{player}")
cmd2 = MoveCommand(player, maze, 'S')
success = cmd2.execute()
print(f"Движение S: {'успешно' if success else 'не удалось'}{player}")
console.render(maze, player.current_cell)
print("\nОтмена последнего хода:")
cmd2.undo()
print(f"После undo: {player}")
console.render(maze, player.current_cell)