maze_python/tests/test_boundary_repair.py

206 lines
6.1 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.

#!/usr/bin/env python3
"""
测试迷宫边界自动修复功能
验证当迷宫四周不是墙时能自动添加边界
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from simple_save_manager import simple_save_manager
from maze import Maze
import json
def test_boundary_repair():
"""测试边界修复功能"""
print("=== 测试迷宫边界自动修复功能 ===")
# 测试加载有问题边界的JSON文件
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}")
# 先查看原始文件内容
with open(test_file, 'r', encoding='utf-8') as f:
original_data = json.load(f)
original_maze = original_data['maze']
print(f"原始迷宫尺寸: {len(original_maze)}x{len(original_maze[0])}")
# 检查原始边界
height = len(original_maze)
width = len(original_maze[0])
print("原始边界检查:")
print(f" 上边界: {original_maze[0]}")
print(f" 下边界: {original_maze[height-1]}")
print(f" 左边界: {[row[0] for row in original_maze]}")
print(f" 右边界: {[row[width-1] for row in original_maze]}")
# 使用simple_save_manager加载
load_data = simple_save_manager.load_maze_from_file(test_file)
if load_data is None:
print("❌ 加载失败")
return False
# 检查修复后的迷宫
repaired_maze = load_data['original_grid']
print(f"修复后迷宫尺寸: {len(repaired_maze)}x{len(repaired_maze[0])}")
# 验证边界是否都是墙
new_height = len(repaired_maze)
new_width = len(repaired_maze[0])
boundary_ok = True
# 检查上下边界
for col in range(new_width):
if repaired_maze[0][col] not in ['#', '1']:
print(f"❌ 上边界第{col}列不是墙: {repaired_maze[0][col]}")
boundary_ok = False
if repaired_maze[new_height-1][col] not in ['#', '1']:
print(f"❌ 下边界第{col}列不是墙: {repaired_maze[new_height-1][col]}")
boundary_ok = False
# 检查左右边界
for row in range(new_height):
if repaired_maze[row][0] not in ['#', '1']:
print(f"❌ 左边界第{row}行不是墙: {repaired_maze[row][0]}")
boundary_ok = False
if repaired_maze[row][new_width-1] not in ['#', '1']:
print(f"❌ 右边界第{row}行不是墙: {repaired_maze[row][new_width-1]}")
boundary_ok = False
if boundary_ok:
print("✅ 边界修复成功,所有边界都是墙壁")
else:
print("❌ 边界修复失败")
return False
return True
def test_maze_loading_with_repair():
"""测试在Maze类中加载带有边界问题的迷宫"""
print("\n=== 测试Maze类加载边界修复 ===")
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
# 加载迷宫
result = maze.load_game(test_file)
if not result:
print("❌ Maze加载失败")
return False
# 检查加载后的迷宫是否正常
if len(maze.grid) == 0:
print("❌ 加载后迷宫网格为空")
return False
# 验证边界
height = len(maze.grid)
width = len(maze.grid[0])
print(f"Maze加载后尺寸: {width}x{height}")
# 简单检查四个角是否是墙
corners = [
maze.grid[0][0], maze.grid[0][width-1],
maze.grid[height-1][0], maze.grid[height-1][width-1]
]
if all(corner in ['#', '1'] for corner in corners):
print("✅ Maze加载成功边界正常")
return True
else:
print(f"❌ Maze加载后边界有问题四个角: {corners}")
return False
def test_create_test_file_without_boundary():
"""创建一个没有边界的测试文件"""
print("\n=== 创建测试用的无边界迷宫文件 ===")
# 创建一个中间没有边界的迷宫
test_maze = [
[" ", "S", " ", " ", " "],
[" ", "#", " ", "#", " "],
[" ", " ", " ", "#", " "],
[" ", "#", " ", " ", " "],
[" ", " ", " ", "E", " "]
]
test_data = {
"maze": test_maze,
"metadata": {
"name": "无边界测试迷宫",
"test": True
}
}
test_file = "saves/test_no_boundary.json"
with open(test_file, 'w', encoding='utf-8') as f:
json.dump(test_data, f, indent=2, ensure_ascii=False)
print(f"创建测试文件: {test_file}")
# 测试加载这个文件
load_data = simple_save_manager.load_maze_from_file(test_file)
if load_data is None:
print("❌ 加载自创建的测试文件失败")
return False
repaired_maze = load_data['original_grid']
print(f"原始: 5x5 -> 修复后: {len(repaired_maze[0])}x{len(repaired_maze)}")
# 打印修复后的迷宫
print("修复后的迷宫:")
for row in repaired_maze:
print(''.join(row))
return True
def main():
print("开始测试迷宫边界自动修复功能...")
tests = [
test_boundary_repair,
test_maze_loading_with_repair,
test_create_test_file_without_boundary
]
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)}")
import traceback
traceback.print_exc()
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)