50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
|
|
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
|