#!/usr/bin/env python3 """ 测试边界修复后的路径生成功能 验证修复边界后SourceCollector能正常工作 """ import sys import os sys.path.append(os.path.dirname(os.path.abspath(__file__))) from maze import Maze from SourceCollector import SourceCollector import traceback def test_source_collector_with_boundary_fix(): """测试边界修复后SourceCollector的路径生成""" print("=== 测试边界修复后路径生成 ===") maze = Maze(wall_size=30, maze_size=600, file_name="test.csv") # 加载有边界问题的文件 test_file = "saves/maze_15_15_2.json" if not os.path.exists(test_file): print(f"❌ 测试文件 {test_file} 不存在") return False print(f"加载测试文件: {test_file}") try: # 使用maze.load_game加载(会自动修复边界) result = maze.load_game(test_file) if not result: print("❌ 迷宫加载失败") return False print(f"✅ 迷宫加载成功") print(f"修复后迷宫尺寸: {len(maze.generater.maze)}x{len(maze.generater.maze[0])}") print(f"显示网格尺寸: {len(maze.grid)}x{len(maze.grid[0])}") # 检查是否有路径信息 if len(maze.full_path) > 0: print(f"已有路径信息,长度: {len(maze.full_path)}") return True # 如果没有路径信息,尝试生成 print("没有路径信息,尝试重新生成...") # 使用修复后的迷宫创建SourceCollector source_collector = SourceCollector(maze=maze.generater.maze) print(f"SourceCollector创建成功,迷宫尺寸: {source_collector.rowNums}x{source_collector.colNums}") # 检查起点和终点 if source_collector.start_pos is None: print("❌ 没有找到起点") return False if source_collector.end_pos is None: print("❌ 没有找到终点") return False print(f"起点: {source_collector.start_pos}") print(f"终点: {source_collector.end_pos}") # 尝试运行路径搜索 source_collector.run() path = source_collector.get_path() if path and len(path) > 0: print(f"✅ 路径生成成功,长度: {len(path)}") print(f"起始几步: {path[:5] if len(path) >= 5 else path}") return True else: print("❌ 路径生成失败,没有找到路径") return False except Exception as e: print(f"❌ 测试过程中出现异常: {str(e)}") traceback.print_exc() return False def test_boundary_check_in_source_collector(): """测试SourceCollector的边界检查功能""" print("\n=== 测试SourceCollector边界检查 ===") # 创建一个小的测试迷宫 test_maze = [ ['1', '1', '1', '1', '1'], ['1', 's', '0', '0', '1'], ['1', '0', '1', '0', '1'], ['1', '0', '0', 'e', '1'], ['1', '1', '1', '1', '1'] ] try: source_collector = SourceCollector(maze=test_maze) print(f"测试迷宫尺寸: {source_collector.rowNums}x{source_collector.colNums}") print(f"起点: {source_collector.start_pos}") print(f"终点: {source_collector.end_pos}") # 测试边界检查 test_cases = [ (-1, 0), # 左边界外 (0, -1), # 上边界外 (5, 2), # 下边界外 (2, 5), # 右边界外 (0, 0), # 左上角(边界内) (4, 4), # 右下角(边界内) (2, 2), # 中心位置 ] print("边界检查测试:") for x, y in test_cases: is_out = source_collector.outofmap(x, y) expected = x < 0 or y < 0 or x >= 5 or y >= 5 status = "✅" if is_out == expected else "❌" print(f" {status} ({x},{y}): {'边界外' if is_out else '边界内'}") # 运行路径搜索 source_collector.run() path = source_collector.get_path() if path and len(path) > 0: print(f"✅ 小迷宫路径生成成功,长度: {len(path)}") return True else: print("❌ 小迷宫路径生成失败") return False except Exception as e: print(f"❌ 小迷宫测试异常: {str(e)}") traceback.print_exc() return False def test_direct_source_collector(): """直接测试SourceCollector加载修复后的迷宫""" print("\n=== 直接测试SourceCollector ===") from simple_save_manager import simple_save_manager test_file = "saves/maze_15_15_2.json" if not os.path.exists(test_file): print(f"❌ 测试文件 {test_file} 不存在") return False try: # 直接使用simple_save_manager加载 load_data = simple_save_manager.load_maze_from_file(test_file) if load_data is None: print("❌ 文件加载失败") return False original_grid = load_data['original_grid'] print(f"加载的迷宫尺寸: {len(original_grid)}x{len(original_grid[0])}") # 直接创建SourceCollector source_collector = SourceCollector(maze=original_grid) print(f"SourceCollector尺寸: {source_collector.rowNums}x{source_collector.colNums}") print(f"起点: {source_collector.start_pos}") print(f"终点: {source_collector.end_pos}") # 运行路径搜索 source_collector.run() path = source_collector.get_path() if path and len(path) > 0: print(f"✅ 直接测试路径生成成功,长度: {len(path)}") return True else: print("❌ 直接测试路径生成失败") return False except Exception as e: print(f"❌ 直接测试异常: {str(e)}") traceback.print_exc() return False def main(): print("开始测试边界修复后的路径生成功能...") tests = [ test_boundary_check_in_source_collector, test_direct_source_collector, test_source_collector_with_boundary_fix ] passed = 0 total = len(tests) for test in tests: try: if test(): passed += 1 else: print(f"❌ 测试 {test.__name__} 失败") except Exception as e: print(f"❌ 测试 {test.__name__} 出现异常: {str(e)}") print(f"\n=== 测试结果 ===") print(f"通过: {passed}/{total}") if passed == total: print("🎉 所有路径生成修复测试通过!") return True else: print("❌ 部分测试失败,请检查相关功能") return False if __name__ == "__main__": success = main() sys.exit(0 if success else 1)