forked from UNN/2026-rff_mp
36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
|
|
from abc import ABC, abstractmethod
|
|||
|
|
from maze import Maze, Cell
|
|||
|
|
|
|||
|
|
class MazeBuilder(ABC):
|
|||
|
|
@abstractmethod
|
|||
|
|
def build_from_file(self, filename: str) -> Maze:
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
class TextFileMazeBuilder(MazeBuilder):
|
|||
|
|
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]
|
|||
|
|
|
|||
|
|
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 = maze.get_cell(x, y)
|
|||
|
|
if ch == '#':
|
|||
|
|
cell.is_wall = True
|
|||
|
|
elif ch == 'S':
|
|||
|
|
cell.is_start = True
|
|||
|
|
maze.start = cell
|
|||
|
|
elif ch == 'E':
|
|||
|
|
cell.is_exit = True
|
|||
|
|
maze.exit = cell
|
|||
|
|
# пробел или любой другой символ – проход
|
|||
|
|
if maze.start is None or maze.exit is None:
|
|||
|
|
raise ValueError("Лабиринт должен содержать S (старт) и E (выход)")
|
|||
|
|
return maze
|