56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
|
|
#!/usr/bin/env python
|
||
|
|
# coding: utf-8
|
||
|
|
|
||
|
|
# In[ ]:
|
||
|
|
|
||
|
|
|
||
|
|
from typing import List, Optional, Tuple
|
||
|
|
from modelsCell import Cell
|
||
|
|
|
||
|
|
class Maze:
|
||
|
|
"""Модель лабиринта."""
|
||
|
|
|
||
|
|
def __init__(self, width: int = 0, height: int = 0):
|
||
|
|
self.width = width
|
||
|
|
self.height = height
|
||
|
|
self._cells: List[List[Optional[Cell]]] = [
|
||
|
|
[None for _ in range(width)] for _ in range(height)
|
||
|
|
]
|
||
|
|
self.start_cell: Optional[Cell] = None
|
||
|
|
self.exit_cell: Optional[Cell] = None
|
||
|
|
|
||
|
|
def set_cell(self, x: int, y: int, cell: Cell) -> None:
|
||
|
|
"""Установить клетку."""
|
||
|
|
if 0 <= x < self.width and 0 <= y < self.height:
|
||
|
|
self._cells[y][x] = cell
|
||
|
|
|
||
|
|
def get_cell(self, x: int, y: int) -> Optional[Cell]:
|
||
|
|
"""Получить клетку по координатам."""
|
||
|
|
if 0 <= x < self.width and 0 <= y < self.height:
|
||
|
|
return self._cells[y][x]
|
||
|
|
return None
|
||
|
|
|
||
|
|
def get_neighbors(self, cell: Cell) -> List[Cell]:
|
||
|
|
"""Получить проходимых соседей клетки (вверх, вниз, влево, вправо)."""
|
||
|
|
neighbors = []
|
||
|
|
directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] # вверх, вниз, влево, вправо
|
||
|
|
|
||
|
|
for dx, dy in directions:
|
||
|
|
nx, ny = cell.x + dx, cell.y + dy
|
||
|
|
neighbor = self.get_cell(nx, ny)
|
||
|
|
if neighbor and neighbor.is_passable():
|
||
|
|
neighbors.append(neighbor)
|
||
|
|
|
||
|
|
return neighbors
|
||
|
|
|
||
|
|
def get_all_cells(self) -> List[Cell]:
|
||
|
|
"""Получить все клетки лабиринта."""
|
||
|
|
cells = []
|
||
|
|
for y in range(self.height):
|
||
|
|
for x in range(self.width):
|
||
|
|
cell = self.get_cell(x, y)
|
||
|
|
if cell:
|
||
|
|
cells.append(cell)
|
||
|
|
return cells
|
||
|
|
|