2026-rff_mp/famutdinovmd/builders.py

61 lines
2.1 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 abc import ABC, abstractmethod
from models import Cell, Maze
class MazeBuilder(ABC):
"""Абстрактный строитель лабиринта"""
@abstractmethod
def build_from_file(self, filename: str) -> Maze:
"""Построить лабиринт из файла"""
pass
class TextFileMazeBuilder(MazeBuilder):
"""Строитель лабиринта из текстового файла"""
WALL_CHAR = '#'
START_CHAR = 'S'
EXIT_CHAR = 'E'
PASS_CHAR = ' '
def build_from_file(self, filename: str) -> Maze:
"""Читает текстовый файл и создаёт лабиринт"""
with open(filename, 'r', encoding='utf-8') as f:
lines = [line.rstrip('\n') for line in f.readlines()]
if not lines:
raise ValueError("Файл с лабиринтом пуст")
height = len(lines)
width = max(len(line) for line in lines)
maze = Maze(width, height)
for y, line in enumerate(lines):
for x, ch in enumerate(line):
if x >= width:
continue
cell = Cell(x, y)
if ch == self.WALL_CHAR:
cell.is_wall = True
elif ch == self.START_CHAR:
cell.is_start = True
elif ch == self.EXIT_CHAR:
cell.is_exit = True
elif ch == self.PASS_CHAR:
pass # проходимая клетка (всё уже настроено)
else:
cell.is_wall = True # неизвестный символ считаем стеной
maze.set_cell(x, y, cell)
# Валидация
if maze.start is None:
raise ValueError("В лабиринте нет стартовой клетки (S)")
if maze.exit is None:
raise ValueError("В лабиринте нет выхода (E)")
return maze