forked from UNN/2026-rff_mp
model
This commit is contained in:
parent
e306180a73
commit
6d20cba4ae
1
romanovpv/task 2/docs/data/main.py
Normal file
1
romanovpv/task 2/docs/data/main.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
import model.py
|
||||
76
romanovpv/task 2/docs/data/model.py
Normal file
76
romanovpv/task 2/docs/data/model.py
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
class Cell:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
x: int,
|
||||
y: int,
|
||||
is_wall=False,
|
||||
is_start=False,
|
||||
is_exit=False
|
||||
):
|
||||
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
self.isWall = is_wall
|
||||
self.isStart = is_start
|
||||
self.isExit = is_exit
|
||||
|
||||
def isPassable(self):
|
||||
return not self.isWall
|
||||
|
||||
def __repr__(self):
|
||||
return f"Cell({self.x},{self.y})"
|
||||
|
||||
def __eq__(self, other):
|
||||
return (
|
||||
isinstance(other, Cell)
|
||||
and self.x == other.x
|
||||
and self.y == other.y
|
||||
)
|
||||
|
||||
def __hash__(self):
|
||||
return hash((self.x, self.y))
|
||||
|
||||
class Maze:
|
||||
|
||||
def __init__(self, width: int, height: int):
|
||||
self.width = width
|
||||
self.height = height
|
||||
|
||||
self.cells = []
|
||||
|
||||
self.start = None
|
||||
self.exit = None
|
||||
|
||||
def add_row(self, row):
|
||||
self.cells.append(row)
|
||||
for cell in row:
|
||||
if cell.isStart:
|
||||
self.start = cell
|
||||
if cell.isExit:
|
||||
self.exit = cell
|
||||
|
||||
def getCell(self, x, y):
|
||||
if 0 <= x < self.height and 0 <= y < self.width:
|
||||
return self.cells[x][y]
|
||||
return None
|
||||
|
||||
def getNeighbors(self, cell):
|
||||
directions = [
|
||||
(-1, 0), # вверх
|
||||
(1, 0), # вниз
|
||||
(0, -1), # влево
|
||||
(0, 1) # вправо
|
||||
]
|
||||
neighbors = []
|
||||
for dx, dy in directions:
|
||||
nx = cell.x + dx
|
||||
ny = cell.y + dy
|
||||
neighbor = self.getCell(nx, ny)
|
||||
if (
|
||||
neighbor
|
||||
and neighbor.isPassable()
|
||||
):
|
||||
neighbors.append(neighbor)
|
||||
return neighbors
|
||||
Loading…
Reference in New Issue
Block a user