2026-rff_mp/BrychkinKA/src/builder/text_file_maze_builder.py
2026-05-25 15:23:54 +03:00

36 lines
1.0 KiB
Python

from src.model.cell import Cell
from src.model.maze import Maze
class TextFileMazeBuilder:
def build_from_file(self, filename):
with open(filename, "r", encoding="utf-8") as f:
lines = [line.rstrip("\n") for line in f]
height = len(lines)
width = max(len(line) for line in lines)
cells = []
start = None
exit_ = None
for y, line in enumerate(lines):
row = []
for x, ch in enumerate(line.ljust(width)):
is_wall = (ch == "#")
is_start = (ch == "S")
is_exit = (ch == "E")
cell = Cell(x, y, is_wall, is_start, is_exit)
row.append(cell)
if is_start:
start = cell
if is_exit:
exit_ = cell
cells.append(row)
if start is None:
raise ValueError("Файл должен содержать S (старт)")
return Maze(width, height, cells, start, exit_)