2026-05-20 20:07:51 +00:00
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from source.strategy.strategy import SearchStats, PathFindingStrategy
|
2026-05-20 20:16:07 +00:00
|
|
|
|
from source.bububu.observer import Observer, Event
|
|
|
|
|
|
from source.classes.maze import 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",
|
|
|
|
|
|
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,
|
|
|
|
|
|
pathLength=len(path)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SearchStats:
|
|
|
|
|
|
"""Общая информация о тесте алгоритма"""
|
|
|
|
|
|
def __init__(self, timeMs: float, visitedCells: int, pathLength: int):
|
|
|
|
|
|
self.timeMs = timeMs
|
|
|
|
|
|
self.visitedCells = visitedCells
|
|
|
|
|
|
self.pathLength = pathLength
|