2026-05-20 20:07:51 +00:00
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-05-21 21:05:50 +00:00
|
|
|
|
from source.strategy import PathFindingStrategy
|
|
|
|
|
|
from source.observer import Observer, Event
|
|
|
|
|
|
from source.classes import Cell, Maze
|
|
|
|
|
|
|
2026-05-20 20:07:51 +00:00
|
|
|
|
|
|
|
|
|
|
class MazeSolver:
|
2026-05-20 20:16:07 +00:00
|
|
|
|
def __init__(self, maze: Maze, strategy: PathFindingStrategy, observer: Observer):
|
2026-05-20 20:07:51 +00:00
|
|
|
|
self.maze = maze
|
|
|
|
|
|
self.strategy = strategy
|
2026-05-20 20:16:07 +00:00
|
|
|
|
self.observer = observer
|
2026-05-20 20:07:51 +00:00
|
|
|
|
|
|
|
|
|
|
def strategyName(self):
|
|
|
|
|
|
return self.strategy.name
|
|
|
|
|
|
|
|
|
|
|
|
def setStrategy(self, strategy: PathFindingStrategy):
|
|
|
|
|
|
self.strategy = strategy
|
|
|
|
|
|
|
|
|
|
|
|
def solve(self):
|
|
|
|
|
|
start_time = time.perf_counter()
|
|
|
|
|
|
path, visited_cells = self.strategy.findPath(self.maze)
|
|
|
|
|
|
finish_time = time.perf_counter()
|
|
|
|
|
|
|
2026-05-20 20:16:07 +00:00
|
|
|
|
self.observer.update(Event(
|
|
|
|
|
|
event="path_found",
|
2026-05-21 20:31:17 +00:00
|
|
|
|
maze=self.maze,
|
2026-05-20 20:16:07 +00:00
|
|
|
|
player_position=self.maze.exit,
|
|
|
|
|
|
path=path
|
|
|
|
|
|
))
|
|
|
|
|
|
|
2026-05-20 20:07:51 +00:00
|
|
|
|
return SearchStats(
|
|
|
|
|
|
timeMs=finish_time - start_time,
|
|
|
|
|
|
visitedCells=visited_cells,
|
2026-05-21 19:06:22 +00:00
|
|
|
|
pathLength=len(path),
|
|
|
|
|
|
path=path
|
2026-05-20 20:07:51 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SearchStats:
|
|
|
|
|
|
"""Общая информация о тесте алгоритма"""
|
2026-05-21 19:06:22 +00:00
|
|
|
|
def __init__(self, timeMs: float, visitedCells: int, pathLength: int, path: list[Cell]):
|
2026-05-20 20:07:51 +00:00
|
|
|
|
self.timeMs = timeMs
|
|
|
|
|
|
self.visitedCells = visitedCells
|
2026-05-21 19:06:22 +00:00
|
|
|
|
self.pathLength = pathLength
|
|
|
|
|
|
self.path = path
|