From 38f361bdc46a8920df164cd4ab3dd17ea3eea76f Mon Sep 17 00:00:00 2001 From: lomakinae Date: Mon, 25 May 2026 02:30:21 +0300 Subject: [PATCH] feat: add Cell and Maze classes --- lomakinae/docs/data/02/src/cell.py | 10 ++++++ lomakinae/docs/data/02/src/maze.py | 49 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 lomakinae/docs/data/02/src/cell.py create mode 100644 lomakinae/docs/data/02/src/maze.py diff --git a/lomakinae/docs/data/02/src/cell.py b/lomakinae/docs/data/02/src/cell.py new file mode 100644 index 0000000..9660e19 --- /dev/null +++ b/lomakinae/docs/data/02/src/cell.py @@ -0,0 +1,10 @@ +class Cell: + def __init__(self, x: int, y: int): + self.x = x + self.y = y + self.is_wall = False + self.is_start = False + self.is_exit = False + + def is_passable(self) -> bool: + return not self.is_wall diff --git a/lomakinae/docs/data/02/src/maze.py b/lomakinae/docs/data/02/src/maze.py new file mode 100644 index 0000000..65e1753 --- /dev/null +++ b/lomakinae/docs/data/02/src/maze.py @@ -0,0 +1,49 @@ +from typing import List, Optional + +from .cell import Cell + + +class Maze: + def __init__(self, width: int, height: int): + self.width = width + self.height = height + self.cells = [[Cell(x, y) for x in range(width)] for y in range(height)] + self.start = None + self.exit = None + + 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 set_cell(self, x: int, y: int, cell_type: str): + cell = self.get_cell(x, y) + if cell is None: + return + + if cell_type == "wall": + cell.is_wall = True + elif cell_type == "start": + if self.start: + self.start.is_start = False + cell.is_start = True + cell.is_wall = False + self.start = cell + elif cell_type == "exit": + if self.exit: + self.exit.is_exit = False + cell.is_exit = True + cell.is_wall = False + self.exit = cell + elif cell_type == "path": + cell.is_wall = False + + 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