From 6932687d9c9dd55b306056c8857d1fde4b0a3319 Mon Sep 17 00:00:00 2001 From: konnovaea Date: Mon, 18 May 2026 19:28:00 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D1=81=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BB=D0=B0=D0=B1=D0=B8=D1=80=D0=B8=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=20=D0=B8=D0=B7=20=D1=84=D0=B0=D0=B9=D0=BB=D0=B0=20(Builder)=20?= =?UTF-8?q?=D0=B8=20=D1=83=D1=81=D0=BF=D0=B5=D1=88=D0=BD=D1=8B=D0=B9=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- konnovaea/maze1.txt | 5 ++ konnovaea/maze_solver.py | 120 +++++++++++++++++++++++++++++++++++++++ konnovaea/test.py | 1 + 3 files changed, 126 insertions(+) create mode 100644 konnovaea/maze1.txt create mode 100644 konnovaea/test.py diff --git a/konnovaea/maze1.txt b/konnovaea/maze1.txt new file mode 100644 index 0000000..2eacde7 --- /dev/null +++ b/konnovaea/maze1.txt @@ -0,0 +1,5 @@ +####### +#S # +# ### # +# E # +####### \ No newline at end of file diff --git a/konnovaea/maze_solver.py b/konnovaea/maze_solver.py index e69de29..1adae7a 100644 --- a/konnovaea/maze_solver.py +++ b/konnovaea/maze_solver.py @@ -0,0 +1,120 @@ +class Cell: + + def __init__(self, x, y): + self.x = x + self.y = y + self.is_wall = False + self.is_start = False + self.is_exit = False + + def is_passable(self): + return not self.is_wall + def __repr__(self): + return f"Cell({self.x}, {self.y})" + +class Maze: + + def __init__(self, width, height): + self.width = width + self.height = height + self.cells = [] + self.start = None + self.exit = None + + for y in range(height): + row = [] + for x in range(width): + row.append(Cell(x, y)) + self.cells.append(row) + + def get_cell(self, x, y): + + if 0 <= x < self.width and 0 <= y < self.height: + return self.cells[y][x] + return None + + def get_neighbors(self, cell): + + neighbors = [] + directions = [(0, -1), (0, 1), (-1, 0), (1,0)] + + for dx, dy in directions: + nx, ny = cell.x + dx, cell.y + dy + neighbor = self.get_cell(nx, ny) + if neighbor and neighbor.is_passable(): + neighbors.append(neighbor) + return neighbors + + def __repr__(self): + return f"Maze({self.width}x{self.height})" + + +from abc import ABC, abstractmethod + +class MazeBuilder(ABC): + + @abstractmethod + def build_from_file(self, filename): + pass + +class TextFileMazeBuilder(MazeBuilder): + + def build_from_file(self, filename): + + with open(filename, 'r', encoding='utf-8') as f: + lines = f.readlines() + + lines = [line.rstrip('\n\r') for line in lines] + + height = len(lines) + width = len(lines[0]) if height > 0 else 0 + + for i, line in enumerate(lines): + if len(line) != width: + raise ValueError(f"Строка {i+1} имеет длину {len(line)}, ожидается {width}") + + maze = Maze(width, height) + start = None + exit_cell = None + + for y, line in enumerate(lines): + for x, ch in enumerate(line): + cell = maze.get_cell(x, y) + + if ch == '#': + cell.is_wall = True + elif ch == ' ': + cell.is_wall = False + elif ch == 'S': + cell.is_wall = False + cell.is_start = True + start = cell + elif ch == 'E': + cell.is_wall = False + cell.ia_exit = True + exit_cell = cell + else: + cell.is_wall = True + + if start is None: + raise ValueError("В лабиринте не найден старт (S)") + if exit_cell is None: + raise ValueError("В лабиринте не найден выход (E)") + + maze.start = start + maze.exit = exit_cell + + return maze + + #тест + +if __name__ == "__main__": + builder = TextFileMazeBuilder() + maze = builder.build_from_file("maze1.txt") + + print(f"лабиринт загружен: {maze}") + print(f"старт: {maze.start}") + print(f"выход: {maze.exit}") + + neighbors = maze.get_neighbors(maze.start) + print(f"соседи старта: {neighbors}") \ No newline at end of file diff --git a/konnovaea/test.py b/konnovaea/test.py new file mode 100644 index 0000000..fd36b2b --- /dev/null +++ b/konnovaea/test.py @@ -0,0 +1 @@ +print("Hello from Python") \ No newline at end of file