|
发表于 2020-2-28 14:36:37
|
显示全部楼层
- class Fighter():
- def __init__(self, name, health, dpa):
- self.name = name
- self.health = health
- self.dpa = dpa
- def attack(self, other):
- other.health -= self.dpa
- if other.health > 0:
- print('{a} attacks {b};{b} now has {c} health.'.format(a=self.name, b=other.name, c=other.health))
- else:
- print('{a} attacks {b};{b} now has {c} health and is dead.'.format(a=self.name, b=other.name, c=other.health))
- # 递归实现互相攻击
- def declare_winner(fighter_a, fighter_b, name):
- if fighter_a.health < 0:
- return print('{} wins'.format(fighter_b.name))
- elif fighter_b.health < 0:
- return print('{} wins'.format(fighter_a.name))
- if fighter_a.name == name:
- fighter_a.attack(fighter_b)
- declare_winner(fighter_a, fighter_b, fighter_b.name)
- else:
- fighter_b.attack(fighter_a)
- declare_winner(fighter_a, fighter_b, fighter_a.name)
- declare_winner(Fighter("Lew", 10, 2), Fighter("Harry", 5, 4), "Harry")
复制代码 |
|