2026-rff_mp/kolesovve/task2/docs/data/builders.py
2026-09-05 01:23:16 +03:00

46 lines
1.5 KiB
Python

from abc import ABC, abstractmethod
from model import Maze, Cell
class MazeBuilder(ABC):
@abstractmethod
def buildFromFile(self, filename):
pass
class TextFileMazeBuilder(MazeBuilder):
def buildFromFile(self, filename):
with open(filename, "r", encoding="utf-8") as file:
lines = []
for line in file:
lines.append(line.rstrip("\n"))
height = len(lines)
width = len(lines[0])
maze = Maze(width, height)
start_count = 0
exit_count = 0
for x in range(len(lines)):
row = []
for y in range(len(lines[x])):
symbol = lines[x][y]
if symbol == "#":
cell = Cell(x, y, is_wall=True)
elif symbol == "S":
cell = Cell(x, y, is_start=True)
start_count += 1
elif symbol == "E":
cell = Cell(x, y, is_exit=True)
exit_count += 1
elif symbol == " ":
cell = Cell(x, y)
else:
raise ValueError(f"Неизвестный символ: {symbol}")
row.append(cell)
maze.add_row(row)
if start_count != 1:
raise ValueError("Должен быть ровно один старт S")
if exit_count != 1:
raise ValueError("Должен быть ровно один выход E")
return maze