2026-rff_mp/stepushovgs/labyrinth/source/classes/maze.py

42 lines
1.4 KiB
Python

from source.classes.cell import Cell
class Maze:
"""Лабиринт"""
def __init__(self, cells, width: int, height: int, start: Cell, exit: Cell):
self.cells = cells
self.width = width
self.height = height
self.start = start
self.exit = exit
pass
def getCell(self, x, y) -> Cell:
return self.cells[x][y]
def getNeighbors(self, cell) -> list[Cell]:
"""Возвращает список соседних проходимых клеток (вверх, вниз, влево, вправо, если в пределах границ и не стена)."""
neighbors = []
c_x, c_y = cell.getXY()
if c_x - 1 >= 0 and not self.cells[c_x - 1][c_y].isWall:
neighbors.append(self.cells[c_x - 1][c_y])
if c_x + 1 < self.width and not self.cells[c_x + 1][c_y].isWall:
neighbors.append(self.cells[c_x + 1][c_y])
if c_y - 1 >= 0 and not self.cells[c_x][c_y - 1].isWall:
neighbors.append(self.cells[c_x][c_y - 1])
if c_y + 1 < self.height and not self.cells[c_x][c_y + 1].isWall:
neighbors.append(self.cells[c_x][c_y + 1])
return neighbors
def printer(self):
"""Выводит в консоль лабиринт (отладочное)"""
for line in self.cells:
for c in line:
print(c.toStr(), end='')
print()