Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Healers by freixodachamorra
class Warrior:
attack = 5
health = 50
max_health = 50
army = None
my_healer = None
def __init__(self):
self.max_health = self.health
self.is_alive = True
def hit(self, other):
other.injury(self.attack)
if self.my_healer != None:
self.my_healer.heal(self)
def injury(self,attack):
self.health -= attack
if self.health <= 0:
self.is_alive = False
return self.is_alive
class Knight(Warrior):
attack = 7
class Defender(Warrior):
attack = 3
defense = 2
health = 60
def injury(self, attack):
attack = max(0, attack - self.defense)
return super(Defender, self).injury(attack)
class Vampire(Warrior):
attack = 4
health = 40
vampirism = 0.5
def hit(self, other):
health_other = other.health
super(Vampire, self).hit(other)
damage = health_other - other.health
self.health += damage * self.vampirism
class Lancer(Warrior):
attack = 6
def hit(self, other):
other_health = other.health
super(Lancer, self).hit(other)
damage = other_health - other.health;
army = other.army
if army != None and len(army.army_list) > 1:
army.army_list[1].injury(damage / 2)
class Healer(Warrior):
attack = 0
health = 60
def heal(self, other):
if self.is_alive:
health = other.health + 2
other.health = min(health, other.max_health)
class Army():
def __init__(self):
self.army_list = []
def add_units(self, class_warrior, units):
for _ in range(units):
soldier = class_warrior()
soldier.army = self
self.army_list.append(soldier)
if len(self.army_list) > 1 and class_warrior == Healer:
self.army_list[-2].my_healer = soldier
def remove_units(self):
self.army_list.pop(0)
class Battle():
def fight(self, army_1, army_2):
def little_battle(one, two):
if fight(one.army_list[0], two.army_list[0]):
two.remove_units()
return True
else:
one.remove_units()
return False
def win(one, two):
if not one.army_list:
return False
if not two.army_list:
return True
return None
output = None
while output == None:
little_battle(army_1, army_2)
output = win(army_1, army_2)
if output != None:
return output
return output
def fight(one, two):
while True:
one.hit(two)
if not two.is_alive:
return True
two.hit(one)
if not one.is_alive:
return 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()
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!")
Aug. 19, 2018