59 lines
1.4 KiB
Python
59 lines
1.4 KiB
Python
|
|
from core.cell import Cell
|
||
|
|
|
||
|
|
|
||
|
|
class Maze:
|
||
|
|
def __init__(self, cells_map, start_cell=None, exit_cell=None):
|
||
|
|
self.cells = cells_map
|
||
|
|
self.start = start_cell
|
||
|
|
self.exit = exit_cell
|
||
|
|
|
||
|
|
self.height = len(cells_map)
|
||
|
|
self.width = len(cells_map[0]) if self.height else 0
|
||
|
|
|
||
|
|
def getCell(self, x: int, y: int):
|
||
|
|
if y < 0 or y >= self.height:
|
||
|
|
return None
|
||
|
|
|
||
|
|
if x < 0 or x >= self.width:
|
||
|
|
return None
|
||
|
|
|
||
|
|
return self.cells[y][x]
|
||
|
|
|
||
|
|
def getNeighbors(self, current: Cell):
|
||
|
|
reachable = []
|
||
|
|
|
||
|
|
top = self.getCell(current.x, current.y - 1)
|
||
|
|
right = self.getCell(current.x + 1, current.y)
|
||
|
|
bottom = self.getCell(current.x, current.y + 1)
|
||
|
|
left = self.getCell(current.x - 1, current.y)
|
||
|
|
|
||
|
|
for candidate in (top, right, bottom, left):
|
||
|
|
if candidate is None:
|
||
|
|
continue
|
||
|
|
|
||
|
|
if candidate.isPassable():
|
||
|
|
reachable.append(candidate)
|
||
|
|
|
||
|
|
return reachable
|
||
|
|
|
||
|
|
def hasStart(self):
|
||
|
|
return self.start is not None
|
||
|
|
|
||
|
|
def hasExit(self):
|
||
|
|
return self.exit is not None
|
||
|
|
|
||
|
|
def size(self):
|
||
|
|
return self.width, self.height
|
||
|
|
|
||
|
|
def __str__(self):
|
||
|
|
rows = []
|
||
|
|
|
||
|
|
for line in self.cells:
|
||
|
|
visual = ""
|
||
|
|
|
||
|
|
for cell in line:
|
||
|
|
visual += str(cell)
|
||
|
|
|
||
|
|
rows.append(visual)
|
||
|
|
|
||
|
|
return "\n".join(rows)
|