- Перемещение models в папку source
- Добавление обращения к Maze как к двумерному списку
This commit is contained in:
SerKin0 2026-05-22 23:11:46 +03:00
parent c096613e08
commit 7a84310d5d
4 changed files with 85 additions and 19 deletions

View File

@ -30,4 +30,7 @@ Thumbs.db
# Логи
*.log
.ruff_cache/
.ruff_cache/
/.idea
pupu.py

View File

@ -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

View File

@ -0,0 +1,6 @@
cell_mapping = {
'wall': '#',
'empty': ' ',
'start': 'S',
'exit': 'E'
}

View File

@ -0,0 +1,10 @@
S#########
#
# ###### #
# # # #
# # ## # #
# # # # #
# #### # #
# # #
# E#
##########