diff --git a/skorohodovsa/task_2/.gitignore b/skorohodovsa/task_2/.gitignore index 27d8248..369c288 100644 --- a/skorohodovsa/task_2/.gitignore +++ b/skorohodovsa/task_2/.gitignore @@ -30,4 +30,7 @@ Thumbs.db # Логи *.log -.ruff_cache/ \ No newline at end of file +.ruff_cache/ + +/.idea +pupu.py \ No newline at end of file diff --git a/skorohodovsa/task_2/models/base.py b/skorohodovsa/task_2/source/models/base.py similarity index 68% rename from skorohodovsa/task_2/models/base.py rename to skorohodovsa/task_2/source/models/base.py index f70972f..2e8f890 100644 --- a/skorohodovsa/task_2/models/base.py +++ b/skorohodovsa/task_2/source/models/base.py @@ -1,5 +1,7 @@ from typing import Optional +from source.settings import cell_mapping + class Cell: """Класс отвечает за хранение характеристик поля лабиринта""" @@ -30,7 +32,7 @@ class Cell: self._is_start = is_start self._is_exit = is_exit - def isPossible(self) -> bool: + def is_possible(self) -> bool: """Проверка возможности перемещения в это поле :return: Если перемещение возможно, то `True`, иначе `False` @@ -58,31 +60,38 @@ class Cell: @is_wall.setter def is_wall(self, value: bool) -> None: - self._clear_flags() - self._is_wall = True + if value: + self._clear_flags() + self._is_wall = value @is_start.setter def is_start(self, value: bool) -> None: - self._clear_flags() - self._is_start = True + if value: + self._clear_flags() + self._is_start = value @is_exit.setter def is_exit(self, value: bool) -> None: - self._clear_flags() - self._is_exit = True + if value: + self._clear_flags() + self._is_exit = value + + def _get_type_cell(self) -> str: + if self._is_wall: + type_cell = cell_mapping.get('wall') + elif self._is_start: + type_cell = cell_mapping.get('start') + elif self._is_exit: + type_cell = cell_mapping.get('exit') + else: + type_cell = cell_mapping.get('empty') + return type_cell 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}'" + return self._get_type_cell() + def __repr__(self): + return f"Cell: (x={self.x}, y={self.y}), '{self._get_type_cell()}'" class Maze: def __init__( @@ -149,7 +158,45 @@ class Maze: for vec_x, vec_y in zip(vector_x, vector_y): temp_x, temp_y = x + vec_x, y + vec_y value = self.get_cell(temp_x, temp_y) - if value is not None: + if value is not None and value.is_possible(): neighbors.append(value) return neighbors + + def __getitem__(self, index: tuple[int, int]) -> Cell: + row, col = index + if not self._check_point_in_map(col, row): + raise IndexError(f"Поле с индексом ({row}, {col}) выходит за пределы лабиринта") + return self._map[row][col] + + def __setitem__(self, index: tuple[int, int], value: str) -> None: + + row, col = index + if not self._check_point_in_map(col, row): + raise IndexError(f"Поле с индексом ({row}, {col}) выходит за пределы лабиринта") + + cell = self._map[row][col] + + cell_type = None + for type_name, symbol in cell_mapping.items(): + if symbol == value: + cell_type = type_name + break + + if cell_type is None: + raise ValueError(f"Значение '{value}' не соответствует ни одному типу клетки") + + if cell_type == "empty": + cell._clear_flags() + else: + setattr(cell, f"is_{cell_type}", True) + + def __str__(self) -> str: + result = "" + + for y in range(self._height): + for x in range(self._width): + result += str(self[y, x]) + result += '\n' + + return result \ No newline at end of file diff --git a/skorohodovsa/task_2/source/settings.py b/skorohodovsa/task_2/source/settings.py new file mode 100644 index 0000000..65da0c8 --- /dev/null +++ b/skorohodovsa/task_2/source/settings.py @@ -0,0 +1,6 @@ +cell_mapping = { + 'wall': '#', + 'empty': ' ', + 'start': 'S', + 'exit': 'E' +} \ No newline at end of file diff --git a/skorohodovsa/task_2/source/templates/10x10_path_v1.txt b/skorohodovsa/task_2/source/templates/10x10_path_v1.txt new file mode 100644 index 0000000..6395399 --- /dev/null +++ b/skorohodovsa/task_2/source/templates/10x10_path_v1.txt @@ -0,0 +1,10 @@ +S######### + # +# ###### # +# # # # +# # ## # # +# # # # # +# #### # # +# # # +# E# +########## \ No newline at end of file