forked from UNN/2026-rff_mp
builder
This commit is contained in:
parent
6d20cba4ae
commit
8c230c900c
70
romanovpv/task 2/docs/data/builders.py
Normal file
70
romanovpv/task 2/docs/data/builders.py
Normal 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
|
||||||
|
|
@ -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)
|
||||||
5
romanovpv/task 2/docs/data/maze.txt
Normal file
5
romanovpv/task 2/docs/data/maze.txt
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
##########
|
||||||
|
#S # #
|
||||||
|
# ### #
|
||||||
|
# ##E #
|
||||||
|
##########
|
||||||
|
|
@ -73,4 +73,17 @@ class Maze:
|
||||||
and neighbor.isPassable()
|
and neighbor.isPassable()
|
||||||
):
|
):
|
||||||
neighbors.append(neighbor)
|
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)
|
||||||
Loading…
Reference in New Issue
Block a user