forked from UNN/2026-rff_mp
написана сборка лабиринта из файла
This commit is contained in:
parent
d396e7391b
commit
c24592d4e2
46
ProninVV/task-2-oop/MazeBuilder.py
Normal file
46
ProninVV/task-2-oop/MazeBuilder.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
from abc import ABC, abstractmethod
|
||||
from labirint import Maze, Cell
|
||||
|
||||
|
||||
class MazeBuilder(ABC):
|
||||
@abstractmethod
|
||||
def buildFromFile(self, filename):
|
||||
pass
|
||||
|
||||
|
||||
class TextFileMazeBuilder(MazeBuilder):
|
||||
def __init__(self):
|
||||
self._maze = None
|
||||
|
||||
def buildFromFile(self, filename: str):
|
||||
|
||||
with open(filename, mode='r', encoding='utf-8') as file:
|
||||
lines = file.read().splitlines()
|
||||
|
||||
height = len(lines)
|
||||
width = len(lines[0])
|
||||
self._maze = Maze(height, width)
|
||||
|
||||
for y, line in enumerate(lines):
|
||||
for x, char in enumerate(line):
|
||||
cell = self._maze.getCell(x, y)
|
||||
|
||||
if char == '#':
|
||||
cell.isWall = True
|
||||
elif char == 'S':
|
||||
cell.isStart = True
|
||||
self._maze.start = cell
|
||||
elif char == 'E':
|
||||
cell.isExit = True
|
||||
self._maze.exit = cell
|
||||
return self._maze
|
||||
|
||||
def validate(self):
|
||||
if self._maze.start is None:
|
||||
raise "в лабиринте нет старта"
|
||||
if self._maze.exit is None:
|
||||
raise "в лабиринте нет начала"
|
||||
|
||||
|
||||
# labir = TextFileMazeBuilder().buildFromFile('text.txt')
|
||||
# print(labir)
|
||||
49
ProninVV/task-2-oop/labirint.py
Normal file
49
ProninVV/task-2-oop/labirint.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
# модель клетки лабиринта
|
||||
|
||||
class Cell:
|
||||
def __init__(self, x, y, isWall=False, isStart=False, isExit=False):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.isWall = isWall
|
||||
self.isStart = isStart
|
||||
self.isExit = isExit
|
||||
|
||||
def isPassable(self):
|
||||
return not self.isWall
|
||||
|
||||
|
||||
# модель лабиринта
|
||||
|
||||
class Maze:
|
||||
|
||||
def __init__(self, height, width, start=None, exit=None):
|
||||
self.height = height # строки
|
||||
self.width = width # столбцы
|
||||
self.__grid = [[Cell(x, y) for x in range(width)]
|
||||
for y in range(height)]
|
||||
self.start = start
|
||||
self.exit = exit
|
||||
|
||||
def getCell(self, x, y) -> Cell:
|
||||
if 0 <= x <= self.width and 0 <= y <= self.height:
|
||||
return self.__grid[y][x]
|
||||
return None
|
||||
|
||||
def getNeighbors(self, cell):
|
||||
dirs = {'left': (-1, 0), 'right': (1, 0),
|
||||
'up': (0, 1), 'down': (0, -1)}
|
||||
neighbors = []
|
||||
for _, val in dirs.items():
|
||||
dx, dy = val
|
||||
nx, ny = cell.x + dx, cell.y + dy
|
||||
neighbor = self.getCell(nx, ny)
|
||||
|
||||
if neighbor and isinstance(neighbor, Cell) and neighbor.isPassable():
|
||||
neighbors.append(neighbor)
|
||||
return neighbors
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
maze1 = Maze(height=5, width=5, start=0, exit=4)
|
||||
cell1 = maze1.getCell(2, 2)
|
||||
print(maze1.getNeighbors(cell1))
|
||||
1
ProninVV/task-2-oop/main.py
Normal file
1
ProninVV/task-2-oop/main.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
|
||||
Loading…
Reference in New Issue
Block a user