Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Healers by Askanio234
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
self.defense = 0
self.max_health = self.health
def damage(self, other, modifier=1.0):
if self.attack > other.defense:
damage = self.attack * modifier - other.defense
other.health -= damage
def restore_health(self, ammount):
self.health += ammount
if self.health > self.max_health:
self.health = self.max_health
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
class Defender(Warrior):
def __init__(self):
super().__init__()
self.health = 60
self.max_health = self.health
self.attack = 3
self.defense = 2
class Vampire(Warrior):
def __init__(self):
super().__init__()
self.health = 40
self.max_health = self.health
self.attack = 4
self.leech = 0.5
def damage(self, other):
super().damage(other)
damage_leeched = (self.attack - other.defense) * self.leech
self.restore_health(damage_leeched)
class Lancer(Warrior):
def __init__(self):
super().__init__()
self.health = 50
self.max_health = self.health
self.attack = 6
self.penetration = 0.5
class Healer(Warrior):
def __init__(self):
super().__init__()
self.health = 50
self.max_health = self.health
self.attack = 0
self.healing_power = 2
def heal(self, other):
other.restore_health(self.healing_power)
def damage_other(unit_1, unit_2, next_unit):
unit_1.damage(unit_2)
if isinstance(unit_1, Lancer) and next_unit:
unit_1.damage(next_unit, modifier=unit_1.penetration)
def fight(unit_1, unit_2, first_army=None, second_army=None):
if first_army and second_army:
unit_1_2, unit_2_2 = first_army.get_next_unit(), second_army.get_next_unit()
else:
unit_1_2, unit_2_2 = None, None
while unit_1.health > 0 and unit_2.health > 0:
damage_other(unit_1, unit_2, unit_2_2)
if first_army:
first_army.remove_dead()
first_army.get_healing()
if not unit_2.is_alive:
return True
damage_other(unit_2, unit_1, unit_1_2)
if second_army:
second_army.remove_dead()
second_army.get_healing()
if not unit_1.is_alive:
return False
class Army:
def __init__(self):
self.units = []
def add_units(self, class_name, number):
units_to_add = [class_name() for num in range(number)]
self.units.extend(units_to_add)
def get_next_unit(self):
if len(self.units) > 1:
return self.units[1]
def get_healing(self):
if len(self.units) > 1:
if isinstance(self.units[1], Healer):
self.units[1].heal(self.units[0])
def remove_dead(self):
for position, unit in enumerate(self.units):
if not unit.is_alive:
self.units.pop(position)
class Battle:
def fight(self, first_army, second_army):
while first_army.units and second_army.units:
result = fight(first_army.units[0], second_army.units[0], first_army, second_army)
if result:
second_army.units.pop(0)
else:
first_army.units.pop(0)
return bool(first_army.units)
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
print("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()
army_1 = Army()
army_2 = Army()
army_1.add_units(Lancer, 7)
army_1.add_units(Vampire, 3)
army_1.add_units(Healer, 1)
army_1.add_units(Warrior, 4)
army_1.add_units(Healer, 1)
army_1.add_units(Defender, 2)
army_2.add_units(Warrior, 4)
army_2.add_units(Defender, 4)
army_2.add_units(Healer, 1)
army_2.add_units(Vampire, 6)
army_2.add_units(Lancer, 4)
assert battle.fight(my_army, enemy_army) == False
assert battle.fight(army_3, army_4) == True
assert battle.fight(army_1, army_2) == True
print("Coding complete? Let's try tests!")
Aug. 22, 2018
Comments: