50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
# модель клетки лабиринта
|
|
|
|
class Cell:
|
|
def __init__(self, x, y, isWall=False, isStart=False, isExit=False):
|
|
self.x = x
|
|
self.y = y
|
|
self.isWall = isWall
|
|
self.isStart = isStart
|
|
self.isExit = isExit
|
|
|
|
def isPassable(self):
|
|
return not self.isWall
|
|
|
|
|
|
# модель лабиринта
|
|
|
|
class Maze:
|
|
|
|
def __init__(self, height, width, start=None, exit=None):
|
|
self.height = height # строки
|
|
self.width = width # столбцы
|
|
self.__grid = [[Cell(x, y) for x in range(width)]
|
|
for y in range(height)]
|
|
self.start = start
|
|
self.exit = exit
|
|
|
|
def getCell(self, x, y) -> Cell:
|
|
if 0 <= x <= self.width and 0 <= y <= self.height:
|
|
return self.__grid[y][x]
|
|
return None
|
|
|
|
def getNeighbors(self, cell):
|
|
dirs = {'left': (-1, 0), 'right': (1, 0),
|
|
'up': (0, 1), 'down': (0, -1)}
|
|
neighbors = []
|
|
for _, val in dirs.items():
|
|
dx, dy = val
|
|
nx, ny = cell.x + dx, cell.y + dy
|
|
neighbor = self.getCell(nx, ny)
|
|
|
|
if neighbor and isinstance(neighbor, Cell) and neighbor.isPassable():
|
|
neighbors.append(neighbor)
|
|
return neighbors
|
|
|
|
|
|
if __name__ == "__main__":
|
|
maze1 = Maze(height=5, width=5, start=0, exit=4)
|
|
cell1 = maze1.getCell(2, 2)
|
|
print(maze1.getNeighbors(cell1))
|