Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Healers by BrianMcleod
# 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
class Warrior:
def __init__(self):
self.health = 50
self.max_health = self.health
self.attack = 5
self.behind_unit = None
@property
def is_alive(self) -> bool:
return self.health > 0
def take_hit(self, attack):
self.health -= attack
return attack
def attack_other(self, other_unit):
other_unit.take_hit(self.attack)
if self.behind_unit:
self.behind_unit.heal(self)
def heal(self, unit):
pass
class Knight(Warrior):
def __init__(self):
Warrior.__init__(self)
self.attack = 7
class Defender(Warrior):
def __init__(self):
Warrior.__init__(self)
self.health = 60
self.max_health = self.health
self.attack = 3
self.defense = 2
def take_hit(self, attack):
if attack > self.defense:
self.health -= attack - self.defense
return attack- self.defense
else:
return 0
class Vampire(Warrior):
def __init__(self):
Warrior.__init__(self)
self.attack = 4
self.health = 40
self.max_health = self.health
def attack_other(self, other_unit):
damage = other_unit.take_hit(self.attack)
self.health += int(damage*0.5)
if self.behind_unit:
self.behind_unit.heal(self)
self.health = self.health if self.health <= self.max_health else self.max_health
class Lancer(Warrior):
def __init__(self):
Warrior.__init__(self)
self.attack = 6
def attack_other(self, other_unit):
other_unit.take_hit(self.attack)
if other_unit.behind_unit:
other_unit.behind_unit.take_hit(int(self.attack*0.5))
if self.behind_unit:
self.behind_unit.heal(self)
class Healer(Warrior):
def __init__(self):
Warrior.__init__(self)
self.attack = 0
self.health = 60
self.max_health = self.health
self.healing = 2
def heal(self, unit):
unit.health = unit.health + self.healing if unit.health + self.healing < unit.max_health else unit.max_health
class Army:
def __init__(self):
self.units = []
def add_units(self, unit_type, count):
for i in range(count):
self.units.append(unit_type())
if len(self.units) > 1:
self.units[-2].behind_unit = self.units[-1]
class Battle:
def __init__(self):
pass
def fight(self, army1, army2):
while (len(army1.units) !=0) and (len(army2.units) != 0):
if fight(army1.units[0], army2.units[0]):
army2.units = army2.units[1:]
else:
army1.units = army1.units[1:]
return len(army1.units) != 0
def fight(unit_1, unit_2):
unit_1_turn = True
while (unit_1.is_alive & unit_2.is_alive):
if (unit_1_turn):
unit_1.attack_other(unit_2)
else:
unit_2.attack_other(unit_1)
unit_1_turn = not unit_1_turn
return unit_1.is_alive
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. 11, 2018
Comments: