feat: add Cell and Maze classes

This commit is contained in:
lomakinae 2026-05-25 02:30:21 +03:00
parent 82e988c965
commit 38f361bdc4
2 changed files with 59 additions and 0 deletions

View File

@ -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

View File

@ -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