diff --git a/stepushovgs/labyrinth/source/builder/__init__.py b/stepushovgs/labyrinth/source/builder/__init__.py new file mode 100644 index 0000000..6cbf051 --- /dev/null +++ b/stepushovgs/labyrinth/source/builder/__init__.py @@ -0,0 +1,4 @@ +from .builder import MazeBuilder +from .text_file_maze_builder import TextFileMazeBuilder + +__all__ = ['MazeBuilder', 'TextFileMazeBuilder'] \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/builder/builder.py b/stepushovgs/labyrinth/source/builder/builder.py index d819170..c09a899 100644 --- a/stepushovgs/labyrinth/source/builder/builder.py +++ b/stepushovgs/labyrinth/source/builder/builder.py @@ -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 - ) \ No newline at end of file + pass \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/builder/text_file_maze_builder.py b/stepushovgs/labyrinth/source/builder/text_file_maze_builder.py new file mode 100644 index 0000000..64aedf9 --- /dev/null +++ b/stepushovgs/labyrinth/source/builder/text_file_maze_builder.py @@ -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 + ) \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/classes/__init__.py b/stepushovgs/labyrinth/source/classes/__init__.py new file mode 100644 index 0000000..d838be5 --- /dev/null +++ b/stepushovgs/labyrinth/source/classes/__init__.py @@ -0,0 +1,4 @@ +from .cell import Cell +from .maze import Maze + +__all__ = ['Cell', 'Maze'] \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/classes/maze.py b/stepushovgs/labyrinth/source/classes/maze.py index cffdd47..6c3a4b6 100644 --- a/stepushovgs/labyrinth/source/classes/maze.py +++ b/stepushovgs/labyrinth/source/classes/maze.py @@ -1,4 +1,4 @@ -from source.classes.cell import Cell +from .cell import Cell class Maze: """Лабиринт""" diff --git a/stepushovgs/labyrinth/source/observer/__init__.py b/stepushovgs/labyrinth/source/observer/__init__.py new file mode 100644 index 0000000..9270ee2 --- /dev/null +++ b/stepushovgs/labyrinth/source/observer/__init__.py @@ -0,0 +1,4 @@ +from .console_view import ConsoleView +from .observer import Observer, Event + +__all__ = ['ConsoleView', 'Observer', 'Event'] \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/observer/console_view.py b/stepushovgs/labyrinth/source/observer/console_view.py index 8377f07..b0345c8 100644 --- a/stepushovgs/labyrinth/source/observer/console_view.py +++ b/stepushovgs/labyrinth/source/observer/console_view.py @@ -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): diff --git a/stepushovgs/labyrinth/source/observer/observer.py b/stepushovgs/labyrinth/source/observer/observer.py index 541a481..680de23 100644 --- a/stepushovgs/labyrinth/source/observer/observer.py +++ b/stepushovgs/labyrinth/source/observer/observer.py @@ -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: diff --git a/stepushovgs/labyrinth/source/strategy/A.py b/stepushovgs/labyrinth/source/strategy/A.py deleted file mode 100644 index 7bc933d..0000000 --- a/stepushovgs/labyrinth/source/strategy/A.py +++ /dev/null @@ -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 \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/strategy/BFS.py b/stepushovgs/labyrinth/source/strategy/BFS.py index ffa2c7a..e616e7c 100644 --- a/stepushovgs/labyrinth/source/strategy/BFS.py +++ b/stepushovgs/labyrinth/source/strategy/BFS.py @@ -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): diff --git a/stepushovgs/labyrinth/source/strategy/DFS.py b/stepushovgs/labyrinth/source/strategy/DFS.py index 91a0587..90109c1 100644 --- a/stepushovgs/labyrinth/source/strategy/DFS.py +++ b/stepushovgs/labyrinth/source/strategy/DFS.py @@ -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 diff --git a/stepushovgs/labyrinth/source/strategy/Dijkstra.py b/stepushovgs/labyrinth/source/strategy/Dijkstra.py index ec47c51..6c98518 100644 --- a/stepushovgs/labyrinth/source/strategy/Dijkstra.py +++ b/stepushovgs/labyrinth/source/strategy/Dijkstra.py @@ -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): diff --git a/stepushovgs/labyrinth/source/strategy/__init__.py b/stepushovgs/labyrinth/source/strategy/__init__.py new file mode 100644 index 0000000..5c1d610 --- /dev/null +++ b/stepushovgs/labyrinth/source/strategy/__init__.py @@ -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'] \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/strategy/astar.py b/stepushovgs/labyrinth/source/strategy/astar.py new file mode 100644 index 0000000..84f5552 --- /dev/null +++ b/stepushovgs/labyrinth/source/strategy/astar.py @@ -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 \ No newline at end of file diff --git a/stepushovgs/labyrinth/source/strategy/maze_solver.py b/stepushovgs/labyrinth/source/strategy/maze_solver.py index bb8b48f..694612d 100644 --- a/stepushovgs/labyrinth/source/strategy/maze_solver.py +++ b/stepushovgs/labyrinth/source/strategy/maze_solver.py @@ -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): diff --git a/stepushovgs/labyrinth/source/strategy/strategy.py b/stepushovgs/labyrinth/source/strategy/strategy.py index 836e45a..0b778c4 100644 --- a/stepushovgs/labyrinth/source/strategy/strategy.py +++ b/stepushovgs/labyrinth/source/strategy/strategy.py @@ -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): diff --git a/stepushovgs/labyrinth/test.ipynb b/stepushovgs/labyrinth/test.ipynb index 00b1ef5..92d9373 100644 --- a/stepushovgs/labyrinth/test.ipynb +++ b/stepushovgs/labyrinth/test.ipynb @@ -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": [