Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Healers by wojciech.dymalski
# Taken from mission The Lancers
# Taken from mission The Vampires
# Taken from mission The Defenders
# Taken from mission Army Battles
# Taken from mission The Warriors
#################### klasy wojowników #####################################
class Warrior:
def __init__(self, health=50, attack=5):
self.maxhealth = health
self.health = health
self.attack = attack
@property
def is_alive(self) -> bool:
return self.health > 0
def hit(self, other):
other.loss(self.attack)
def loss(self, attack):
self.health -= self.damage(attack)
def damage(self, attack):
return attack
def max_health(self):
self.health = min(self.health, self.maxhealth)
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
def damage(self, attack):
return max(0, attack - self.defense)
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4)
self.vampirism = 0.5
def hit(self, other):
other.loss(self.attack)
self.health += self.vampirism*other.damage(self.attack)
class Lancer(Warrior):
def __init__(self):
super().__init__(attack=6)
def hit(self, other):
other.loss(self.attack)
def piercing(self, other):
other.loss(self.attack*0.5)
class Healer(Warrior):
def __init__(self):
super().__init__(health=60, attack=0)
self.healing = 2
def heal(self, other):
other.health += self.healing
other.max_health
class Army():
def __init__(self):
self.units = []
def add_units(self, typ, amount):
for i in range(amount):
self.units.append(typ())
@property
def exist(self) -> bool:
return len(self.units) > 0
@property
def dual_exist(self) -> bool:
return len(self.units) > 1
class Dual(Army):
def __init__(self):
super().__init__()
def add_units(self, typ):
self.units.append(typ)
class Battle(object):
def __init__(object):
pass
def fight(self, army_1, army_2):
while army_1.exist and army_2.exist:
dual_1 = Dual()
dual_1.add_units(army_1.units[0])
dual_2 = Dual()
dual_2.add_units(army_2.units[0])
if army_1.dual_exist:
dual_1.add_units(army_1.units[1])
if army_2.dual_exist:
dual_2.add_units(army_2.units[1])
dual_1_won = dual_fight(dual_1, dual_2)
if dual_1_won:
army_2.units.pop(0)
else:
army_1.units.pop(0)
return army_1.exist
#################### walka ################################################
def fight(unit_1, unit_2):
while unit_1.is_alive and unit_2.is_alive:
unit_1.hit(unit_2)
if unit_2.is_alive:
unit_2.hit(unit_1)
return unit_1.is_alive
def dual_fight(dual_1, dual_2):
while dual_1.units[0].is_alive and dual_2.units[0].is_alive:
dual_1.units[0].hit(dual_2.units[0])
if isinstance(dual_1.units[0], Lancer) and dual_2.dual_exist:
dual_1.units[0].piercing(dual_2.units[1])
if dual_1.dual_exist and isinstance(dual_1.units[1], Healer):
dual_1.units[1].heal(dual_1.units[0])
if dual_2.units[0].is_alive:
dual_2.units[0].hit(dual_1.units[0])
if isinstance(dual_2.units[0], Lancer) and dual_1.dual_exist:
dual_2.units[0].piercing(dual_1.units[1])
if dual_2.dual_exist and isinstance(dual_2.units[1], Healer):
dual_2.units[1].heal(dual_2.units[0])
return dual_1.units[0].is_alive
#################### bitwa ################################################
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!")
Feb. 5, 2019