Добавленны заготовки для алгоритмов

This commit is contained in:
GordStep 2026-05-20 21:42:45 +03:00
parent c9448652cf
commit e5493a5439
9 changed files with 113 additions and 14 deletions

View File

@ -12,8 +12,7 @@ class MazeBuilder(ABC):
class TextFileMazeBuilder(MazeBuilder):
def buildFromFile(self, filename: str) -> Maze:
"""Получает лабиринт из текстового файла"""
with open(filename) as f:
data = f.read().splitlines()
x, y = 0, 0

View File

@ -1,11 +1,38 @@
class Cell:
"""
Клетка лабиринта
def __init__(self, x, y, isWall=False, isStart=False, isExit=False):
`x, y` - координаты клетки в лабиринте
`isWall` - Является ли клетка стеной
`isStart` - Является ли клетка стартом
`isExit` - Является ли клетка выходом лабиринта
`value` - Вес клетки
"""
def __init__(self, x: int, y: int, isWall=False, isStart=False, isExit=False, value=1):
"""
Создание клетки лабиринта
`x, y` - координаты клетки в лабиринте
`isWall` - Является ли клетка стеной
`isStart` - Является ли клетка стартом
`isExit` - Является ли клетка выходом лабиринта
`value` - Вес клетки
"""
self.__x = x
self.__y = y
self.isWall = isWall
self.isStart = isStart
self.isExit = isExit
self.valur = value
pass
@property
@ -28,6 +55,19 @@ class Cell:
return self.__x, self.__y
def toStr(self):
"""
Возвращает строчкое представление клетки
`#` - Стена
`S` - Начало лабиринта
`E` - Конец лабиринта
` `(пробел) - свободный проход
`<int>` - Вес клетки
"""
if self.isWall:
return '#'
elif self.isStart:
@ -35,4 +75,9 @@ class Cell:
elif self.isExit:
return 'E'
else:
return ' '
return ' '
@property
def value(self) -> int:
"""Возвращает вес клетки"""
return self.value

View File

@ -1,6 +1,7 @@
from source.classes.cell import Cell
class Maze:
"""Лабиринт"""
def __init__(self, cells, width, height, start, exit):
self.cells = cells
self.width = width
@ -13,6 +14,7 @@ class Maze:
return self.cells[x][y]
def getNeighbors(self, cell) -> list[Cell]:
"""Возвращает список соседних проходимых клеток (вверх, вниз, влево, вправо, если в пределах границ и не стена)."""
neighbors = []
c_x, c_y = cell.getXY()
@ -32,6 +34,7 @@ class Maze:
return neighbors
def printer(self):
"""Выводит в консоль лабиринт (отладочное)"""
for line in self.cells:
for c in line:
print(c.toStr(), end='')

View File

@ -0,0 +1,10 @@
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

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

View File

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

View File

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

View File

@ -0,0 +1,14 @@
from abc import ABC, abstractmethod
from source.classes.cell import Cell
from source.classes.maze import Maze
class PathFindingStrategy(ABC):
"""Интерфейс для семейства алгоритмов поиска пути от старта до выхода."""
@abstractmethod
def findPath(self, maze: Maze, start: Cell, exit: Cell):
"""Возвращающим список клеток пути (от старта до выхода включительно) или пустой список, если пути нет."""
pass

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "4489fc7e",
"metadata": {},
"outputs": [
@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "fde1eddb",
"metadata": {},
"outputs": [
@ -36,14 +36,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"S#####\n",
" # # #\n",
" # #\n",
"## #\n",
" # #\n",
"#### #\n",
"# #\n",
"#E####\n"
"S # ###\n",
"## # # E\n",
"# # #\n",
"### ## #\n",
"# #\n",
"########\n"
]
}
],