kards-env/tests/examples/test_new_readme_example.py

68 lines
2.8 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
"""
测试更新后的README中的新战斗引擎示例代码
"""
import sys
import os
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..'))
# 新的README示例代码
from kards_battle.core.battle_engine import BattleEngine
from kards_battle.examples.sample_units import create_american_gi, create_german_infantry
from kards_battle.core.enums import LineType
# 创建战斗引擎 (专注战斗系统,不包含卡牌)
engine = BattleEngine("Germany", "USA", debug_mode=True)
# 创建单位并设置激活成本
german_infantry = create_german_infantry()
american_gi = create_american_gi()
german_infantry.stats.operation_cost = 1
american_gi.stats.operation_cost = 1
print(f"初始状态:")
print(f"德军 - Kredits: {engine.get_kredits('Germany')}, Slot: {engine.get_kredits_slot('Germany')}")
print(f"美军 - Kredits: {engine.get_kredits('USA')}, Slot: {engine.get_kredits_slot('USA')}")
# 第1回合 - 德军直接部署单位到支援线
result1 = engine.deploy_unit_to_support(german_infantry, "Germany")
print(f"德军步兵部署到支援线: {result1['success']}")
# 切换到美军回合 (Kredits Slot自动增长)
engine.end_turn()
print(f"美军回合 - Kredits: {engine.get_kredits('USA')}, Slot: {engine.get_kredits_slot('USA')}")
# 第2回合 - 美军部署并移动
result2 = engine.deploy_unit_to_support(american_gi, "USA")
print(f"美军GI部署到支援线: {result2['success']}")
# 美军移动GI到前线 (消耗1点Kredits激活费用)
move_result = engine.move_unit(american_gi.id, (LineType.FRONT, 0), "USA")
print(f"美军GI移动到前线: {move_result['success']}")
if move_result['success']:
print(f"前线控制权: {move_result['front_line_controller']}")
print(f"美军剩余Kredits: {engine.get_kredits('USA')}")
# 第3回合 - 德军反击
engine.end_turn()
print(f"德军回合 - Kredits: {engine.get_kredits('Germany')}")
# 德军从支援线攻击前线的美军 (消耗1点Kredits激活费用)
attack_result = engine.attack_target(german_infantry.id, american_gi.id, "Germany")
print(f"德军攻击美军: {attack_result['success']}")
if attack_result['success']:
print(f"造成伤害: {attack_result['damage_dealt']}, 反击伤害: {attack_result['counter_damage']}")
# 查看最终战场状态
print(f"\n最终战场:")
print(engine.battlefield)
# DEBUG功能演示 - 设置资源和属性
engine.debug_set_kredits("Germany", kredits=10)
engine.debug_set_unit_stats(german_infantry.id, attack=5, defense=8)
print(f"\nDEBUG: 德军Kredits设为10步兵属性增强为 5/8")
print(f"验证: 德军Kredits = {engine.get_kredits('Germany')}, 步兵属性 = {german_infantry.stats.attack}/{german_infantry.stats.current_defense}")
print(f"\n🎉 新战斗引擎README示例测试通过!")