feat: add Cell and Maze classes
This commit is contained in:
parent
82e988c965
commit
38f361bdc4
10
lomakinae/docs/data/02/src/cell.py
Normal file
10
lomakinae/docs/data/02/src/cell.py
Normal 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
|
||||||
49
lomakinae/docs/data/02/src/maze.py
Normal file
49
lomakinae/docs/data/02/src/maze.py
Normal 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
|
||||||
Loading…
Reference in New Issue
Block a user