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

42 lines
1.5 KiB
Python
Raw Normal View History

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