游戏可以运行

This commit is contained in:
2026-05-24 10:48:58 +08:00
parent a8c5fb2356
commit 0670598bf4
7 changed files with 71 additions and 21 deletions

View File

@@ -82,36 +82,48 @@ class AIPlayer:
def _attack(self, ai):
actions = []
player = self.battlefield.player
# Frontline units: attack enemy frontline > enemy support > capital
for unit in ai.get_frontline_units():
if not unit.can_attack or unit.has_attacked:
continue
enemy_units = player.get_frontline_units()
if enemy_units:
killable = [u for u in enemy_units if u.current_hp <= unit.get_effective_attack()]
enemy_front = player.get_frontline_units()
enemy_support = player.get_support_units()
all_enemy = enemy_front + enemy_support
if all_enemy:
killable = [u for u in all_enemy if u.current_hp <= unit.get_effective_attack()]
if killable:
target = min(killable, key=lambda u: u.current_hp)
else:
target = max(enemy_units, key=lambda u: u.get_effective_attack())
dead = self.battlefield.resolve_attack(unit, target)
actions.append(("attack_unit", unit, target))
else:
self.battlefield.attack_capital(unit)
actions.append(("attack_capital", unit))
# Ranged units in support attack
for unit in ai.get_support_units():
if not unit.can_attack or unit.has_attacked or not unit.is_ranged():
continue
enemy_units = player.get_frontline_units()
if enemy_units:
killable = [u for u in enemy_units if u.current_hp <= unit.get_effective_attack()]
target = min(killable, key=lambda u: u.current_hp) if killable else min(enemy_units, key=lambda u: u.current_hp)
target = max(all_enemy, key=lambda u: u.get_effective_attack())
self.battlefield.resolve_attack(unit, target)
actions.append(("attack_unit", unit, target))
else:
self.battlefield.attack_capital(unit)
actions.append(("attack_capital", unit))
# Support units: attack enemy frontline > enemy support > capital (ranged only)
for unit in ai.get_support_units():
if not unit.can_attack or unit.has_attacked:
continue
enemy_front = player.get_frontline_units()
enemy_support = player.get_support_units()
if unit.is_ranged():
all_enemy = enemy_front + enemy_support
if all_enemy:
killable = [u for u in all_enemy if u.current_hp <= unit.get_effective_attack()]
target = min(killable, key=lambda u: u.current_hp) if killable else min(all_enemy, key=lambda u: u.current_hp)
self.battlefield.resolve_attack(unit, target)
actions.append(("attack_unit", unit, target))
else:
self.battlefield.attack_capital(unit)
actions.append(("attack_capital", unit))
elif enemy_front:
killable = [u for u in enemy_front if u.current_hp <= unit.get_effective_attack()]
target = min(killable, key=lambda u: u.current_hp) if killable else min(enemy_front, key=lambda u: u.current_hp)
self.battlefield.resolve_attack(unit, target)
actions.append(("attack_unit", unit, target))
return actions
def _handle_deploy(self, card, ai):