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

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

View File

@ -1,11 +1,38 @@
class Cell: 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.__x = x
self.__y = y self.__y = y
self.isWall = isWall self.isWall = isWall
self.isStart = isStart self.isStart = isStart
self.isExit = isExit self.isExit = isExit
self.valur = value
pass pass
@property @property
@ -28,6 +55,19 @@ class Cell:
return self.__x, self.__y return self.__x, self.__y
def toStr(self): def toStr(self):
"""
Возвращает строчкое представление клетки
`#` - Стена
`S` - Начало лабиринта
`E` - Конец лабиринта
` `(пробел) - свободный проход
`<int>` - Вес клетки
"""
if self.isWall: if self.isWall:
return '#' return '#'
elif self.isStart: elif self.isStart:
@ -36,3 +76,8 @@ class Cell:
return 'E' return 'E'
else: else:
return ' ' return ' '
@property
def value(self) -> int:
"""Возвращает вес клетки"""
return self.value

View File

@ -1,6 +1,7 @@
from source.classes.cell import Cell from source.classes.cell import Cell
class Maze: class Maze:
"""Лабиринт"""
def __init__(self, cells, width, height, start, exit): def __init__(self, cells, width, height, start, exit):
self.cells = cells self.cells = cells
self.width = width self.width = width
@ -13,6 +14,7 @@ class Maze:
return self.cells[x][y] return self.cells[x][y]
def getNeighbors(self, cell) -> list[Cell]: def getNeighbors(self, cell) -> list[Cell]:
"""Возвращает список соседних проходимых клеток (вверх, вниз, влево, вправо, если в пределах границ и не стена)."""
neighbors = [] neighbors = []
c_x, c_y = cell.getXY() c_x, c_y = cell.getXY()
@ -32,6 +34,7 @@ class Maze:
return neighbors return neighbors
def printer(self): def printer(self):
"""Выводит в консоль лабиринт (отладочное)"""
for line in self.cells: for line in self.cells:
for c in line: for c in line:
print(c.toStr(), end='') 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": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"id": "4489fc7e", "id": "4489fc7e",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -28,7 +28,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"id": "fde1eddb", "id": "fde1eddb",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -36,14 +36,12 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"S#####\n", "S # ###\n",
" # # #\n", "## # # E\n",
" # #\n", "# # #\n",
"## #\n", "### ## #\n",
" # #\n", "# #\n",
"#### #\n", "########\n"
"# #\n",
"#E####\n"
] ]
} }
], ],