From 05a9bdd8f4879d02a1691d347c3dbe9b283ac470 Mon Sep 17 00:00:00 2001 From: SerKin0 <71343548+SerKin0@users.noreply.github.com> Date: Wed, 20 May 2026 13:31:25 +0300 Subject: [PATCH] =?UTF-8?q?[2]=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BF=D0=B5=D1=80=D0=B2=D1=8B=D1=85?= =?UTF-8?q?=20=D0=B2=D0=B5=D1=80=D1=81=D0=B8=D0=B9=20=D0=BA=D0=BB=D0=B0?= =?UTF-8?q?=D1=81=D1=81=D0=BE=D0=B2=20`Cell`=20=D0=B8=20`Maze`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skorohodovsa/task_2/.gitignore | 34 ++++++++- skorohodovsa/task_2/models/map.py | 102 +++++++++++++++++++++++++++ skorohodovsa/task_2/requirements.txt | 7 ++ 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 skorohodovsa/task_2/models/map.py create mode 100644 skorohodovsa/task_2/requirements.txt diff --git a/skorohodovsa/task_2/.gitignore b/skorohodovsa/task_2/.gitignore index af253e0..27d8248 100644 --- a/skorohodovsa/task_2/.gitignore +++ b/skorohodovsa/task_2/.gitignore @@ -1 +1,33 @@ -/.obsidian \ No newline at end of file +/.obsidian + +# Виртуальное окружение +venv/ +env/ +.venv/ +.env/ + +# Python кэш +__pycache__/ +*.py[cod] +*.so +.Python + +# Сборка документации Sphinx - ЭТО ВАЖНО! +docs/build/ +docs/source/_build/ + +# Системные файлы +.DS_Store +Thumbs.db +*.swp +*.swo +*~ + +# IDE +.vscode/ +.idea/ + +# Логи +*.log + +.ruff_cache/ \ No newline at end of file diff --git a/skorohodovsa/task_2/models/map.py b/skorohodovsa/task_2/models/map.py new file mode 100644 index 0000000..385c5c9 --- /dev/null +++ b/skorohodovsa/task_2/models/map.py @@ -0,0 +1,102 @@ +from typing import Optional + + +class Cell: + """Класс отвечает за хранение характеристик поля лабиринта""" + + def __init__( + self, + x: int, + y: int, + is_wall: bool = False, + is_start: bool = False, + is_exit: bool = False, + ): + """ + :param x: Координата поля по оси X + :type x: int + :param y: Координата поля по оси Y + :type y: int + :param is_wall: Является ли поле **стеной**, defaults to False + :type is_wall: bool, optional + :param is_start: Является ли поле **началом**, defaults to False + :type is_start: bool, optional + :param is_exit: Является ли поле **концом**, defaults to False + :type is_exit: bool, optional + """ + self.x = x + self.y = y + self.is_wall = is_wall + self.is_start = is_start + self.is_exit = is_exit + + def isPossible(self) -> bool: + """Проверка возможности перемещения в это поле + + :return: Если перемещение возможно, то `True`, иначе `False` + :rtype: bool + """ + return not self.is_wall + + +class Maze: + def __init__( + self, + size: tuple[int, int] = (10, 10), + start_point: tuple[int, int] = (0, 0), + exit_point: tuple[int, int] = (-1, -1), + ): + # Установка размеров лабиринта + self._width = size[0] + self._height = size[1] + + # Установка значения стартового поля в лабиринте + self._start_x = start_point[0] + self._start_y = start_point[1] + + # Установка значения выходного поля в лабиринте + self._exit_x = self._width - 1 if exit_point[0] == -1 else exit_point[0] + self._exit_y = self._height - 1 if exit_point[1] == -1 else exit_point[1] + + # Создание двумерного списка лабиринта + self._map = [ + [Cell(x, y) for x in range(self._width)] for y in range(self._height) + ] + + # Проверяем, что координаты старта находятся в области лабиринта + if self._check_point_in_map(self._start_x, self._start_y): + self._map[self._start_y][self._start_x].is_start = True + else: + raise ValueError( + f"Координата точки старта задана вне области лабиринта: ({self._start_x}, {self._start_y}) not in ({self._width}, {self._height})" + ) + + # Проверяем, что координаты выхода находятся в области лабиринта + if not self._check_point_in_map(self._exit_x, self._exit_y): + raise ValueError( + f"Координата точки выхода задана вне области лабиринта: ({self._start_x}, {self._start_y}) not in ({self._width}, {self._height})" + ) + self._map[self._exit_y][self._exit_x].is_exit = True + + def _check_point_in_map(self, x: int, y: int) -> bool: + return (0 <= x < self._width) and (0 <= y < self._height) + + def get_cell(self, x: int, y: int) -> Optional[Cell]: + if not self._check_point_in_map(x, y): + raise ValueError( + f"Указанные координаты выходят за границы лабиринта: ({x}, {y}) not in ({self._width}, {self._height})" + ) + return self._map[y][x] + + def get_neighbors(self, x: int, y: int) -> Optional[list[Cell]]: + vector_x = [0, 1, 0, -1] + vector_y = [1, 0, -1, 0] + + neighbors = [] + + for vec_x, vec_y in zip(vector_x, vector_y): + temp_x, temp_y = x + vec_x, y + vec_y + if self._check_point_in_map(temp_x, temp_y): + neighbors.append(self._map[temp_y][temp_x]) + + return neighbors diff --git a/skorohodovsa/task_2/requirements.txt b/skorohodovsa/task_2/requirements.txt new file mode 100644 index 0000000..a802ecc --- /dev/null +++ b/skorohodovsa/task_2/requirements.txt @@ -0,0 +1,7 @@ +sphinx +myst-parser +sphinxawesome-theme +nbsphinx +myst-nb +tabulate +bibtexparser