#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 测试存档界面加载并生成路径功能 """ import sys import os sys.path.append(os.path.dirname(os.path.abspath(__file__))) from maze import Maze from SourceCollector import SourceCollector def test_load_via_save_ui(): """测试通过存档界面加载存档并生成路径""" print("=== 测试存档界面加载功能 ===\n") # 创建迷宫实例 maze = Maze(wall_size=20, maze_size=400, file_name="test.csv") # 模拟存档界面的加载逻辑 print("1. 模拟通过存档界面加载sample.json...") save_file = "saves/sample.json" if os.path.exists(save_file): # 执行加载 if maze.load_game(save_file): print("✓ 存档加载成功") # 模拟存档界面中的路径重新生成逻辑 if maze.generater.maze: maze.source_collector = SourceCollector(maze=maze.generater.maze) maze.source_collector.run() maze.full_path = maze.source_collector.get_path() maze.path_step = 0 maze.is_path_complete = False maze.grid = maze.generater.maze print(f"✓ 路径重新生成成功") print(f"迷宫大小: {maze.size}x{maze.size}") print(f"路径长度: {len(maze.full_path)}") print(f"路径前5步: {maze.full_path[:5]}") print(f"路径后5步: {maze.full_path[-5:]}") # 验证起点和终点 if maze.full_path: start_pos = maze.full_path[0] end_pos = maze.full_path[-1] # 检查起点是否为起点标记 start_cell = maze.generater.maze[start_pos[0]][start_pos[1]] end_cell = maze.generater.maze[end_pos[0]][end_pos[1]] print(f"路径起点: {start_pos}, 网格值: {start_cell}") print(f"路径终点: {end_pos}, 网格值: {end_cell}") if start_cell == 's' and end_cell == 'e': print("✓ 路径起点和终点验证正确") else: print("✗ 路径起点或终点验证失败") return False return True else: print("✗ 迷宫数据无效") return False else: print("✗ 存档加载失败") return False else: print(f"✗ 文件不存在: {save_file}") return False def test_multiple_saves(): """测试加载多个不同的存档文件""" print("\n=== 测试多个存档文件 ===\n") # 获取所有存档文件 saves_dir = "saves" if not os.path.exists(saves_dir): print("saves目录不存在") return False save_files = [f for f in os.listdir(saves_dir) if f.endswith('.json') or f.endswith('.csv')] if not save_files: print("没有找到存档文件") return False print(f"找到{len(save_files)}个存档文件:") for save_file in save_files: print(f"- {save_file}") # 测试加载每个文件 results = [] for save_file in save_files[:3]: # 只测试前3个文件 print(f"\n测试加载: {save_file}") maze = Maze(wall_size=20, maze_size=400, file_name="test.csv") full_path = os.path.join(saves_dir, save_file) if maze.load_game(full_path): # 重新生成路径 if maze.generater.maze: maze.source_collector = SourceCollector(maze=maze.generater.maze) maze.source_collector.run() maze.full_path = maze.source_collector.get_path() print(f" ✓ 加载成功,路径长度: {len(maze.full_path)}") results.append(True) else: print(f" ✗ 迷宫数据无效") results.append(False) else: print(f" ✗ 加载失败") results.append(False) success_count = sum(results) print(f"\n测试结果: {success_count}/{len(results)} 个文件加载成功") return success_count == len(results) if __name__ == "__main__": success1 = test_load_via_save_ui() success2 = test_multiple_saves() print(f"\n=== 总体测试结果 ===") print(f"存档界面加载测试: {'成功' if success1 else '失败'}") print(f"多文件加载测试: {'成功' if success2 else '失败'}") if success1 and success2: print("✓ 所有测试通过!存档界面现在会正确重新生成路径。") else: print("✗ 部分测试失败,需要进一步检查。")