46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
|
|
# maze_solver/models.py
|
||
|
|
from typing import Optional, List
|
||
|
|
|
||
|
|
|
||
|
|
class Cell:
|
||
|
|
"""Представляет одну клетку лабиринта."""
|
||
|
|
def __init__(self, x: int, y: int, is_wall: bool = False):
|
||
|
|
self.x = x
|
||
|
|
self.y = y
|
||
|
|
self.isWall = is_wall
|
||
|
|
self.isStart = False
|
||
|
|
self.isExit = False
|
||
|
|
|
||
|
|
def isPassable(self) -> bool:
|
||
|
|
"""Можно ли пройти через клетку."""
|
||
|
|
return not self.isWall
|
||
|
|
|
||
|
|
def __repr__(self):
|
||
|
|
return f"Cell({self.x}, {self.y}, Wall={self.isWall})"
|
||
|
|
|
||
|
|
|
||
|
|
class Maze:
|
||
|
|
"""Хранит полную карту лабиринта."""
|
||
|
|
def __init__(self, width: int, height: int, grid: Optional[List[List[Cell]]] = None):
|
||
|
|
self.width = width
|
||
|
|
self.height = height
|
||
|
|
self.grid = grid if grid else []
|
||
|
|
self.start: Optional[Cell] = None
|
||
|
|
self.exit: Optional[Cell] = None
|
||
|
|
|
||
|
|
def getCell(self, x: int, y: int) -> Optional[Cell]:
|
||
|
|
"""Безопасное получение клетки по координатам."""
|
||
|
|
if 0 <= x < self.width and 0 <= y < self.height:
|
||
|
|
return self.grid[y][x]
|
||
|
|
return None
|
||
|
|
|
||
|
|
def getNeighbors(self, cell: Cell) -> List[Cell]:
|
||
|
|
"""Возвращает список соседних ПРОХОДИМЫХ клеток."""
|
||
|
|
neighbors = []
|
||
|
|
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] # up, down, left, right
|
||
|
|
for dx, dy in directions:
|
||
|
|
nx, ny = cell.x + dx, cell.y + dy
|
||
|
|
neighbor = self.getCell(nx, ny)
|
||
|
|
if neighbor and neighbor.isPassable():
|
||
|
|
neighbors.append(neighbor)
|
||
|
|
return neighbors
|