2026-rff_mp/ivantsovma/maze/test_strategy.py

117 lines
3.7 KiB
Python
Raw Normal View History

import sys
import os
# Добавляем корневую папку в путь поиска
sys.path.insert(0, r'C:\ivantsovma\docs\MazeProject')
from builders import TextFileMazeBuilder
from strategies import BFSStrategy, DFSStrategy, AStarStrategy
def test_strategies():
print("ТЕСТИРОВАНИЕ STRATEGY ПАТТЕРНА")
# Загружаем лабиринт
builder = TextFileMazeBuilder()
maze = builder.build_from_file("mazes/small_maze.txt")
print(f"\nЛабиринт: {maze.width}x{maze.height}")
print(f"Старт: ({maze.start.x}, {maze.start.y})")
print(f"Выход: ({maze.exit.x}, {maze.exit.y})")
# Создаем стратегии
strategies = [
BFSStrategy(),
DFSStrategy(),
AStarStrategy()
]
print("РЕЗУЛЬТАТЫ ПОИСКА ПУТИ")
for strategy in strategies:
print(f"\n--- {strategy.name} ---")
# Ищем путь
path = strategy.find_path(maze, maze.start, maze.exit)
if path:
print(f"Путь найден!")
print(f"Посещено клеток: {strategy.visited_count}")
print(f"Длина пути: {len(path)} шагов")
print(f"Путь: ", end="")
for i, cell in enumerate(path[:5]):
print(f"({cell.x},{cell.y})", end="")
if i < len(path[:5]) - 1:
print("", end="")
if len(path) > 5:
print(f" ... → ({path[-1].x},{path[-1].y})")
else:
print()
else:
print(f"Путь не найден!")
# Визуализация
print("ВИЗУАЛИЗАЦИЯ ЛАБИРИНТА С ПУТЕМ (BFS)")
# Находим путь BFS
bfs = BFSStrategy()
path = bfs.find_path(maze, maze.start, maze.exit)
path_set = set(path)
print("\n " + " " * 4 + "0 1 2 3 4 5 6")
for y in range(maze.height):
line = f" {y}"
for x in range(maze.width):
cell = maze.get_cell(x, y)
if cell in path_set and cell != maze.start and cell != maze.exit:
line += ""
elif cell == maze.start:
line += "S "
elif cell == maze.exit:
line += "E "
elif cell.is_wall:
line += "# "
else:
line += "· "
print(line)
print("\n Условные обозначения:")
print(" # - стена")
print(" · - проход")
print(" ● - путь")
print(" S - старт")
print(" E - выход")
def test_simple_maze():
print("ТЕСТ НА ПРОСТОМ ЛАБИРИНТЕ")
builder = TextFileMazeBuilder()
maze = builder.build_from_file("mazes/simple_maze.txt")
print(f"\nЛабиринт 5x3:")
for y in range(maze.height):
line = ""
for x in range(maze.width):
cell = maze.get_cell(x, y)
if cell.is_wall:
line += "#"
elif cell.is_start:
line += "S"
elif cell.is_exit:
line += "E"
else:
line += " "
print(f" {line}")
strategies = [BFSStrategy(), DFSStrategy(), AStarStrategy()]
for strategy in strategies:
path = strategy.find_path(maze, maze.start, maze.exit)
if path:
print(f"\n {strategy.name}: путь найден за {len(path)} шагов")
else:
print(f"\n {strategy.name}: путь НЕ найден")
if __name__ == "__main__":
test_strategies()
test_simple_maze()
print("ТЕСТИРОВАНИЕ ЗАВЕРШЕНО")