From 6d20cba4aef21d35775e7ffae25cae2ec6f7c71e Mon Sep 17 00:00:00 2001 From: Pavel Date: Sun, 17 May 2026 22:20:25 +0300 Subject: [PATCH] model --- romanovpv/task 2/docs/data/main.py | 1 + romanovpv/task 2/docs/data/model.py | 76 +++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 romanovpv/task 2/docs/data/main.py create mode 100644 romanovpv/task 2/docs/data/model.py diff --git a/romanovpv/task 2/docs/data/main.py b/romanovpv/task 2/docs/data/main.py new file mode 100644 index 0000000..811c29f --- /dev/null +++ b/romanovpv/task 2/docs/data/main.py @@ -0,0 +1 @@ +import model.py \ No newline at end of file diff --git a/romanovpv/task 2/docs/data/model.py b/romanovpv/task 2/docs/data/model.py new file mode 100644 index 0000000..a424c2e --- /dev/null +++ b/romanovpv/task 2/docs/data/model.py @@ -0,0 +1,76 @@ +class Cell: + + def __init__( + self, + x: int, + y: int, + is_wall=False, + is_start=False, + is_exit=False + ): + + self.x = x + self.y = y + + self.isWall = is_wall + self.isStart = is_start + self.isExit = is_exit + + def isPassable(self): + return not self.isWall + + def __repr__(self): + return f"Cell({self.x},{self.y})" + + def __eq__(self, other): + return ( + isinstance(other, Cell) + and self.x == other.x + and self.y == other.y + ) + + def __hash__(self): + return hash((self.x, self.y)) + +class Maze: + + def __init__(self, width: int, height: int): + self.width = width + self.height = height + + self.cells = [] + + self.start = None + self.exit = None + + def add_row(self, row): + self.cells.append(row) + for cell in row: + if cell.isStart: + self.start = cell + if cell.isExit: + self.exit = cell + + def getCell(self, x, y): + if 0 <= x < self.height and 0 <= y < self.width: + return self.cells[x][y] + return None + + def getNeighbors(self, cell): + directions = [ + (-1, 0), # вверх + (1, 0), # вниз + (0, -1), # влево + (0, 1) # вправо + ] + neighbors = [] + for dx, dy in directions: + nx = cell.x + dx + ny = cell.y + dy + neighbor = self.getCell(nx, ny) + if ( + neighbor + and neighbor.isPassable() + ): + neighbors.append(neighbor) + return neighbors \ No newline at end of file