diff --git a/skorohodovsa/task_2/models/map.py b/skorohodovsa/task_2/models/base.py similarity index 52% rename from skorohodovsa/task_2/models/map.py rename to skorohodovsa/task_2/models/base.py index 385c5c9..e0992c5 100644 --- a/skorohodovsa/task_2/models/map.py +++ b/skorohodovsa/task_2/models/base.py @@ -26,9 +26,9 @@ class Cell: """ self.x = x self.y = y - self.is_wall = is_wall - self.is_start = is_start - self.is_exit = is_exit + self._is_wall = is_wall + self._is_start = is_start + self._is_exit = is_exit def isPossible(self) -> bool: """Проверка возможности перемещения в это поле @@ -37,55 +37,40 @@ class Cell: :rtype: bool """ return not self.is_wall + + def __str__(self) -> str: + if self._is_wall: + type_cell = "Стена" + elif self._is_start: + type_cell = "Начало" + elif self._is_exit: + type_cell = "Выход" + else: + type_cell = "Пусто" + + return f"Cell: (x={self.x}, y={self.y}), '{type_cell}'" 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), + size: tuple[int, int] = (10, 10) ): # Установка размеров лабиринта 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 None return self._map[y][x] def get_neighbors(self, x: int, y: int) -> Optional[list[Cell]]: @@ -96,7 +81,8 @@ class Maze: 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]) + value = self.get_cell(temp_x, temp_y) + if value is not None: + neighbors.append(value) return neighbors diff --git a/skorohodovsa/task_2/requirements.txt b/skorohodovsa/task_2/requirements.txt index a802ecc..791d693 100644 --- a/skorohodovsa/task_2/requirements.txt +++ b/skorohodovsa/task_2/requirements.txt @@ -5,3 +5,4 @@ nbsphinx myst-nb tabulate bibtexparser +pytest \ No newline at end of file diff --git a/skorohodovsa/task_2/test/__init__.py b/skorohodovsa/task_2/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/skorohodovsa/task_2/test/map/__init__.py b/skorohodovsa/task_2/test/map/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/skorohodovsa/task_2/test/map/test_cell.py b/skorohodovsa/task_2/test/map/test_cell.py new file mode 100644 index 0000000..b25d10e --- /dev/null +++ b/skorohodovsa/task_2/test/map/test_cell.py @@ -0,0 +1,8 @@ +import pytest +import sys +import os +import copy + +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "./../../models"))) + +from base import Cell \ No newline at end of file diff --git a/skorohodovsa/task_2/test/map/test_maze.py b/skorohodovsa/task_2/test/map/test_maze.py new file mode 100644 index 0000000..e69de29