forked from UNN/2026-rff_mp
87 lines
1.7 KiB
Python
87 lines
1.7 KiB
Python
import time
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from maze import Maze
|
|
from strategies import PathFindingStrategy
|
|
|
|
|
|
@dataclass
|
|
class SearchStats:
|
|
time_ms: float
|
|
visited_cells: int
|
|
path_length: int
|
|
|
|
|
|
class MazeSolver:
|
|
|
|
def __init__(
|
|
self,
|
|
maze: Maze,
|
|
strategy: PathFindingStrategy
|
|
):
|
|
|
|
self.maze = maze
|
|
self.strategy = strategy
|
|
|
|
self.observers = []
|
|
|
|
# =====================================
|
|
# Observer
|
|
# =====================================
|
|
|
|
def add_observer(self, observer):
|
|
|
|
self.observers.append(observer)
|
|
|
|
def notify(self, event: str):
|
|
|
|
for observer in self.observers:
|
|
observer.update(event)
|
|
|
|
# =====================================
|
|
# Strategy
|
|
# =====================================
|
|
|
|
def set_strategy(
|
|
self,
|
|
strategy: PathFindingStrategy
|
|
):
|
|
|
|
self.strategy = strategy
|
|
|
|
self.notify(
|
|
f"Strategy changed to {strategy.__class__.__name__}"
|
|
)
|
|
|
|
# =====================================
|
|
# Solve
|
|
# =====================================
|
|
|
|
def solve(self):
|
|
|
|
self.notify("Search started")
|
|
|
|
start_time = time.perf_counter()
|
|
|
|
path, visited_cells = self.strategy.find_path(
|
|
self.maze,
|
|
self.maze.start,
|
|
self.maze.exit
|
|
)
|
|
|
|
end_time = time.perf_counter()
|
|
|
|
stats = SearchStats(
|
|
time_ms=(end_time - start_time) * 1000,
|
|
visited_cells=visited_cells,
|
|
path_length=len(path)
|
|
)
|
|
|
|
if path:
|
|
self.notify("Path found")
|
|
else:
|
|
self.notify("No path found")
|
|
|
|
return path, stats
|