diff --git a/ProninVV/task-2-oop/MazeBuilder.py b/ProninVV/task-2-oop/MazeBuilder.py new file mode 100644 index 0000000..7aa4195 --- /dev/null +++ b/ProninVV/task-2-oop/MazeBuilder.py @@ -0,0 +1,46 @@ +from abc import ABC, abstractmethod +from labirint import Maze, Cell + + +class MazeBuilder(ABC): + @abstractmethod + def buildFromFile(self, filename): + pass + + +class TextFileMazeBuilder(MazeBuilder): + def __init__(self): + self._maze = None + + def buildFromFile(self, filename: str): + + with open(filename, mode='r', encoding='utf-8') as file: + lines = file.read().splitlines() + + height = len(lines) + width = len(lines[0]) + self._maze = Maze(height, width) + + for y, line in enumerate(lines): + for x, char in enumerate(line): + cell = self._maze.getCell(x, y) + + if char == '#': + cell.isWall = True + elif char == 'S': + cell.isStart = True + self._maze.start = cell + elif char == 'E': + cell.isExit = True + self._maze.exit = cell + return self._maze + + def validate(self): + if self._maze.start is None: + raise "в лабиринте нет старта" + if self._maze.exit is None: + raise "в лабиринте нет начала" + + +# labir = TextFileMazeBuilder().buildFromFile('text.txt') +# print(labir) diff --git a/ProninVV/task-2-oop/labirint.py b/ProninVV/task-2-oop/labirint.py new file mode 100644 index 0000000..f5fd69c --- /dev/null +++ b/ProninVV/task-2-oop/labirint.py @@ -0,0 +1,49 @@ +# модель клетки лабиринта + +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)) diff --git a/ProninVV/task-2-oop/main.py b/ProninVV/task-2-oop/main.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/ProninVV/task-2-oop/main.py @@ -0,0 +1 @@ +