Прописал init для всех модулей

This commit is contained in:
GordStep 2026-05-22 00:05:50 +03:00
parent c8694aa053
commit 06f8192f6a
17 changed files with 122 additions and 172 deletions

View File

@ -0,0 +1,4 @@
from .builder import MazeBuilder
from .text_file_maze_builder import TextFileMazeBuilder
__all__ = ['MazeBuilder', 'TextFileMazeBuilder']

View File

@ -2,61 +2,8 @@ from abc import ABC, abstractmethod
from source.classes.maze import Maze
from source.classes.cell import Cell
class MazeBuilder(ABC):
@abstractmethod
def buildFromFile(self, filename: str) -> Maze:
pass
class TextFileMazeBuilder(MazeBuilder):
def buildFromFile(self, filename: str) -> Maze:
"""Получает лабиринт из текстового файла"""
with open(filename) as f:
data = f.read().splitlines()
x, y = 0, 0
width = len(data[0])
height = len(data)
cells = [[None] * width for _ in range(height)]
start, c_exit = None, None
for line in data:
x = 0
for c in line.strip():
if c == 'S':
cells[y][x] = Cell(x, y, isStart=True)
start = cells[y][x]
x += 1
elif c == 'E':
cells[y][x] = Cell(x, y, isExit=True)
c_exit = cells[y][x]
x += 1
elif c == '#':
cells[y][x] = Cell(x, y, isWall=True)
x += 1
elif c == ' ':
cells[y][x] = Cell(x, y)
x += 1
else:
print(f'Обнаружен неизвестный символ({c}) в файле лабиринта\nfilename: {filename}\nОн заменён на стену')
cells[y][x] = Cell(x, y, isWall=True)
x += 1
y += 1
if start == None:
raise ValueError(f'В файле лабиринта не обнаружен вход!\nfilename: {filename}')
if c_exit == None:
raise ValueError(f'В файле лабиринта не обнаружен выход!\nfilename: {filename}')
return Maze(
cells=cells,
width=width,
height=height,
start=start,
exit_cell=c_exit
)
pass

View File

@ -0,0 +1,54 @@
from source.classes.maze import Maze, Cell
from .builder import MazeBuilder
class TextFileMazeBuilder(MazeBuilder):
def buildFromFile(self, filename: str) -> Maze:
"""Получает лабиринт из текстового файла"""
with open(filename) as f:
data = f.read().splitlines()
x, y = 0, 0
width = len(data[0])
height = len(data)
cells = [[None] * width for _ in range(height)]
start, c_exit = None, None
for line in data:
x = 0
for c in line.strip():
if c == 'S':
cells[y][x] = Cell(x, y, isStart=True)
start = cells[y][x]
x += 1
elif c == 'E':
cells[y][x] = Cell(x, y, isExit=True)
c_exit = cells[y][x]
x += 1
elif c == '#':
cells[y][x] = Cell(x, y, isWall=True)
x += 1
elif c == ' ':
cells[y][x] = Cell(x, y)
x += 1
else:
print(f'Обнаружен неизвестный символ({c}) в файле лабиринта\nfilename: {filename}\nОн заменён на стену')
cells[y][x] = Cell(x, y, isWall=True)
x += 1
y += 1
if start == None:
raise ValueError(f'В файле лабиринта не обнаружен вход!\nfilename: {filename}')
if c_exit == None:
raise ValueError(f'В файле лабиринта не обнаружен выход!\nfilename: {filename}')
return Maze(
cells=cells,
width=width,
height=height,
start=start,
exit_cell=c_exit
)

View File

@ -0,0 +1,4 @@
from .cell import Cell
from .maze import Maze
__all__ = ['Cell', 'Maze']

View File

@ -1,4 +1,4 @@
from source.classes.cell import Cell
from .cell import Cell
class Maze:
"""Лабиринт"""

View File

@ -0,0 +1,4 @@
from .console_view import ConsoleView
from .observer import Observer, Event
__all__ = ['ConsoleView', 'Observer', 'Event']

View File

@ -1,9 +1,8 @@
import os
from source.observer.observer import Observer, Event
from source.classes.cell import Cell
from source.classes.maze import Maze
from .observer import Observer, Event
from source.classes import Cell, Maze
class ConsoleView(Observer):

View File

@ -1,8 +1,7 @@
from abc import ABC, abstractmethod
# import os
# from source.classes.cell import Cell
from source.classes.maze import Maze
from source.classes import Maze
class Event:

View File

@ -1,10 +0,0 @@
from source.strategy.strategy import PathFindingStrategy
from source.classes.maze import Maze
from source.classes.cell import Cell
class AStrategy(PathFindingStrategy):
def findPath(self, maze: Maze, start: Cell, exit: Cell):
pass
def __A__(self, maze: Maze, start: Cell, exit: Cell):
pass

View File

@ -1,9 +1,8 @@
from collections import deque
from source.strategy.strategy import PathFindingStrategy, reconstruct_path
from source.classes.maze import Maze
from source.classes.cell import Cell
from source.strategy import PathFindingStrategy, reconstruct_path
from source.classes import Maze, Cell
class BFS(PathFindingStrategy):
def name(self):

View File

@ -1,6 +1,5 @@
from source.strategy.strategy import PathFindingStrategy, reconstruct_path
from source.classes.maze import Maze
from source.classes.cell import Cell
from source.strategy import PathFindingStrategy, reconstruct_path
from source.classes import Maze, Cell
class DFS(PathFindingStrategy):
@property

View File

@ -1,6 +1,6 @@
from source.strategy.strategy import PathFindingStrategy
from source.classes.maze import Maze
from source.classes.cell import Cell
from source.strategy import PathFindingStrategy
from source.classes import Maze, Cell
class Dijkstra(PathFindingStrategy):
def findPath(self, maze: Maze, start: Cell, exit: Cell):

View File

@ -0,0 +1,8 @@
from .bfs import BFS
from .dfs import DFS
from .astar import AStar
from .dijkstra import Dijkstra
from .maze_solver import MazeSolver
from .strategy import PathFindingStrategy, reconstruct_path
__all__ = ['BFS', 'DFS', 'AStar', 'Dijkstra', 'MazeSolver', 'PathFindingStrategy', 'reconstruct_path']

View File

@ -0,0 +1,10 @@
from source.strategy import PathFindingStrategy
from source.classes import Maze, Cell
class AStar(PathFindingStrategy):
def findPath(self, maze: Maze, start: Cell, exit: Cell):
pass
def __A__(self, maze: Maze, start: Cell, exit: Cell):
pass

View File

@ -1,10 +1,10 @@
import time
from source.strategy.strategy import PathFindingStrategy
from source.observer.observer import Observer, Event
from source.classes.cell import Cell
from source.classes.maze import Maze
from source.strategy import PathFindingStrategy
from source.observer import Observer, Event
from source.classes import Cell, Maze
class MazeSolver:
def __init__(self, maze: Maze, strategy: PathFindingStrategy, observer: Observer):

View File

@ -1,8 +1,7 @@
from abc import ABC, abstractmethod
from source.classes.cell import Cell
from source.classes.maze import Maze
from source.classes import Cell, Maze
class PathFindingStrategy(ABC):

View File

@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"id": "fde1eddb",
"metadata": {},
"outputs": [
@ -46,7 +46,7 @@
}
],
"source": [
"from source.builder.builder import TextFileMazeBuilder\n",
"from source.builder import TextFileMazeBuilder\n",
"\n",
"builder = TextFileMazeBuilder()\n",
"maze = builder.buildFromFile(filename='test_lab.txt')\n",
@ -89,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"id": "19840429",
"metadata": {},
"outputs": [
@ -97,26 +97,6 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Старт: (0, 0)\n",
"Выход: (7, 1)\n",
"Соседи старта: [(1, 0)]\n",
"для клекти (0, 0) соседи: [(1, 0)]\n",
"для клекти (1, 0) соседи: [(0, 0), (2, 0)]\n",
"для клекти (2, 0) соседи: [(2, 1), (1, 0)]\n",
"для клекти (2, 1) соседи: [(2, 0), (2, 2)]\n",
"для клекти (2, 2) соседи: [(2, 1), (1, 2), (3, 2)]\n",
"для клекти (3, 2) соседи: [(3, 3), (2, 2), (4, 2)]\n",
"для клекти (4, 2) соседи: [(4, 1), (3, 2)]\n",
"для клекти (4, 1) соседи: [(4, 0), (4, 2)]\n",
"для клекти (4, 0) соседи: [(4, 1)]\n",
"для клекти (3, 3) соседи: [(3, 2), (3, 4)]\n",
"для клекти (3, 4) соседи: [(3, 3), (2, 4), (4, 4)]\n",
"для клекти (4, 4) соседи: [(3, 4), (5, 4)]\n",
"для клекти (5, 4) соседи: [(4, 4), (6, 4)]\n",
"для клекти (6, 4) соседи: [(6, 3), (5, 4)]\n",
"для клекти (6, 3) соседи: [(6, 2), (6, 4)]\n",
"для клекти (6, 2) соседи: [(6, 1), (6, 3)]\n",
"для клекти (6, 1) соседи: [(6, 2), (7, 1)]\n",
"Путь найден:\n",
"S**# ###\n",
"##*# #*E\n",
@ -153,9 +133,9 @@
}
],
"source": [
"from source.strategy.DFS import DFS\n",
"from source.strategy.maze_solver import MazeSolver\n",
"from source.classes.cell import Cell\n",
"from source.strategy import DFS, MazeSolver\n",
"# from source.strategy.maze_solver import \n",
"from source.classes import Cell\n",
"\n",
"\n",
"solver = MazeSolver(maze, DFS(), ConsoleView())\n",
@ -171,67 +151,21 @@
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Старт: (0, 0)\n",
"Выход: (7, 1)\n",
"Соседи старта: [(1, 0)]\n",
"для клекти (0, 0) соседи: [(1, 0)]\n",
"для клекти (1, 0) соседи: [(0, 0), (2, 0)]\n",
"для клекти (2, 0) соседи: [(2, 1), (1, 0)]\n",
"для клекти (2, 1) соседи: [(2, 0), (2, 2)]\n",
"для клекти (2, 2) соседи: [(2, 1), (1, 2), (3, 2)]\n",
"для клекти (3, 2) соседи: [(3, 3), (2, 2), (4, 2)]\n",
"для клекти (4, 2) соседи: [(4, 1), (3, 2)]\n",
"для клекти (4, 1) соседи: [(4, 0), (4, 2)]\n",
"для клекти (4, 0) соседи: [(4, 1)]\n",
"для клекти (3, 3) соседи: [(3, 2), (3, 4)]\n",
"для клекти (3, 4) соседи: [(3, 3), (2, 4), (4, 4)]\n",
"для клекти (4, 4) соседи: [(3, 4), (5, 4)]\n",
"для клекти (5, 4) соседи: [(4, 4), (6, 4)]\n",
"для клекти (6, 4) соседи: [(6, 3), (5, 4)]\n",
"для клекти (6, 3) соседи: [(6, 2), (6, 4)]\n",
"для клекти (6, 2) соседи: [(6, 1), (6, 3)]\n",
"для клекти (6, 1) соседи: [(6, 2), (7, 1)]\n",
"Путь найден:\n",
"S**# ###\n",
"##*# #*E\n",
"# ** #*#\n",
"###*##*#\n",
"# ****#\n",
"########\n"
"ename": "TypeError",
"evalue": "'module' object is not callable",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[9], line 6\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01msource\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mstrategy\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mmaze_solver\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m MazeSolver\n\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01msource\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mclasses\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcell\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m Cell\n\u001b[1;32m----> 6\u001b[0m solver \u001b[38;5;241m=\u001b[39m MazeSolver(maze, \u001b[43mBFS\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m, ConsoleView())\n\u001b[0;32m 7\u001b[0m stats \u001b[38;5;241m=\u001b[39m solver\u001b[38;5;241m.\u001b[39msolve()\n\u001b[0;32m 9\u001b[0m [cord\u001b[38;5;241m.\u001b[39mgetXY() \u001b[38;5;28;01mfor\u001b[39;00m cord \u001b[38;5;129;01min\u001b[39;00m maze\u001b[38;5;241m.\u001b[39mgetNeighbors(cell\u001b[38;5;241m=\u001b[39mCell(\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m0\u001b[39m))], [cord\u001b[38;5;241m.\u001b[39mgetXY() \u001b[38;5;28;01mfor\u001b[39;00m cord \u001b[38;5;129;01min\u001b[39;00m stats\u001b[38;5;241m.\u001b[39mpath]\n",
"\u001b[1;31mTypeError\u001b[0m: 'module' object is not callable"
]
},
{
"data": {
"text/plain": [
"([(2, 1), (1, 0)],\n",
" [(0, 0),\n",
" (1, 0),\n",
" (2, 0),\n",
" (2, 1),\n",
" (2, 2),\n",
" (3, 2),\n",
" (3, 3),\n",
" (3, 4),\n",
" (4, 4),\n",
" (5, 4),\n",
" (6, 4),\n",
" (6, 3),\n",
" (6, 2),\n",
" (6, 1),\n",
" (7, 1)])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from source.strategy.BFS import BFS\n",
"from source.strategy.maze_solver import MazeSolver\n",
"from source.classes.cell import Cell\n",
"from source.strategy import BFS, MazeSolver\n",
"# from source.strategy import \n",
"from source.classes import Cell\n",
"\n",
"\n",
"solver = MazeSolver(maze, BFS(), ConsoleView())\n",
@ -242,7 +176,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"id": "857c5c04",
"metadata": {},
"outputs": [
@ -264,7 +198,7 @@
"{'0', '1', '2', '3', '4'}"
]
},
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@ -293,7 +227,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"id": "9a5ea5cb",
"metadata": {},
"outputs": [
@ -379,7 +313,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"id": "32edf4d1",
"metadata": {},
"outputs": [