From 86f4936e0ee838420854f01b70b5e9c6115ba66a Mon Sep 17 00:00:00 2001 From: Dima Date: Sat, 23 May 2026 19:39:52 +0300 Subject: [PATCH] =?UTF-8?q?2=20=D1=8D=D1=82=D0=B0=D0=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nikolaevda/task2/Zadanie2.py | 47 +++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/nikolaevda/task2/Zadanie2.py b/nikolaevda/task2/Zadanie2.py index 5c34ea6..1ce8a09 100644 --- a/nikolaevda/task2/Zadanie2.py +++ b/nikolaevda/task2/Zadanie2.py @@ -53,4 +53,49 @@ class Maze: cell = self.get_cell(x, y) if cell: cell.is_exit = True - self.exit = cell \ No newline at end of file + self.exit = cell + + + +class MazeBuilder: + """интерфейс строителя лабиринта""" + + def buildFromFile(self, filename): + raise NotImplementedError + + +class TextFileMazeBuilder(MazeBuilder): + """загрузка лабиринта из текстового файла""" + + def buildFromFile(self, filename): + with open(filename, 'r', encoding='utf-8') as f: + lines = [line.rstrip('\n') for line in f.readlines()] + + height = len(lines) + width = max(len(line) for line in lines) if height > 0 else 0 + + for i in range(height): + if len(lines[i]) < width: + lines[i] = lines[i] + ' ' * (width - len(lines[i])) + + maze = Maze(width, height) + start_count = 0 + exit_count = 0 + + for y, line in enumerate(lines): + for x, ch in enumerate(line): + if ch == '#': + maze.get_cell(x, y).is_wall = True + elif ch == 'S': + maze.set_start(x, y) + start_count += 1 + elif ch == 'E': + maze.set_exit(x, y) + exit_count += 1 + else: + maze.get_cell(x, y).is_wall = False + + if start_count != 1 or exit_count != 1: + raise ValueError(f"Ошибка: S={start_count}, E={exit_count} (нужно по одному)") + + return maze \ No newline at end of file