Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
class Army(list) solution in Clear category for The Healers by David_Jones
class Warrior:
def __init__(self, health=50, attack=5):
self.max_health = self.health = health
self.attack = attack
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
class Defender(Warrior):
def __init__(self):
super().__init__(health=60, attack=3)
self.defense = 2
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4)
self.vampirism = 0.5
class Lancer(Warrior):
def __init__(self):
super().__init__(attack=6)
class Healer(Warrior):
def __init__(self):
super().__init__(health=60, attack=0)
def heal(self, allie):
allie.health = min(allie.max_health, allie.health + 2)
class Army(list):
def add_units(self, unit_type, n):
self.extend(unit_type() for _ in range(n))
class Battle:
def fight(self, army_1, army_2):
while army_1:
if fight(army_1[0], army_2[0], army_1, army_2):
del army_2[0]
else:
del army_1[0]
if not army_2:
return True
return False
def fight(unit_1, unit_2, army_1=None, army_2=None):
def hit(unit_1, army_1, unit_2, army_2):
if type(unit_2) is Defender:
damage = max(0, unit_1.attack - unit_2.defense)
else:
damage = unit_1.attack
unit_2.health -= damage
if type(unit_1) is Vampire:
unit_1.health += damage * unit_1.vampirism
elif type(unit_1) is Lancer and army_2 is not None and len(army_2) > 1:
army_2[1].health -= 0.5 * damage
if army_1 is not None and len(army_1) > 1 and type(army_1[1]) is Healer:
army_1[1].heal(unit_1)
while unit_1.is_alive:
hit(unit_1, army_1, unit_2, army_2)
if not unit_2.is_alive:
return True
hit(unit_2, army_2, unit_1, army_1)
return False
June 20, 2019