From d302eea6494082b9609d1d1263a4d12eb4fef318 Mon Sep 17 00:00:00 2001 From: komissarovgo Date: Sun, 17 May 2026 13:25:07 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BA=D0=BE=D0=B4=20models?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- komissarovgo/docs2/data2/maze_lab/models.py | 89 +++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 komissarovgo/docs2/data2/maze_lab/models.py diff --git a/komissarovgo/docs2/data2/maze_lab/models.py b/komissarovgo/docs2/data2/maze_lab/models.py new file mode 100644 index 0000000..f0e665f --- /dev/null +++ b/komissarovgo/docs2/data2/maze_lab/models.py @@ -0,0 +1,89 @@ +from dataclasses import dataclass +from typing import List, Optional + + +@dataclass +class Cell: + + x: int + y: int + is_wall: bool = False + is_start: bool = False + is_exit: bool = False + + def is_passable(self) -> bool: + return not self.is_wall + + def __hash__(self) -> int: + return hash((self.x, self.y)) + + def __eq__(self, other: object) -> bool: + if not isinstance(other, Cell): + return False + return self.x == other.x and self.y == other.y + + +class Maze: + + + def __init__(self, width: int, height: int): + self.width = width + self.height = height + self._cells: List[List[Cell]] = [] + self.start: Optional[Cell] = None + self.exit: Optional[Cell] = None + + def set_cells(self, cells: List[List[Cell]]) -> None: + self._cells = cells + for row in cells: + for cell in row: + if cell.is_start: + self.start = cell + if cell.is_exit: + self.exit = cell + + def get_cell(self, x: int, y: int) -> Optional[Cell]: + if 0 <= x < self.width and 0 <= y < self.height: + return self._cells[y][x] + return None + + def get_neighbors(self, cell: Cell) -> List[Cell]: + neighbors = [] + directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] + + for dx, dy in directions: + nx, ny = cell.x + dx, cell.y + dy + neighbor = self.get_cell(nx, ny) + if neighbor and neighbor.is_passable(): + neighbors.append(neighbor) + + return neighbors + + def __str__(self) -> str: + result = [] + for row in self._cells: + line = '' + for cell in row: + if cell.is_start: + line += 'S' + elif cell.is_exit: + line += 'E' + elif cell.is_wall: + line += '#' + else: + line += ' ' + result.append(line) + return '\n'.join(result) + + +class Player: + + + def __init__(self, start_cell: Cell): + self.current_cell = start_cell + + def move_to(self, cell: Cell) -> None: + self.current_cell = cell + + def can_move_to(self, cell: Cell) -> bool: + return cell.is_passable() \ No newline at end of file