diff --git a/ProninVV/task-2-oop/BreadthFirstSearch.py b/ProninVV/task-2-oop/BreadthFirstSearch.py new file mode 100644 index 0000000..6837a21 --- /dev/null +++ b/ProninVV/task-2-oop/BreadthFirstSearch.py @@ -0,0 +1,31 @@ +from strategy import PathFindingStrategy +from Maze import Maze, Cell + + +class BFSStrategy(PathFindingStrategy): + def findPath(maze: Maze, start: Cell, exit: Cell): + + if not start or not exit: + return [] + # очерель: перывй вошел - первый вышел + queue = [start] + # будем хранить откуда в какую клетку пришли + parents = {start: None} + + while (len(queue) != 0): + u = queue.pop(0) + if u == exit: + path = [] + current = exit + while current is not None: + path.append(current) + current = parents[current] + path.reverse() + return path + + childs = maze.getNeighbors(u) + for child in childs: + if child not in parents: + parents[child] = u + queue.append(child) + return [] diff --git a/ProninVV/task-2-oop/labirint.py b/ProninVV/task-2-oop/Maze.py similarity index 95% rename from ProninVV/task-2-oop/labirint.py rename to ProninVV/task-2-oop/Maze.py index f5fd69c..6140d80 100644 --- a/ProninVV/task-2-oop/labirint.py +++ b/ProninVV/task-2-oop/Maze.py @@ -25,7 +25,7 @@ class Maze: self.exit = exit def getCell(self, x, y) -> Cell: - if 0 <= x <= self.width and 0 <= y <= self.height: + if (0 <= x < self.width) and (0 <= y < self.height): return self.__grid[y][x] return None diff --git a/ProninVV/task-2-oop/MazeBuilder.py b/ProninVV/task-2-oop/MazeBuilder.py index 7aa4195..cff52e9 100644 --- a/ProninVV/task-2-oop/MazeBuilder.py +++ b/ProninVV/task-2-oop/MazeBuilder.py @@ -1,5 +1,5 @@ from abc import ABC, abstractmethod -from labirint import Maze, Cell +from Maze import Maze, Cell class MazeBuilder(ABC): @@ -33,14 +33,11 @@ class TextFileMazeBuilder(MazeBuilder): elif char == 'E': cell.isExit = True self._maze.exit = cell + self._validate() return self._maze - def validate(self): + def _validate(self): if self._maze.start is None: raise "в лабиринте нет старта" if self._maze.exit is None: raise "в лабиринте нет начала" - - -# labir = TextFileMazeBuilder().buildFromFile('text.txt') -# print(labir) diff --git a/ProninVV/task-2-oop/main.py b/ProninVV/task-2-oop/main.py index 8b13789..77589db 100644 --- a/ProninVV/task-2-oop/main.py +++ b/ProninVV/task-2-oop/main.py @@ -1 +1,7 @@ +from MazeBuilder import TextFileMazeBuilder +from BreadthFirstSearch import BFSStrategy + +maze1 = TextFileMazeBuilder().buildFromFile("text.txt") +pathh = BFSStrategy.findPath(maze1, maze1.start, maze1.exit) +print(pathh) diff --git a/ProninVV/task-2-oop/strategy.py b/ProninVV/task-2-oop/strategy.py new file mode 100644 index 0000000..ffb6613 --- /dev/null +++ b/ProninVV/task-2-oop/strategy.py @@ -0,0 +1,12 @@ +from abc import ABC, abstractmethod +from typing import List +from Maze import Maze, Cell + +# интерфейс стратегий + + +class PathFindingStrategy(ABC): + @abstractmethod + def findPath(maze: Maze, start, exit) -> List[Cell]: + """ возвращает список клеток пути (от старта до выхода включительно) или пустой список, если пути нет """ + pass