This commit is contained in:
Pavel 2026-05-18 00:40:59 +03:00
parent 6d20cba4ae
commit 8c230c900c
4 changed files with 101 additions and 2 deletions

View File

@ -0,0 +1,70 @@
from abc import ABC, abstractmethod
from model import Maze, Cell
class MazeBuilder(ABC):
@abstractmethod
def buildFromFile(self, filename):
pass
class TextFileMazeBuilder(MazeBuilder):
def buildFromFile(self, filename):
with open(
filename,
"r",
encoding="utf-8"
) as file:
lines = [
line.rstrip("\n")
for line in file
]
height = len(lines)
width = len(lines[0])
maze = Maze(
width,
height
)
start_count = 0
exit_count = 0
for x, line in enumerate(lines):
row = []
for y, symbol in enumerate(line):
if symbol == "#":
cell = Cell(
x,
y,
is_wall=True
)
elif symbol == "S":
cell = Cell(
x,
y,
is_start=True
)
start_count += 1
elif symbol == "E":
cell = Cell(
x,
y,
is_exit=True
)
exit_count += 1
elif symbol == " ":
cell = Cell(
x,
y
)
else:
raise ValueError(
f"Неизвестный символ: {symbol}"
)
row.append(cell)
maze.add_row(row)
if start_count != 1:
raise ValueError(
"Должен быть ровно один старт S"
)
if exit_count != 1:
raise ValueError(
"Должен быть ровно один выход E"
)
return maze

View File

@ -1 +1,12 @@
import model.py
import model
from builders import (TextFileMazeBuilder)
builder = TextFileMazeBuilder()
maze = builder.buildFromFile("maze.txt")
maze.printMaze()
print()
print("Старт:")
print(maze.start)
print()
print("Выход:")
print(maze.exit)

View File

@ -0,0 +1,5 @@
##########
#S # #
# ### #
# ##E #
##########

View File

@ -73,4 +73,17 @@ class Maze:
and neighbor.isPassable()
):
neighbors.append(neighbor)
return neighbors
return neighbors
def printMaze(self):
for row in self.cells:
line = ""
for cell in row:
if cell.isStart:
line += "S"
elif cell.isExit:
line += "E"
elif cell.isWall:
line += "#"
else:
line += " "
print(line)