107 lines
3.5 KiB
Python
107 lines
3.5 KiB
Python
#!/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_and_generate_path():
|
|
"""测试加载样例文件并生成路径的功能"""
|
|
print("=== 测试加载样例文件并生成路径 ===\n")
|
|
|
|
# 创建迷宫实例
|
|
maze = Maze(wall_size=20, maze_size=400, file_name="test.csv")
|
|
|
|
# 测试加载sample.json
|
|
sample_file = "saves/sample.json"
|
|
print(f"1. 尝试加载 {sample_file}...")
|
|
|
|
if os.path.exists(sample_file):
|
|
print(f"文件存在,开始加载...")
|
|
|
|
# 加载文件
|
|
if maze.load_game(sample_file):
|
|
print("✓ 加载成功")
|
|
print(f"迷宫大小: {maze.size}x{maze.size}")
|
|
|
|
# 重新生成路径
|
|
print("\n2. 重新生成路径...")
|
|
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"路径长度: {len(maze.full_path)}")
|
|
print(f"路径前10步: {maze.full_path[:10]}")
|
|
|
|
# 显示迷宫的起点和终点
|
|
start_pos = None
|
|
end_pos = None
|
|
for y in range(maze.size):
|
|
for x in range(maze.size):
|
|
if maze.generater.maze[y][x] == 's':
|
|
start_pos = (y, x)
|
|
elif maze.generater.maze[y][x] == 'e':
|
|
end_pos = (y, x)
|
|
|
|
print(f"起点位置: {start_pos}")
|
|
print(f"终点位置: {end_pos}")
|
|
|
|
if maze.full_path:
|
|
print(f"路径起点: {maze.full_path[0]}")
|
|
print(f"路径终点: {maze.full_path[-1]}")
|
|
|
|
return True
|
|
else:
|
|
print("✗ 迷宫数据无效")
|
|
return False
|
|
else:
|
|
print("✗ 加载失败")
|
|
return False
|
|
else:
|
|
print(f"✗ 文件不存在: {sample_file}")
|
|
return False
|
|
|
|
def test_save_json():
|
|
"""测试保存JSON功能"""
|
|
print("\n=== 测试保存JSON功能 ===\n")
|
|
|
|
# 创建并生成新迷宫
|
|
maze = Maze(wall_size=20, maze_size=400, file_name="test.csv")
|
|
maze.generate()
|
|
|
|
print(f"生成迷宫大小: {maze.size}x{maze.size}")
|
|
print(f"路径长度: {len(maze.full_path)}")
|
|
|
|
# 保存为JSON
|
|
result = maze.save_game("test_button_save", "json")
|
|
if result:
|
|
print(f"✓ JSON保存成功: {result}")
|
|
return True
|
|
else:
|
|
print("✗ JSON保存失败")
|
|
return False
|
|
|
|
if __name__ == "__main__":
|
|
success1 = test_load_and_generate_path()
|
|
success2 = test_save_json()
|
|
|
|
print(f"\n=== 测试结果 ===")
|
|
print(f"加载并生成路径: {'成功' if success1 else '失败'}")
|
|
print(f"保存JSON: {'成功' if success2 else '失败'}")
|
|
|
|
if success1 and success2:
|
|
print("✓ 所有测试通过!新按钮功能正常工作。")
|
|
else:
|
|
print("✗ 部分测试失败,需要检查。")
|