diff --git a/MusinAA/task2/__init__.py b/MusinAA/task2/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/MusinAA/task2/mazeObjects/__init__.py b/MusinAA/task2/mazeObjects/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/MusinAA/task2/mazeObjects/cell.py b/MusinAA/task2/mazeObjects/cell.py new file mode 100644 index 0000000..2eaf73f --- /dev/null +++ b/MusinAA/task2/mazeObjects/cell.py @@ -0,0 +1,13 @@ +class Cell: + """Хранит координаты (x, y) + флаги isWall, isStart, isExit + метод isPassable() (возвращает True для прохода, если не стена).""" + def __init__(self, x: int, y: int, 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 diff --git a/MusinAA/task2/mazeObjects/maze.py b/MusinAA/task2/mazeObjects/maze.py new file mode 100644 index 0000000..042b8df --- /dev/null +++ b/MusinAA/task2/mazeObjects/maze.py @@ -0,0 +1,40 @@ +from task2.mazeObjects.cell import Cell + +class Maze: + """Хранит двумерный массив клеток, + ширину, высоту, ссылки на стартовую и выходную клетку. + Методы: + getCell(x, y), getNeighbors(cell) – возвращает список соседних проходимых клеток + (вверх, вниз, влево, вправо, если в пределах границ и не стена).""" + + def __init__(self, mazeArray: list[list[Cell]], start: dict, end: dict) -> None: + self.mazeArray = mazeArray + self.width = len(mazeArray) + self.height = len(mazeArray[0]) + + self.startCell = self.getCell(start['x'], start['y']) + self.endCell = self.getCell(end['x'], end['y']) + + def getCell(self, x: int, y: int): + return self.mazeArray[y][x] + + def checkCell(self, x: int, y: int): + if not(0 <= x and x < self.width): + return False + if not(0 <= y and y < self.height): + return False + return self.getCell(x, y).isPassable() + + def getNeighbors(self, cell: Cell): + point = (cell.x, cell.y) + offsets = ((0, 1), + (0, -1), + (-1, 0), + (1, 0)) + passableCells = [] + for ofst in offsets: + x = point[0]+ofst[0] + y = point[1]+ofst[1] + if self.checkCell(x, y): + passableCells.append(self.getCell(x, y)) + return passableCells \ No newline at end of file