2026-rff_mp/ivantsovma/maze/play_maze.py

93 lines
3.0 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.

import sys
import os
#Добавляем корневую папку
sys.path.insert(0, r'C:\ivantsovma\docs\MazeProject')
from builders import TextFileMazeBuilder
from strategies import BFSStrategy
from visualization.observer import Event, EventType
from visualization.console_view import ConsoleView
from visualization.game_controller import GameController
def play_maze():
print("НАЙДИ ВЫХОД ИЗ ЛАБИРИНТА")
#Загружаем лабиринт
builder = TextFileMazeBuilder()
#Выбор лабиринта
print("\nВыберите лабиринт:")
print("1. Простой лабиринт (simple_maze.txt)")
print("2. Сложный лабиринт (small_maze.txt)")
choice = input("Ваш выбор (1/2): ").strip()
if choice == "1":
maze_file = "mazes/simple_maze.txt"
else:
maze_file = "mazes/small_maze.txt"
try:
maze = builder.build_from_file(maze_file)
except Exception as e:
print(f"Ошибка загрузки лабиринта: {e}")
return
#Создаём контроллер и отображение
controller = GameController(maze)
view = ConsoleView()
#Подписываем view на события контроллера
controller.attach(view)
#Уведомляем о загрузке лабиринта
controller.notify(Event(EventType.MAZE_LOADED, maze))
#Находим и показываем оптимальный путь (для подсказки)
bfs = BFSStrategy()
optimal_path = bfs.find_path(maze, maze.start, maze.exit)
controller.notify(Event(EventType.PATH_FOUND, optimal_path))
print("\nУправление:")
print(" w - вверх s - вниз a - влево d - вправо")
print(" u - отменить q - выход")
# Игровой цикл
while True:
view.render()
# Проверка победы
if controller.get_player_position() == maze.exit:
view.render()
print("\nВЫ НАШЛИ ВЫХОД!")
break
# Чтение команды
cmd = input("\nВведите команду: ").lower().strip()
if cmd == 'q':
print("Выход из игры...")
break
elif cmd == 'u':
controller.undo()
elif cmd == 'w':
controller.move((0, -1))
elif cmd == 's':
controller.move((0, 1))
elif cmd == 'a':
controller.move((-1, 0))
elif cmd == 'd':
controller.move((1, 0))
else:
print("Неизвестная команда! Используйте w/a/s/d, u или q")
if __name__ == "__main__":
try:
play_maze()
except KeyboardInterrupt:
print("\n\nИгра прервана пользователем")
except Exception as e:
print(f"\nОшибка: {e}")
import traceback
traceback.print_exc()