Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Healers by liw_80
class Warrior:
def __init__(self, health=50, attack=5):
self.attack = attack
self.health = health
self.max_health = health
@property
def is_alive(self):
return self.health > 0
def attack_other(self, opponent, **warriors):
# for opponent in opponents:
# {second_enemy, ally_healer}
opponent.attacked(self.attack)
if 'ally_healer' in warriors and warriors['ally_healer']:
warriors['ally_healer'].heal(self)
def attacked(self, hurt):
blood_loss = self.calc_blood_loss(hurt)
self.health -= blood_loss
return blood_loss
def calc_blood_loss(self, hurt):
return min(hurt, self.health)
class Knight(Warrior):
def __init__(self):
super().__init__(health=50, attack=7)
self.attack = 7
class Defender(Warrior):
def __init__(self):
super().__init__(health=60, attack=3)
self.defense = 2
def calc_blood_loss(self, hurt):
return min(max(hurt - self.defense, 0), self.health)
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4)
self.vampirism = 50
def attack_other(self, opponent, **warriors):
blood = opponent.attacked(self.attack)
self.health = self.health + blood * self.vampirism // 100
if 'ally_healer' in warriors and warriors['ally_healer']:
warriors['ally_healer'].heal(self)
class Lancer(Warrior):
def __init__(self):
super().__init__(health=50, attack=6)
def attack_other(self, opponent, **warriors):
opponent.attacked(self.attack)
if 'second_enemy' in warriors and warriors['second_enemy']:
warriors['second_enemy'].health = max(0, warriors['second_enemy'].health - self.attack // 2)
if 'ally_healer' in warriors and warriors['ally_healer']:
warriors['ally_healer'].heal(self)
class Healer(Warrior):
def __init__(self):
super().__init__(health=60, attack=0)
self.power = 2
def heal(self, ally):
ally.health = min(ally.health + self.power, ally.max_health)
def fight(unit_1, unit_2, unit_1_s=None, unit_2_s=None):
i = 0
while unit_1.is_alive and unit_2.is_alive:
if i % 2 == 0:
unit_1.attack_other(unit_2, second_enemy=unit_2_s, ally_healer=unit_1_s if type(unit_1_s) == Healer else None)
else:
unit_2.attack_other(unit_1, second_enemy=unit_1_s, ally_healer=unit_2_s if type(unit_2_s) == Healer else None)
i += 1
return True if unit_1.is_alive else False
class Army:
def __init__(self):
self.my_army = []
@property
def has_units(self):
return bool(self.my_army)
def add_units(self, role, num):
self.my_army.extend([role() for _ in range(num)])
def __bool__(self):
return self.has_units
def __getitem__(self, i):
return self.my_army[i]
def __len__(self):
return len(self.my_army)
def release_unit(self, i):
return self.my_army.pop(i)
class Battle:
def fight(self, unit_1, unit_2):
while unit_1 and unit_2:
res = fight(unit_1[0], unit_2[0],
unit_1[1] if len(unit_1) > 1 else None,
unit_2[1] if len(unit_2) > 1 else None)
if res:
unit_2.release_unit(0)
else:
unit_1.release_unit(0)
return True if len(unit_1.my_army) > 0 else False
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
#fight tests
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
bob = Defender()
mike = Knight()
rog = Warrior()
lancelot = Defender()
eric = Vampire()
adam = Vampire()
richard = Defender()
ogre = Warrior()
freelancer = Lancer()
vampire = Vampire()
assert fight(chuck, bruce) == True
assert fight(dave, carl) == False
assert chuck.is_alive == True
assert bruce.is_alive == False
assert carl.is_alive == True
assert dave.is_alive == False
assert fight(carl, mark) == False
assert carl.is_alive == False
assert fight(bob, mike) == False
assert fight(lancelot, rog) == True
assert fight(eric, richard) == False
assert fight(ogre, adam) == True
assert fight(freelancer, vampire) == True
assert freelancer.is_alive == True
#battle tests
my_army = Army()
my_army.add_units(Defender, 2)
my_army.add_units(Vampire, 2)
my_army.add_units(Lancer, 4)
my_army.add_units(Warrior, 1)
enemy_army = Army()
enemy_army.add_units(Warrior, 2)
enemy_army.add_units(Lancer, 2)
enemy_army.add_units(Defender, 2)
enemy_army.add_units(Vampire, 3)
army_3 = Army()
army_3.add_units(Warrior, 1)
army_3.add_units(Lancer, 1)
army_3.add_units(Defender, 2)
army_4 = Army()
army_4.add_units(Vampire, 3)
army_4.add_units(Warrior, 1)
army_4.add_units(Lancer, 2)
battle = Battle()
assert battle.fight(my_army, enemy_army) == True
assert battle.fight(army_3, army_4) == False
print("Coding complete? Let's try tests!")
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
#fight tests
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
bob = Defender()
mike = Knight()
rog = Warrior()
lancelot = Defender()
eric = Vampire()
adam = Vampire()
richard = Defender()
ogre = Warrior()
freelancer = Lancer()
vampire = Vampire()
priest = Healer()
assert fight(chuck, bruce) == True
assert fight(dave, carl) == False
assert chuck.is_alive == True
assert bruce.is_alive == False
assert carl.is_alive == True
assert dave.is_alive == False
assert fight(carl, mark) == False
assert carl.is_alive == False
assert fight(bob, mike) == False
assert fight(lancelot, rog) == True
assert fight(eric, richard) == False
assert fight(ogre, adam) == True
assert fight(freelancer, vampire) == True
assert freelancer.is_alive == True
assert freelancer.health == 14
priest.heal(freelancer)
assert freelancer.health == 16
#battle tests
my_army = Army()
my_army.add_units(Defender, 2)
my_army.add_units(Healer, 1)
my_army.add_units(Vampire, 2)
my_army.add_units(Lancer, 2)
my_army.add_units(Healer, 1)
my_army.add_units(Warrior, 1)
enemy_army = Army()
enemy_army.add_units(Warrior, 2)
enemy_army.add_units(Lancer, 4)
enemy_army.add_units(Healer, 1)
enemy_army.add_units(Defender, 2)
enemy_army.add_units(Vampire, 3)
enemy_army.add_units(Healer, 1)
army_3 = Army()
army_3.add_units(Warrior, 1)
army_3.add_units(Lancer, 1)
army_3.add_units(Healer, 1)
army_3.add_units(Defender, 2)
army_4 = Army()
army_4.add_units(Vampire, 3)
army_4.add_units(Warrior, 1)
army_4.add_units(Healer, 1)
army_4.add_units(Lancer, 2)
battle = Battle()
assert battle.fight(my_army, enemy_army) == False
assert battle.fight(army_3, army_4) == True
print("Coding complete? Let's try tests!")
Jan. 20, 2020