From 434b63d633d20da8e3b293aca9ed8317ace63a19 Mon Sep 17 00:00:00 2001 From: volkovva Date: Mon, 25 May 2026 02:27:34 +0300 Subject: [PATCH] builder --- VolkovVA/cod.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/VolkovVA/cod.py b/VolkovVA/cod.py index e69de29..ada70ba 100644 --- a/VolkovVA/cod.py +++ b/VolkovVA/cod.py @@ -0,0 +1,59 @@ +class Cell: + def __init__(self, x, y, is_wall=False, is_start=False, is_exit=False): + self.x = x + self.y = y + self.is_wall = is_wall + self.is_start = is_start + self.is_exit = is_exit + + def is_passable(self): + return not self.is_wall + +class Maze: + def __init__(self, width, height): + self.width = width + self.height = height + self.cells = [[Cell(x, y) for x in range(width)] for y in range(height)] + self.start_cell = None + self.exit_cell = None + + def get_cell(self, x, y): + if 0 <= x < self.width and 0 <= y < self.height: + return self.cells[y][x] + return None + + def get_neighbors(self, cell): + neighbors = [] + directions = [(0, 1), (0, -1), (1, 0), (-1, 0)] + for dx, dy in directions: + neighbor = self.get_cell(cell.x + dx, cell.y + dy) + if neighbor and neighbor.is_passable(): + neighbors.append(neighbor) + return neighbors + +class MazeBuilder: + def buildFromFile(self, filename): + with open(filename, 'r') as f: + lines = f.readlines() + + height = len(lines) + width = len(lines[0].strip()) + + maze = Maze(width, height) + + for y, line in enumerate(lines): + for x, char in enumerate(line.strip()): + cell = maze.get_cell(x, y) + if char == '#': + cell.is_wall = True + elif char == 'S': + cell.is_start = True + maze.start_cell = cell + elif char == 'E': + cell.is_exit = True + maze.exit_cell = cell + + if not maze.start_cell or not maze.exit_cell: + raise ValueError("Лабиринт сломан") + + return maze \ No newline at end of file