261 lines
9.0 KiB
Plaintext
261 lines
9.0 KiB
Plaintext
|
|
#!/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
|
|||
|
|
from kards_battle.core.enums import LineType
|
|||
|
|
from kards_battle.examples.sample_units import create_german_infantry, create_american_gi, create_german_88mm_gun
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_kredits_slot_system():
|
|||
|
|
"""测试Kredits Slot系统"""
|
|||
|
|
print("\n💰 Kredits Slot系统测试")
|
|||
|
|
print("-" * 40)
|
|||
|
|
|
|||
|
|
engine = BattleEngine("Germany", "USA", debug_mode=True)
|
|||
|
|
|
|||
|
|
# 初始状态检查
|
|||
|
|
print(f"初始状态:")
|
|||
|
|
print(f" 德军 Slot: {engine.get_kredits_slot(0)}, Kredits: {engine.get_kredits(0)}")
|
|||
|
|
print(f" 美军 Slot: {engine.get_kredits_slot(1)}, Kredits: {engine.get_kredits(1)}")
|
|||
|
|
|
|||
|
|
# 模拟多回合,检查槽数增长
|
|||
|
|
results = []
|
|||
|
|
for turn in range(1, 25): # 测试到第25回合,确保能达到12
|
|||
|
|
result = engine.end_turn()
|
|||
|
|
player = result["new_active_player"]
|
|||
|
|
slot = result["kredits_slot"]
|
|||
|
|
kredits = result["kredits"]
|
|||
|
|
|
|||
|
|
# 只打印关键回合
|
|||
|
|
if turn <= 5 or slot >= 10 or turn == 24:
|
|||
|
|
print(f"回合{turn+1}: {player} - Slot: {slot}, Kredits: {kredits}")
|
|||
|
|
results.append((turn+1, player, slot, kredits))
|
|||
|
|
|
|||
|
|
# 验证规则
|
|||
|
|
print("\n验证规则:")
|
|||
|
|
slot_12_reached = any(slot >= 12 for _, _, slot, _ in results)
|
|||
|
|
kredits_equal_slot = all(slot == kredits for _, _, slot, kredits in results)
|
|||
|
|
|
|||
|
|
print(f"✅ Slot达到12: {'是' if slot_12_reached else '否'}")
|
|||
|
|
print(f"✅ Kredits等于Slot: {'是' if kredits_equal_slot else '否'}")
|
|||
|
|
|
|||
|
|
assert slot_12_reached, "Kredits Slot should reach 12"
|
|||
|
|
assert kredits_equal_slot, "Kredits should always equal Kredits Slot"
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_direct_unit_deployment():
|
|||
|
|
"""测试直接单位部署功能"""
|
|||
|
|
print("\n🚀 直接单位部署测试")
|
|||
|
|
print("-" * 40)
|
|||
|
|
|
|||
|
|
engine = BattleEngine("Germany", "USA", debug_mode=True)
|
|||
|
|
|
|||
|
|
# 创建单位
|
|||
|
|
german_inf = create_german_infantry()
|
|||
|
|
american_gi = create_american_gi()
|
|||
|
|
german_inf.stats.operation_cost = 1
|
|||
|
|
american_gi.stats.operation_cost = 1
|
|||
|
|
|
|||
|
|
# 部署德军单位
|
|||
|
|
deploy_result1 = engine.deploy_unit_to_support(german_inf, 0)
|
|||
|
|
print(f"德军步兵部署: {deploy_result1['success']}")
|
|||
|
|
|
|||
|
|
# 部署美军单位
|
|||
|
|
deploy_result2 = engine.deploy_unit_to_support(american_gi, 1)
|
|||
|
|
print(f"美军GI部署: {deploy_result2['success']}")
|
|||
|
|
|
|||
|
|
print(f"\n战场状态:")
|
|||
|
|
print(engine.battlefield)
|
|||
|
|
|
|||
|
|
success = deploy_result1["success"] and deploy_result2["success"]
|
|||
|
|
print(f"✅ 部署测试: {'通过' if success else '失败'}")
|
|||
|
|
|
|||
|
|
assert success, "Unit deployment should succeed"
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_movement_and_attack():
|
|||
|
|
"""测试移动和攻击功能"""
|
|||
|
|
print("\n⚔️ 移动和攻击测试")
|
|||
|
|
print("-" * 40)
|
|||
|
|
|
|||
|
|
# Setup units for testing
|
|||
|
|
engine = BattleEngine("Germany", "USA", debug_mode=True)
|
|||
|
|
german_inf = create_german_infantry()
|
|||
|
|
american_gi = create_american_gi()
|
|||
|
|
german_inf.stats.operation_cost = 1
|
|||
|
|
american_gi.stats.operation_cost = 1
|
|||
|
|
|
|||
|
|
# Deploy units
|
|||
|
|
deploy_result1 = engine.deploy_unit_to_support(german_inf, 0)
|
|||
|
|
deploy_result2 = engine.deploy_unit_to_support(american_gi, 1)
|
|||
|
|
|
|||
|
|
success = deploy_result1["success"] and deploy_result2["success"]
|
|||
|
|
if not success:
|
|||
|
|
print("❌ 部署失败,无法测试移动攻击")
|
|||
|
|
assert False, "Unit deployment failed"
|
|||
|
|
|
|||
|
|
# 等待一回合让单位可以行动
|
|||
|
|
engine.end_turn()
|
|||
|
|
engine.end_turn()
|
|||
|
|
|
|||
|
|
# 给足够的Kredits
|
|||
|
|
engine.debug_set_kredits(0, kredits=10)
|
|||
|
|
engine.debug_set_kredits(1, kredits=10)
|
|||
|
|
|
|||
|
|
# 德军移动到前线
|
|||
|
|
print(f"\n德军Kredits: {engine.get_kredits(0)}")
|
|||
|
|
move_result = engine.move_unit(german_inf.id, (LineType.FRONT, 0), 0)
|
|||
|
|
print(f"德军步兵移动到前线: {move_result['success']}")
|
|||
|
|
print(f"德军剩余Kredits: {engine.get_kredits(0)}")
|
|||
|
|
|
|||
|
|
if move_result["success"]:
|
|||
|
|
print(f"前线控制权: {move_result['front_line_controller']}")
|
|||
|
|
print(f"战场状态:")
|
|||
|
|
print(engine.battlefield)
|
|||
|
|
|
|||
|
|
# 切换到美军回合
|
|||
|
|
engine.end_turn()
|
|||
|
|
|
|||
|
|
# 美军攻击德军
|
|||
|
|
print(f"\n美军Kredits: {engine.get_kredits(1)}")
|
|||
|
|
attack_result = engine.attack_target(american_gi.id, german_inf.id, 1)
|
|||
|
|
print(f"美军攻击德军: {attack_result['success']}")
|
|||
|
|
|
|||
|
|
if attack_result["success"]:
|
|||
|
|
print(f"造成伤害: {attack_result.get('damage_dealt', 0)}")
|
|||
|
|
print(f"反击伤害: {attack_result.get('counter_damage', 0)}")
|
|||
|
|
print(f"摧毁单位: {len(attack_result.get('units_destroyed', []))}")
|
|||
|
|
|
|||
|
|
print(f"美军剩余Kredits: {engine.get_kredits(1)}")
|
|||
|
|
|
|||
|
|
movement_success = move_result.get("success", False)
|
|||
|
|
attack_success = attack_result.get("success", False)
|
|||
|
|
|
|||
|
|
print(f"✅ 移动测试: {'通过' if movement_success else '失败'}")
|
|||
|
|
print(f"✅ 攻击测试: {'通过' if attack_success else '失败'}")
|
|||
|
|
|
|||
|
|
assert movement_success, "Unit movement should succeed"
|
|||
|
|
assert attack_success, "Unit attack should succeed"
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_debug_functions():
|
|||
|
|
"""测试DEBUG功能"""
|
|||
|
|
print("\n🔧 DEBUG功能测试")
|
|||
|
|
print("-" * 40)
|
|||
|
|
|
|||
|
|
engine = BattleEngine("Germany", "USA", debug_mode=True)
|
|||
|
|
|
|||
|
|
# 测试设置Kredits
|
|||
|
|
debug_result1 = engine.debug_set_kredits(0, kredits=15, kredits_slot=8)
|
|||
|
|
print(f"设置德军Kredits/Slot: {debug_result1['success']}")
|
|||
|
|
print(f" 德军 Kredits: {engine.get_kredits(0)}, Slot: {engine.get_kredits_slot(0)}")
|
|||
|
|
|
|||
|
|
# 测试设置HQ防御
|
|||
|
|
debug_result2 = engine.debug_set_hq_defense(1, 10)
|
|||
|
|
print(f"设置美军HQ防御: {debug_result2['success']}")
|
|||
|
|
print(f" 美军HQ防御: {engine.battlefield.player2_hq.current_defense}")
|
|||
|
|
|
|||
|
|
# 创建单位测试属性设置
|
|||
|
|
german_inf = create_german_infantry()
|
|||
|
|
engine.deploy_unit_to_support(german_inf, 0)
|
|||
|
|
|
|||
|
|
debug_result3 = engine.debug_set_unit_stats(german_inf.id, attack=10, defense=15)
|
|||
|
|
print(f"设置单位属性: {debug_result3['success']}")
|
|||
|
|
print(f" 单位攻击力: {german_inf.stats.attack}, 防御力: {german_inf.stats.current_defense}")
|
|||
|
|
|
|||
|
|
# 测试非DEBUG模式
|
|||
|
|
engine_no_debug = BattleEngine("A", "B", debug_mode=False)
|
|||
|
|
debug_result4 = engine_no_debug.debug_set_kredits(0, kredits=20)
|
|||
|
|
print(f"非DEBUG模式限制: {not debug_result4['success']}")
|
|||
|
|
|
|||
|
|
all_success = all([
|
|||
|
|
debug_result1["success"],
|
|||
|
|
debug_result2["success"],
|
|||
|
|
debug_result3["success"],
|
|||
|
|
not debug_result4["success"] # 这个应该失败
|
|||
|
|
])
|
|||
|
|
|
|||
|
|
print(f"✅ DEBUG功能测试: {'通过' if all_success else '失败'}")
|
|||
|
|
|
|||
|
|
assert all_success, "All debug functions should work correctly"
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_artillery_no_counter():
|
|||
|
|
"""测试火炮不受反击"""
|
|||
|
|
print("\n🎯 火炮不受反击测试")
|
|||
|
|
print("-" * 40)
|
|||
|
|
|
|||
|
|
engine = BattleEngine("Germany", "USA", debug_mode=True)
|
|||
|
|
|
|||
|
|
# 部署单位
|
|||
|
|
artillery = create_german_88mm_gun()
|
|||
|
|
american_gi = create_american_gi()
|
|||
|
|
artillery.stats.operation_cost = 1
|
|||
|
|
american_gi.stats.operation_cost = 1
|
|||
|
|
|
|||
|
|
engine.deploy_unit_to_support(artillery, 0)
|
|||
|
|
engine.deploy_unit_to_support(american_gi, 1)
|
|||
|
|
|
|||
|
|
# 给足够Kredits
|
|||
|
|
engine.debug_set_kredits(0, kredits=10)
|
|||
|
|
|
|||
|
|
# 火炮攻击GI
|
|||
|
|
attack_result = engine.attack_target(artillery.id, american_gi.id, 0)
|
|||
|
|
|
|||
|
|
success = attack_result.get("success", False)
|
|||
|
|
no_counter = attack_result.get("counter_damage", -1) == 0
|
|||
|
|
|
|||
|
|
print(f"火炮攻击成功: {'是' if success else '否'}")
|
|||
|
|
print(f"火炮不受反击: {'是' if no_counter else '否'}")
|
|||
|
|
print(f"造成伤害: {attack_result.get('damage_dealt', 0)}")
|
|||
|
|
|
|||
|
|
result = success and no_counter
|
|||
|
|
print(f"✅ 火炮测试: {'通过' if result else '失败'}")
|
|||
|
|
|
|||
|
|
assert result, "Artillery should not receive counter damage"
|
|||
|
|
|
|||
|
|
|
|||
|
|
def run_all_tests():
|
|||
|
|
"""运行所有测试"""
|
|||
|
|
print("🧪 新战斗引擎系统全面测试")
|
|||
|
|
print("=" * 50)
|
|||
|
|
|
|||
|
|
tests = [
|
|||
|
|
("Kredits Slot系统", test_kredits_slot_system),
|
|||
|
|
("直接部署功能", lambda: test_direct_unit_deployment()[0]),
|
|||
|
|
("移动和攻击", test_movement_and_attack),
|
|||
|
|
("DEBUG功能", test_debug_functions),
|
|||
|
|
("火炮不受反击", test_artillery_no_counter),
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
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()
|