2026-rff_mp/stepushovgs/labyrinth/source/strategy/maze_solver.py

55 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import time
from .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):
self.maze = maze
self.strategy = strategy
self.observer = observer
def strategyName(self) -> str:
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()
self.observer.update(Event(
event="path_found",
maze=self.maze,
player_position=self.maze.exit,
path=path
))
return SearchStats(
timeMs=(finish_time - start_time) * 1000,
visitedCells=visited_cells,
pathLength=len(path),
path=path
)
class SearchStats:
"""Общая информация о тесте алгоритма"""
def __init__(self, timeMs: float, visitedCells: int, pathLength: int, path: list[Cell]):
self.timeMs = timeMs
self.visitedCells = visitedCells
self.pathLength = pathLength
self.path = path
def show(self):
"""Вывод информации о тесте в консоль"""
print(f'time: {self.timeMs} ms\nvisited cells: {self.visitedCells}\npath length: {self.pathLength}')
# def toStr(self) -> str:
# return f'{self.timeMs} {self.visitedCells} {self.pathLength}'