201 lines
6.4 KiB
Python
201 lines
6.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
烟幕规则测试
|
|
验证烟幕的复杂移除条件是否正确实现
|
|
"""
|
|
import sys
|
|
import os
|
|
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
|
|
|
|
|
|
def test_smokescreen_movement_removal():
|
|
"""测试移动后烟幕消失"""
|
|
print("\n🌫️ 烟幕移动测试")
|
|
print("-" * 30)
|
|
|
|
engine = BattleEngine("Germany", "USA", debug_mode=True)
|
|
|
|
# 创建带烟幕的单位
|
|
unit = create_german_infantry()
|
|
unit.add_keyword("SMOKESCREEN")
|
|
unit.stats.operation_cost = 1
|
|
|
|
print(f"初始状态: 烟幕={unit.has_keyword('SMOKESCREEN')}")
|
|
|
|
# 部署单位
|
|
engine.deploy_unit_to_support(unit, 0)
|
|
print(f"部署后: 烟幕={unit.has_keyword('SMOKESCREEN')}")
|
|
|
|
# 等到下一回合使其能移动
|
|
engine.end_turn()
|
|
engine.end_turn()
|
|
engine.debug_set_kredits(0, kredits=10)
|
|
|
|
# 移动单位
|
|
move_result = engine.move_unit(unit.id, (LineType.FRONT, 0), 0)
|
|
print(f"移动成功: {move_result.get('success', False)}")
|
|
print(f"移动后: 烟幕={unit.has_keyword('SMOKESCREEN')}")
|
|
|
|
# 验证烟幕应该消失
|
|
assert not unit.has_keyword("SMOKESCREEN"), "移动后应该失去烟幕"
|
|
print("✅ 移动后烟幕正确消失")
|
|
|
|
|
|
def test_smokescreen_attack_removal():
|
|
"""测试攻击后烟幕消失"""
|
|
print("\n🌫️ 烟幕攻击测试")
|
|
print("-" * 30)
|
|
|
|
# 直接测试perform_attack方法
|
|
attacker = create_german_infantry()
|
|
attacker.add_keyword("SMOKESCREEN")
|
|
|
|
print(f"攻击前: 烟幕={attacker.has_keyword('SMOKESCREEN')}")
|
|
|
|
# 直接调用perform_attack方法
|
|
attacker.perform_attack()
|
|
|
|
print(f"攻击后: 烟幕={attacker.has_keyword('SMOKESCREEN')}")
|
|
|
|
# 验证烟幕应该消失
|
|
assert not attacker.has_keyword("SMOKESCREEN"), "攻击后应该失去烟幕"
|
|
print("✅ 攻击后烟幕正确消失")
|
|
|
|
|
|
def test_smokescreen_frontline_removal():
|
|
"""测试前线自动移除烟幕"""
|
|
print("\n🌫️ 烟幕前线测试")
|
|
print("-" * 30)
|
|
|
|
engine = BattleEngine("Germany", "USA", debug_mode=True)
|
|
|
|
# 创建带烟幕的单位
|
|
unit = create_german_infantry()
|
|
unit.add_keyword("SMOKESCREEN")
|
|
unit.stats.operation_cost = 1
|
|
|
|
print(f"初始状态: 烟幕={unit.has_keyword('SMOKESCREEN')}")
|
|
|
|
# 部署到支援线
|
|
engine.deploy_unit_to_support(unit, 0)
|
|
print(f"部署到支援线: 烟幕={unit.has_keyword('SMOKESCREEN')}")
|
|
|
|
# 等到下一回合
|
|
engine.end_turn()
|
|
engine.end_turn()
|
|
engine.debug_set_kredits(0, kredits=10)
|
|
|
|
# 移动到前线
|
|
move_result = engine.move_unit(unit.id, (LineType.FRONT, 0), 0)
|
|
print(f"移动到前线成功: {move_result.get('success', False)}")
|
|
print(f"到达前线后: 烟幕={unit.has_keyword('SMOKESCREEN')}")
|
|
print(f"当前位置: {unit.position}")
|
|
|
|
# 验证烟幕应该消失(两个原因:移动后和前线位置)
|
|
assert not unit.has_keyword("SMOKESCREEN"), "前线单位不应该有烟幕"
|
|
print("✅ 前线单位正确失去烟幕")
|
|
|
|
|
|
def test_smokescreen_guard_conflict():
|
|
"""测试烟幕与守护的冲突"""
|
|
print("\n🌫️ 烟幕守护冲突测试")
|
|
print("-" * 30)
|
|
|
|
# 创建带烟幕的单位
|
|
unit = create_german_infantry()
|
|
unit.add_keyword("SMOKESCREEN")
|
|
|
|
print(f"初始状态: 烟幕={unit.has_keyword('SMOKESCREEN')}, 守护={unit.has_keyword('GUARD')}")
|
|
|
|
# 添加守护关键词
|
|
unit.add_keyword("GUARD")
|
|
|
|
print(f"添加守护后: 烟幕={unit.has_keyword('SMOKESCREEN')}, 守护={unit.has_keyword('GUARD')}")
|
|
|
|
# 验证烟幕应该消失
|
|
assert not unit.has_keyword("SMOKESCREEN"), "有守护的单位不应该有烟幕"
|
|
assert unit.has_keyword("GUARD"), "守护关键词应该存在"
|
|
print("✅ 守护与烟幕正确互斥")
|
|
|
|
|
|
def test_smokescreen_protection():
|
|
"""测试烟幕保护效果"""
|
|
print("\n🌫️ 烟幕保护测试")
|
|
print("-" * 30)
|
|
|
|
engine = BattleEngine("Germany", "USA", debug_mode=True)
|
|
|
|
# 创建带烟幕的目标
|
|
target = create_german_infantry()
|
|
target.add_keyword("SMOKESCREEN")
|
|
target.stats.operation_cost = 1
|
|
|
|
# 创建攻击者
|
|
attacker = create_german_infantry()
|
|
attacker.stats.operation_cost = 1
|
|
|
|
# 部署单位
|
|
engine.deploy_unit_to_support(target, 0)
|
|
engine.deploy_unit_to_support(attacker, 1)
|
|
|
|
print(f"目标烟幕: {target.has_keyword('SMOKESCREEN')}")
|
|
|
|
# 切换到美军回合
|
|
engine.end_turn()
|
|
print(f"当前活跃玩家: {engine.active_player}")
|
|
engine.debug_set_kredits(1, kredits=10)
|
|
|
|
# 尝试攻击带烟幕的单位
|
|
attack_result = engine.attack_target(attacker.id, target.id, 1)
|
|
print(f"攻击烟幕单位结果: {attack_result.get('success', False)}")
|
|
print(f"失败原因: {attack_result.get('reason', 'Unknown')}")
|
|
|
|
# 攻击应该失败
|
|
assert not attack_result.get('success', False), "不应该能攻击烟幕单位"
|
|
print("✅ 烟幕保护正确生效")
|
|
|
|
|
|
def run_all_smokescreen_tests():
|
|
"""运行所有烟幕测试"""
|
|
print("🧪 烟幕规则完整测试")
|
|
print("=" * 40)
|
|
|
|
tests = [
|
|
("移动后烟幕消失", test_smokescreen_movement_removal),
|
|
("攻击后烟幕消失", test_smokescreen_attack_removal),
|
|
("前线自动移除烟幕", test_smokescreen_frontline_removal),
|
|
("烟幕守护冲突", test_smokescreen_guard_conflict),
|
|
("烟幕保护效果", test_smokescreen_protection),
|
|
]
|
|
|
|
results = {}
|
|
for name, test_func in tests:
|
|
try:
|
|
test_func()
|
|
results[name] = True
|
|
except Exception as e:
|
|
print(f"❌ {name} 测试失败: {e}")
|
|
results[name] = False
|
|
|
|
print("\n📊 烟幕测试结果汇总")
|
|
print("=" * 40)
|
|
|
|
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__":
|
|
success = run_all_smokescreen_tests()
|
|
if not success:
|
|
sys.exit(1) |