From c9448652cfc2540efb886f2536e3288def1f3372 Mon Sep 17 00:00:00 2001 From: GordStep Date: Wed, 20 May 2026 13:05:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8E=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=BE=D0=B2?= =?UTF-8?q?=20-=20Cell=20-=20Maze=20-=20TextFileMazeBuilder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../labyrinth/source/classes/builder.py | 64 ++++++++++++ stepushovgs/labyrinth/source/classes/cell.py | 38 ++++++++ stepushovgs/labyrinth/source/classes/maze.py | 39 ++++++++ stepushovgs/labyrinth/test.ipynb | 97 +++++++++++++++++++ stepushovgs/labyrinth/test_lab.txt | 6 ++ 5 files changed, 244 insertions(+) create mode 100644 stepushovgs/labyrinth/source/classes/builder.py create mode 100644 stepushovgs/labyrinth/source/classes/cell.py create mode 100644 stepushovgs/labyrinth/source/classes/maze.py create mode 100644 stepushovgs/labyrinth/test.ipynb create mode 100644 stepushovgs/labyrinth/test_lab.txt diff --git a/stepushovgs/labyrinth/source/classes/builder.py b/stepushovgs/labyrinth/source/classes/builder.py new file mode 100644 index 0000000..dc39544 --- /dev/null +++ b/stepushovgs/labyrinth/source/classes/builder.py @@ -0,0 +1,64 @@ +from abc import ABC, abstractmethod + + +from source.classes.maze import Maze +from source.classes.cell import Cell + +class MazeBuilder(ABC): + @abstractmethod + def buildFromFile(self, filename: str) -> Maze: + pass + + +class TextFileMazeBuilder(MazeBuilder): + def buildFromFile(self, filename: str) -> Maze: + + + with open(filename) as f: + data = f.read().splitlines() + x, y = 0, 0 + width = len(data[0]) + height = len(data) + + cells = [[None] * width for _ in range(height)] + + start, c_exit = None, None + + for line in data: + x = 0 + + for c in line.strip(): + if c == 'S': + cells[y][x] = Cell(x, y, isStart=True) + start = cells[y][x] + x += 1 + elif c == 'E': + cells[y][x] = Cell(x, y, isExit=True) + c_exit = cells[y][x] + x += 1 + elif c == '#': + cells[y][x] = Cell(x, y, isWall=True) + x += 1 + elif c == ' ': + cells[y][x] = Cell(x, y) + x += 1 + else: + print(f'Обнаружен неизвестный символ({c}) в файле лабиринта\nfilename: {filename}\nОн заменён на стену') + cells[y][x] = Cell(x, y, isWall=True) + x += 1 + + y += 1 + + if start == None: + raise ValueError(f'В файле лабиринта не обнаружен вход!\nfilename: {filename}') + + if c_exit == None: + raise ValueError(f'В файле лабиринта не обнаружен выход!\nfilename: {filename}') + + return Maze( + cells=cells, + width=width, + height=height, + start=start, + exit=c_exit + ) \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/classes/cell.py b/stepushovgs/labyrinth/source/classes/cell.py new file mode 100644 index 0000000..80e8be0 --- /dev/null +++ b/stepushovgs/labyrinth/source/classes/cell.py @@ -0,0 +1,38 @@ +class Cell: + + def __init__(self, x, y, isWall=False, isStart=False, isExit=False): + self.__x = x + self.__y = y + self.isWall = isWall + self.isStart = isStart + self.isExit = isExit + pass + + @property + def isPassable(self) -> bool: + """возвращает `True` для прохода, если клетка не стена""" + return not self.isWall + + @property + def x(self) -> int: + """Возвращает координату клетки по оси X""" + return self.__x + + @property + def y(self) -> int: + """Возвращает координату клетки по оси Y""" + return self.__y + + def getXY(self) -> tuple[int, int]: + """Возвращает кортеж координат в формате `(x, y)`""" + return self.__x, self.__y + + def toStr(self): + if self.isWall: + return '#' + elif self.isStart: + return 'S' + elif self.isExit: + return 'E' + else: + return ' ' \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/classes/maze.py b/stepushovgs/labyrinth/source/classes/maze.py new file mode 100644 index 0000000..eca8815 --- /dev/null +++ b/stepushovgs/labyrinth/source/classes/maze.py @@ -0,0 +1,39 @@ +from source.classes.cell import Cell + +class Maze: + def __init__(self, cells, width, height, start, exit): + self.cells = cells + self.width = width + self.height = height + self.start = start + self.exit = exit + pass + + def getCell(self, x, y) -> Cell: + return self.cells[x][y] + + def getNeighbors(self, cell) -> list[Cell]: + neighbors = [] + + c_x, c_y = cell.getXY() + + if c_x - 1 >= 0 and not self.cells[c_x - 1][c_y].isWall: + neighbors.append(self.cells[c_x - 1][c_y]) + + if c_x + 1 < self.width and not self.cells[c_x + 1][c_y].isWall: + neighbors.append(self.cells[c_x + 1][c_y]) + + if c_y - 1 >= 0 and not self.cells[c_x][c_y - 1].isWall: + neighbors.append(self.cells[c_x][c_y - 1]) + + if c_y + 1 < self.height and not self.cells[c_x][c_y + 1].isWall: + neighbors.append(self.cells[c_x][c_y + 1]) + + return neighbors + + def printer(self): + for line in self.cells: + for c in line: + print(c.toStr(), end='') + + print() \ No newline at end of file diff --git a/stepushovgs/labyrinth/test.ipynb b/stepushovgs/labyrinth/test.ipynb new file mode 100644 index 0000000..35aacb6 --- /dev/null +++ b/stepushovgs/labyrinth/test.ipynb @@ -0,0 +1,97 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "4489fc7e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "S # ###\n", + "## # # E\n", + "# # #\n", + "### ## #\n", + "# #\n", + "########\n" + ] + } + ], + "source": [ + "with open('test_lab.txt') as f:\n", + " data = f.readlines()\n", + " for el in data:\n", + " print(el.rstrip())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fde1eddb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "S#####\n", + " # # #\n", + " # #\n", + "## #\n", + " # #\n", + "#### #\n", + "# #\n", + "#E####\n" + ] + } + ], + "source": [ + "from source.classes.builder import TextFileMazeBuilder\n", + "\n", + "builder = TextFileMazeBuilder()\n", + "maze = builder.buildFromFile(filename='test_lab.txt')\n", + "\n", + "maze.printer()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22325f68", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "857c5c04", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/stepushovgs/labyrinth/test_lab.txt b/stepushovgs/labyrinth/test_lab.txt new file mode 100644 index 0000000..951064f --- /dev/null +++ b/stepushovgs/labyrinth/test_lab.txt @@ -0,0 +1,6 @@ +S # ### +## # # E +# # # +### ## # +# # +######## \ No newline at end of file