From c07a4807bf42d51373b5e40ed0d25a71027a0243 Mon Sep 17 00:00:00 2001 From: smirnovavyu Date: Fri, 4 Sep 2026 15:01:58 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?SmirnovaVYu/docs/data=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SmirnovaVYu/docs/data/builders.py | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 SmirnovaVYu/docs/data/builders.py diff --git a/SmirnovaVYu/docs/data/builders.py b/SmirnovaVYu/docs/data/builders.py new file mode 100644 index 00000000..68379330 --- /dev/null +++ b/SmirnovaVYu/docs/data/builders.py @@ -0,0 +1,56 @@ +from abc import ABC, abstractmethod +from models import Cell, Maze + + +class MazeBuilder(ABC): + + @abstractmethod + def build_from_file(self, filename: str) -> Maze: + pass + + +class TextFileMazeBuilder(MazeBuilder): + + WALL_CHAR = '#' + START_CHAR = 'S' + EXIT_CHAR = 'E' + PASS_CHAR = ' ' + + 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.readlines()] + + 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 = Cell(x, y) + + if ch == self.WALL_CHAR: + cell.is_wall = True + elif ch == self.START_CHAR: + cell.is_start = True + elif ch == self.EXIT_CHAR: + cell.is_exit = True + elif ch == self.PASS_CHAR: + pass + else: + cell.is_wall = True + + maze.set_cell(x, y, cell) + + if maze.start is None: + raise ValueError("В лабиринте нет стартовой клетки (S)") + if maze.exit is None: + raise ValueError("В лабиринте нет выхода (E)") + + return maze \ No newline at end of file