исправление метода name

This commit is contained in:
GordStep 2026-05-22 22:10:16 +03:00
parent 1224a5afee
commit e54b6c0a7e
4 changed files with 106 additions and 66 deletions

View File

@ -5,6 +5,7 @@ from source.strategy import PathFindingStrategy, reconstruct_path
from source.classes import Maze, Cell from source.classes import Maze, Cell
class BFS(PathFindingStrategy): class BFS(PathFindingStrategy):
@property
def name(self): def name(self):
"""Возвращает название метода""" """Возвращает название метода"""
return "BFS" return "BFS"

View File

@ -3,6 +3,10 @@ from source.classes import Maze, Cell
class AStar(PathFindingStrategy): class AStar(PathFindingStrategy):
@property
def name(self) -> str:
return "A*"
def findPath(self, maze: Maze, start: Cell, exit: Cell): def findPath(self, maze: Maze, start: Cell, exit: Cell):
pass pass

View File

@ -12,7 +12,7 @@ class MazeSolver:
self.strategy = strategy self.strategy = strategy
self.observer = observer self.observer = observer
def strategyName(self): def strategyName(self) -> str:
return self.strategy.name return self.strategy.name
def setStrategy(self, strategy: PathFindingStrategy): def setStrategy(self, strategy: PathFindingStrategy):
@ -46,3 +46,7 @@ class SearchStats:
self.visitedCells = visitedCells self.visitedCells = visitedCells
self.pathLength = pathLength self.pathLength = pathLength
self.path = path self.path = path
def show(self):
"""Вывод информации о тесте в консоль"""
print(f'time: {self.timeMs} ms\nvisited cells: {self.visitedCells}\npath length: {self.pathLength}')

View File

@ -2,14 +2,14 @@
"cells": [ "cells": [
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 1,
"id": "4dbe48b6", "id": "4dbe48b6",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from source.builder import TextFileMazeBuilder\n", "from source.builder import TextFileMazeBuilder\n",
"from source.observer import ConsoleView, Event\n", "from source.observer import ConsoleView, Event\n",
"from source.strategy import MazeSolver, BFS, DFS\n", "from source.strategy import MazeSolver, BFS, DFS, Dijkstra\n",
"# from source.strategy.maze_solver import \n", "# from source.strategy.maze_solver import \n",
"from source.classes import Cell\n", "from source.classes import Cell\n",
"# from source.strategy import " "# from source.strategy import "
@ -17,7 +17,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 2,
"id": "007bf97a", "id": "007bf97a",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -29,7 +29,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 3,
"id": "4489fc7e", "id": "4489fc7e",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -55,7 +55,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 4,
"id": "fde1eddb", "id": "fde1eddb",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -83,7 +83,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 5,
"id": "22325f68", "id": "22325f68",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -116,7 +116,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 6,
"id": "19840429", "id": "19840429",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -154,7 +154,7 @@
" (7, 1)])" " (7, 1)])"
] ]
}, },
"execution_count": 17, "execution_count": 6,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -168,7 +168,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 18, "execution_count": 7,
"id": "73ba37a8", "id": "73ba37a8",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -206,7 +206,7 @@
" (7, 1)])" " (7, 1)])"
] ]
}, },
"execution_count": 18, "execution_count": 7,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -220,7 +220,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": 8,
"id": "857c5c04", "id": "857c5c04",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -229,12 +229,11 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"0\n", "0\n",
"2\n",
"1\n", "1\n",
"4\n", "4\n",
"2\n",
"3\n", "3\n",
"3\n", "3\n"
"2\n"
] ]
}, },
{ {
@ -243,7 +242,7 @@
"{'0', '1', '2', '3', '4'}" "{'0', '1', '2', '3', '4'}"
] ]
}, },
"execution_count": 19, "execution_count": 8,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -272,7 +271,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 20, "execution_count": 9,
"id": "9a5ea5cb", "id": "9a5ea5cb",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -358,7 +357,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 21, "execution_count": 10,
"id": "32edf4d1", "id": "32edf4d1",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -383,7 +382,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 11,
"id": "48d20564", "id": "48d20564",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -391,40 +390,37 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"BFS\n",
"Путь найден:\n", "Путь найден:\n",
"# # # # # # # # # # # # # # # # # # # #\n", "#####################################\n",
"S. # # # #\n", "#S #\n",
"#.# # # # # # # # # # # # #\n", "#. #\n",
"#..... # # # # # # # # #\n", "#. #\n",
"# #.# # # # # # # # # # # #\n", "#. #\n",
"# #............. # # # # #\n", "#. #\n",
"# # # # # # #. # # # # # # #\n", "#. #\n",
"# # # # #..... # # # # #\n", "#. #\n",
"# # # # # # #. # # # # #\n", "#. #\n",
"# # # # #. # # # # #\n", "#..................................E#\n",
"# # # # # # #. # # # # #\n", "#####################################\n",
"# # # # #..... # # # #\n", "time: 0.000567700000374316 ms\n",
"# # # # # # # # #. # # # #\n", "visited cells: 315\n",
"# # # # #. # # #\n", "path length: 43\n"
"# # # # # # # # #. # # # # #\n",
"# # # # # # #. # # # #\n",
"# # # # # # #. # # # #\n",
"# # # # # # #. # # # #\n",
"# # # #...........E #\n",
"# # # # # # # # # # # # # # # # # # # #\n"
] ]
} }
], ],
"source": [ "source": [
"maze2 = builder.buildFromFile(test_lab3)\n", "maze2 = builder.buildFromFile(test_lab2)\n",
"\n", "\n",
"solver = MazeSolver(maze2, BFS(), ConsoleView())\n", "solver = MazeSolver(maze2, BFS(), ConsoleView())\n",
"stats = solver.solve()" "print(solver.strategyName())\n",
"stats = solver.solve()\n",
"stats.show()"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": 12,
"id": "bf13d5ba", "id": "bf13d5ba",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@ -432,41 +428,76 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"DFS\n",
"Путь найден:\n", "Путь найден:\n",
"# # # # # # # # # # # # # # # # # # # #\n", "#####################################\n",
"S.......# # # #\n", "#S..................................#\n",
"# # # .# # # # # # # # # # #\n", "# .#\n",
"#.......# # # # # # # # #\n", "#...................................#\n",
"#. # # # # # # # # # # # # #\n", "#. #\n",
"#...# ...........# # # # #\n", "#...................................#\n",
"# .# # #. # # # .# # # # # # #\n", "# .#\n",
"#...# #...# # .....# # # # #\n", "#...................................#\n",
"#. # # .# # # # .# # # # #\n", "#. #\n",
"#...# #...# #...# # # # #\n", "#..................................E#\n",
"# .# #. # # # #. # # # # #\n", "#####################################\n",
"#...# #..... # #.......# # # #\n", "time: 0.0004403000002639601 ms\n",
"#. # # # #...# # # # .# # # #\n", "visited cells: 315\n",
"#...#.......# .# #...# # #\n", "path length: 179\n"
"# .#.# # .#...# # # #. # # # # #\n",
"#...#. #...#. # # #...# # # #\n",
"#. #...#. #...# # # .# # # #\n",
"#...# .#...# .# # # .# # # #\n",
"# .....# .....# # .........E #\n",
"# # # # # # # # # # # # # # # # # # # #\n"
] ]
} }
], ],
"source": [ "source": [
"maze2 = builder.buildFromFile(test_lab3)\n", "maze2 = builder.buildFromFile(test_lab2)\n",
"\n", "\n",
"solver = MazeSolver(maze2, DFS(), ConsoleView())\n", "solver = MazeSolver(maze2, DFS(), ConsoleView())\n",
"stats = solver.solve()" "print(solver.strategyName())\n",
"stats = solver.solve()\n",
"stats.show()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "9383cb75",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dijkstra\n",
"Путь найден:\n",
"#####################################\n",
"#S #\n",
"#. #\n",
"#. #\n",
"#. #\n",
"#. #\n",
"#. #\n",
"#. #\n",
"#. #\n",
"#..................................E#\n",
"#####################################\n",
"time: 0.0007290000003195019 ms\n",
"visited cells: 315\n",
"path length: 43\n"
]
}
],
"source": [
"maze2 = builder.buildFromFile(test_lab2)\n",
"\n",
"solver = MazeSolver(maze2, Dijkstra(), ConsoleView())\n",
"print(solver.strategyName())\n",
"stats = solver.solve()\n",
"stats.show()"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "9383cb75", "id": "835cff61",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": []