2026-05-14 17:18:53 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2026-05-16 17:46:12 +00:00
|
|
|
from Maze import Maze, Cell
|
2026-05-14 17:18:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MazeBuilder(ABC):
|
|
|
|
|
@abstractmethod
|
|
|
|
|
def buildFromFile(self, filename):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TextFileMazeBuilder(MazeBuilder):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self._maze = None
|
|
|
|
|
|
2026-05-19 19:35:33 +00:00
|
|
|
@property
|
|
|
|
|
def maze(self):
|
|
|
|
|
return self._maze
|
|
|
|
|
|
2026-05-14 17:18:53 +00:00
|
|
|
def buildFromFile(self, filename: str):
|
|
|
|
|
|
|
|
|
|
with open(filename, mode='r', encoding='utf-8') as file:
|
|
|
|
|
lines = file.read().splitlines()
|
|
|
|
|
|
|
|
|
|
height = len(lines)
|
|
|
|
|
width = len(lines[0])
|
|
|
|
|
self._maze = Maze(height, width)
|
|
|
|
|
|
|
|
|
|
for y, line in enumerate(lines):
|
|
|
|
|
for x, char in enumerate(line):
|
|
|
|
|
cell = self._maze.getCell(x, y)
|
|
|
|
|
|
|
|
|
|
if char == '#':
|
|
|
|
|
cell.isWall = True
|
|
|
|
|
elif char == 'S':
|
|
|
|
|
cell.isStart = True
|
|
|
|
|
self._maze.start = cell
|
|
|
|
|
elif char == 'E':
|
|
|
|
|
cell.isExit = True
|
|
|
|
|
self._maze.exit = cell
|
2026-05-16 17:46:12 +00:00
|
|
|
self._validate()
|
2026-05-14 17:18:53 +00:00
|
|
|
return self._maze
|
|
|
|
|
|
2026-05-16 17:46:12 +00:00
|
|
|
def _validate(self):
|
2026-05-14 17:18:53 +00:00
|
|
|
if self._maze.start is None:
|
|
|
|
|
raise "в лабиринте нет старта"
|
|
|
|
|
if self._maze.exit is None:
|
|
|
|
|
raise "в лабиринте нет начала"
|