forked from UNN/2026-rff_mp
[2]
- Перемещение models в папку source - Добавление обращения к Maze как к двумерному списку
This commit is contained in:
parent
c096613e08
commit
7a84310d5d
5
skorohodovsa/task_2/.gitignore
vendored
5
skorohodovsa/task_2/.gitignore
vendored
|
|
@ -30,4 +30,7 @@ Thumbs.db
|
|||
# Логи
|
||||
*.log
|
||||
|
||||
.ruff_cache/
|
||||
.ruff_cache/
|
||||
|
||||
/.idea
|
||||
pupu.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
|
||||
6
skorohodovsa/task_2/source/settings.py
Normal file
6
skorohodovsa/task_2/source/settings.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
cell_mapping = {
|
||||
'wall': '#',
|
||||
'empty': ' ',
|
||||
'start': 'S',
|
||||
'exit': 'E'
|
||||
}
|
||||
10
skorohodovsa/task_2/source/templates/10x10_path_v1.txt
Normal file
10
skorohodovsa/task_2/source/templates/10x10_path_v1.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
S#########
|
||||
#
|
||||
# ###### #
|
||||
# # # #
|
||||
# # ## # #
|
||||
# # # # #
|
||||
# #### # #
|
||||
# # #
|
||||
# E#
|
||||
##########
|
||||
Loading…
Reference in New Issue
Block a user