diff --git a/BorisovMI/lab_2/docs/data/maze.py b/BorisovMI/lab_2/docs/data/maze.py index aabd9ad..9d91122 100644 --- a/BorisovMI/lab_2/docs/data/maze.py +++ b/BorisovMI/lab_2/docs/data/maze.py @@ -65,4 +65,58 @@ class Maze: cell = self.getCell(x, y) if cell: cell.isExit = True - self.exit = cell \ No newline at end of file + self.exit = cell + +class MazeBuilder(ABC): + + def buildFromFile(self, filename): + pass + +class TextileMazeBuilder(MazeBuilder): + def buildFromFile(self, filename): + with open(filename, 'r', encoding='utf-8') as f: + lines = f.readlines() + + + lines = [line.rstrip('\n\r') for line in lines] + + height = len(lines) + width = len(lines[0]) if height > 0 else 0 + + + for line in lines: + if len(line) != width: + raise ValueError("все строки одинаковой длины") + + + maze = Maze(width, height) + + + for y in range(height): + for x in range(width): + char = lines[y][x] + cell = maze.getCell(x, y) + + if char == '#': + cell.isWall = True + elif char == ' ': + cell.isWall = False + elif char == 's': + cell.isWall = False + cell.isStart = True + maze.start = cell + elif char == 'e': + cell.isWall = False + cell.isExit = True + maze.exit = cell + else: + raise ValueError(f"неизв сим") + + + if maze.start is None: + raise ValueError("в лабиринте не найден старт") + if maze.exit is None: + raise ValueError("в лабиринте не найден выход") + + return maze +