kards-env/tests/unit/test_turn_system.py.needs_f...

167 lines
5.9 KiB
Plaintext
Raw Permalink Normal View History

2025-09-05 17:05:43 +08:00
#!/usr/bin/env python3
"""
测试修正后的回合系统
双方共同执行一轮为一个回合
"""
import sys
import os
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
from kards_battle.core.battle_engine import BattleEngine
def test_turn_system():
"""测试回合系统的正确性"""
print("\n🔄 回合系统测试")
print("-" * 40)
engine = BattleEngine("Germany", "USA", debug_mode=True)
print(f"初始状态:")
print(f" 回合: {engine.current_turn}, 阶段: {engine.turn_phase}")
print(f" 当前玩家: {engine.active_player}")
print(f" 德军 Slot: {engine.get_kredits_slot('Germany')}, Kredits: {engine.get_kredits('Germany')}")
print(f" 美军 Slot: {engine.get_kredits_slot('USA')}, Kredits: {engine.get_kredits('USA')}")
# 记录回合变化
turns_log = []
for i in range(8): # 进行8次end_turn调用
result = engine.end_turn()
turns_log.append({
"step": i + 1,
"turn": result["turn_number"],
"phase": result["turn_phase"],
"player": result["new_active_player"],
"is_new_round": result["is_new_round"],
"kredits": result["kredits"],
"kredits_slot": result["kredits_slot"]
})
print(f"\nStep {i+1}:")
print(f" 回合: {result['turn_number']}, 阶段: {result['turn_phase']}")
print(f" 当前玩家: {result['new_active_player']}")
print(f" 是否新回合: {result['is_new_round']}")
print(f" Kredits: {result['kredits']}, Slot: {result['kredits_slot']}")
print(f"\n📊 回合变化汇总:")
print("-" * 40)
for log in turns_log:
player_short = "德军" if log["player"] == "Germany" else "美军"
new_round_mark = " 🆕" if log["is_new_round"] else ""
print(f"Step {log['step']}: 回合{log['turn']}.{log['phase']} - {player_short} (Kredits:{log['kredits']}/Slot:{log['kredits_slot']}){new_round_mark}")
# 验证规则
print(f"\n✅ 验证规则:")
# 1. 验证回合数只在phase=1时增长
new_rounds = [log for log in turns_log if log["is_new_round"]]
turn_numbers = [log["turn"] for log in new_rounds]
turn_increase_correct = turn_numbers == [2, 3, 4, 5] # 应该递增
print(f" 回合数正确递增: {'是' if turn_increase_correct else '否'} - {turn_numbers}")
# 2. 验证阶段在1和2之间交替
phases = [log["phase"] for log in turns_log]
phase_alternation_correct = phases == [2, 1, 2, 1, 2, 1, 2, 1]
print(f" 阶段正确交替: {'是' if phase_alternation_correct else '否'} - {phases}")
# 3. 验证玩家正确交替
players = [log["player"] for log in turns_log]
player_alternation_correct = all(
players[i] != players[i+1] for i in range(len(players)-1)
)
print(f" 玩家正确交替: {'是' if player_alternation_correct else '否'}")
# 4. 验证Kredits Slot只在新回合时增长
germany_slots = []
usa_slots = []
for log in turns_log:
if log["player"] == "Germany":
germany_slots.append(log["kredits_slot"])
else:
usa_slots.append(log["kredits_slot"])
# 德军的Slot应该递增[2, 3, 4, 5]初始1每次新回合+1
# 美军的Slot应该递增[1, 2, 3, 4]初始0每次轮到自己+1
germany_growth_correct = germany_slots == [2, 3, 4, 5]
usa_growth_correct = usa_slots == [1, 2, 3, 4]
print(f" 德军Slot正确增长: {'是' if germany_growth_correct else '否'} - {germany_slots}")
print(f" 美军Slot正确增长: {'是' if usa_growth_correct else '否'} - {usa_slots}")
all_correct = all([
turn_increase_correct,
phase_alternation_correct,
player_alternation_correct,
germany_growth_correct,
usa_growth_correct
])
assert all_correct, "Turn system should work correctly"
def test_initial_state():
"""测试初始状态"""
print("\n🎬 初始状态测试")
print("-" * 40)
engine = BattleEngine("Germany", "USA")
# 验证初始状态
initial_correct = (
engine.current_turn == 1 and
engine.turn_phase == 1 and
engine.active_player == "Germany" and
engine.get_kredits_slot("Germany") == 1 and # 第一回合开始时应该有1点
engine.get_kredits_slot("USA") == 0 and
engine.get_kredits("Germany") == 1 and
engine.get_kredits("USA") == 0
)
print(f"初始回合: {engine.current_turn}")
print(f"初始阶段: {engine.turn_phase}")
print(f"初始玩家: {engine.active_player}")
print(f"德军初始Slot/Kredits: {engine.get_kredits_slot('Germany')}/{engine.get_kredits('Germany')}")
print(f"美军初始Slot/Kredits: {engine.get_kredits_slot('USA')}/{engine.get_kredits('USA')}")
print(f"✅ 初始状态正确: {'是' if initial_correct else '否'}")
assert initial_correct, "Initial state should be correct"
def run_all_tests():
"""运行所有测试"""
print("🧪 回合系统测试套件")
print("=" * 50)
tests = [
("初始状态", test_initial_state),
("回合系统", test_turn_system),
]
results = {}
for name, test_func in tests:
try:
results[name] = test_func()
except Exception as e:
print(f"❌ {name} 测试异常: {e}")
results[name] = False
print("\n📊 测试结果汇总")
print("=" * 50)
all_passed = True
for name, passed in results.items():
status = "✅ 通过" if passed else "❌ 失败"
print(f"{name}: {status}")
if not passed:
all_passed = False
print(f"\n总体结果: {'🎉 全部通过!' if all_passed else '❌ 有测试失败'}")
return all_passed
if __name__ == "__main__":
run_all_tests()