forked from UNN/2026-rff_mp
61 lines
1.3 KiB
Python
61 lines
1.3 KiB
Python
|
|
from abc import ABC, abstractmethod
|
||
|
|
|
||
|
|
from cell import Cell
|
||
|
|
from maze import Maze
|
||
|
|
|
||
|
|
|
||
|
|
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 file:
|
||
|
|
lines = [line.rstrip("\n") for line in file]
|
||
|
|
|
||
|
|
cells = []
|
||
|
|
|
||
|
|
start = None
|
||
|
|
exit = None
|
||
|
|
|
||
|
|
for y, line in enumerate(lines):
|
||
|
|
|
||
|
|
row = []
|
||
|
|
|
||
|
|
for x, char in enumerate(line):
|
||
|
|
|
||
|
|
is_wall = char == "#"
|
||
|
|
is_start = char == "S"
|
||
|
|
is_exit = char == "E"
|
||
|
|
|
||
|
|
cell = Cell(
|
||
|
|
x=x,
|
||
|
|
y=y,
|
||
|
|
is_wall=is_wall,
|
||
|
|
is_start=is_start,
|
||
|
|
is_exit=is_exit
|
||
|
|
)
|
||
|
|
|
||
|
|
if is_start:
|
||
|
|
start = cell
|
||
|
|
|
||
|
|
if is_exit:
|
||
|
|
exit = cell
|
||
|
|
|
||
|
|
row.append(cell)
|
||
|
|
|
||
|
|
cells.append(row)
|
||
|
|
|
||
|
|
if start is None:
|
||
|
|
raise ValueError("Старт S не найден")
|
||
|
|
|
||
|
|
if exit is None:
|
||
|
|
raise ValueError("Выход E не найден")
|
||
|
|
|
||
|
|
return Maze(cells, start, exit)
|