[2]lab2 #306
|
|
@ -330,3 +330,149 @@ def interactive_move_demo(maze, path):
|
|||
cmd.undo()
|
||||
print(f"Отменён последний шаг, позиция: ({player.current_cell.x},{player.current_cell.y})")
|
||||
|
||||
def test_single_maze(filename, strategies, repeats=5):
|
||||
"""Тестирование одного лабиринта с разными стратегиями"""
|
||||
builder = TextFileMazeBuilder()
|
||||
maze = builder.build_from_file(filename)
|
||||
results = []
|
||||
for strategy in strategies:
|
||||
solver = MazeSolver(maze, strategy)
|
||||
times = []
|
||||
visits = []
|
||||
lengths = []
|
||||
for _ in range(repeats):
|
||||
_, stats = solver.solve()
|
||||
times.append(stats.time_ms)
|
||||
visits.append(stats.visited_cells)
|
||||
lengths.append(stats.path_length)
|
||||
results.append({
|
||||
'algorithm': strategy.__class__.__name__,
|
||||
'avg_time_ms': sum(times) / repeats,
|
||||
'avg_visited': sum(visits) / repeats,
|
||||
'avg_path_len': sum(lengths) / repeats
|
||||
})
|
||||
return results
|
||||
|
||||
|
||||
def save_maze_to_file(maze, filename):
|
||||
"""Сохранение лабиринта в файл"""
|
||||
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
||||
with open(filename, 'w', encoding='utf-8') as f:
|
||||
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 += " "
|
||||
f.write(line + "\n")
|
||||
|
||||
|
||||
def create_test_mazes():
|
||||
"""Создание тестовых лабиринтов"""
|
||||
os.makedirs("mazes", exist_ok=True)
|
||||
|
||||
# 1. Простой лабиринт 10x10 (tiny.txt)
|
||||
maze1 = Maze(10, 10)
|
||||
for y in range(10):
|
||||
for x in range(10):
|
||||
is_start = (x == 0 and y == 0)
|
||||
is_exit = (x == 9 and y == 0)
|
||||
is_wall = False
|
||||
if y == 1 and x not in [0, 1, 9]:
|
||||
is_wall = True
|
||||
if y == 2 and x not in [9]:
|
||||
is_wall = True
|
||||
if y == 3 and x not in [0, 9]:
|
||||
is_wall = True
|
||||
if y == 4 and x not in [0, 1, 9]:
|
||||
is_wall = True
|
||||
if y == 5 and x not in [9]:
|
||||
is_wall = True
|
||||
if y == 6 and x not in [0, 9]:
|
||||
is_wall = True
|
||||
if y == 7 and x not in [9]:
|
||||
is_wall = True
|
||||
if y == 8 and x not in [0, 9]:
|
||||
is_wall = True
|
||||
cell = Cell(x, y, is_wall=is_wall)
|
||||
cell.is_start = is_start
|
||||
cell.is_exit = is_exit
|
||||
maze1.cells[y][x] = cell
|
||||
if is_start:
|
||||
maze1.start = cell
|
||||
if is_exit:
|
||||
maze1.exit = cell
|
||||
save_maze_to_file(maze1, "mazes/tiny.txt")
|
||||
|
||||
# 2. Средний лабиринт 15x15 (medium.txt)
|
||||
maze2 = Maze(15, 15)
|
||||
for y in range(15):
|
||||
for x in range(15):
|
||||
is_start = (x == 0 and y == 0)
|
||||
is_exit = (x == 14 and y == 14)
|
||||
is_wall = (x % 3 == 1 and y % 2 == 0) and not is_start and not is_exit
|
||||
cell = Cell(x, y, is_wall=is_wall)
|
||||
cell.is_start = is_start
|
||||
cell.is_exit = is_exit
|
||||
maze2.cells[y][x] = cell
|
||||
if is_start:
|
||||
maze2.start = cell
|
||||
if is_exit:
|
||||
maze2.exit = cell
|
||||
save_maze_to_file(maze2, "mazes/medium.txt")
|
||||
|
||||
# 3. Большой лабиринт 30x30 (large.txt)
|
||||
maze3 = Maze(30, 30)
|
||||
for y in range(30):
|
||||
for x in range(30):
|
||||
is_start = (x == 0 and y == 0)
|
||||
is_exit = (x == 29 and y == 29)
|
||||
is_wall = (x % 2 == 0 and y % 3 == 0) and not is_start and not is_exit
|
||||
cell = Cell(x, y, is_wall=is_wall)
|
||||
cell.is_start = is_start
|
||||
cell.is_exit = is_exit
|
||||
maze3.cells[y][x] = cell
|
||||
if is_start:
|
||||
maze3.start = cell
|
||||
if is_exit:
|
||||
maze3.exit = cell
|
||||
save_maze_to_file(maze3, "mazes/large.txt")
|
||||
|
||||
# 4. Пустой лабиринт 15x15 (empty.txt)
|
||||
maze4 = Maze(15, 15)
|
||||
for y in range(15):
|
||||
for x in range(15):
|
||||
is_start = (x == 0 and y == 0)
|
||||
is_exit = (x == 14 and y == 14)
|
||||
cell = Cell(x, y, is_wall=False)
|
||||
cell.is_start = is_start
|
||||
cell.is_exit = is_exit
|
||||
maze4.cells[y][x] = cell
|
||||
if is_start:
|
||||
maze4.start = cell
|
||||
if is_exit:
|
||||
maze4.exit = cell
|
||||
save_maze_to_file(maze4, "mazes/empty.txt")
|
||||
|
||||
# 5. Лабиринт без выхода 10x10 (no_exit.txt)
|
||||
maze5 = Maze(10, 10)
|
||||
for y in range(10):
|
||||
for x in range(10):
|
||||
is_start = (x == 0 and y == 0)
|
||||
is_exit = (x == 9 and y == 9)
|
||||
is_wall = (x > 0 and y > 0) and not is_start
|
||||
cell = Cell(x, y, is_wall=is_wall)
|
||||
cell.is_start = is_start
|
||||
cell.is_exit = is_exit
|
||||
maze5.cells[y][x] = cell
|
||||
if is_start:
|
||||
maze5.start = cell
|
||||
if is_exit:
|
||||
maze5.exit = cell
|
||||
save_maze_to_file(maze5, "mazes/no_exit.txt")
|
||||
Loading…
Reference in New Issue
Block a user