Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
With (new) hit method returning damage, adapted for vampires. solution in Clear category for The Vampires by Phil15
class Warrior:
"""Define a warrior."""
def __init__(self):
self.health = 50
self.attack = 5
@property
def is_alive(self) -> bool:
return self.health > 0
def decrease_health(self, damage):
self.health -= damage
return damage
def hit(self, other):
if self.is_alive:
return other.decrease_health(self.attack)
return 0
class Knight(Warrior):
"""Define a knight, it's a warrior with an increased attack of 7."""
def __init__(self):
super().__init__()
self.attack = 7
def fight(unit_1, unit_2) -> bool:
"""Duel fight...
Is unit_1 stronger than unit_2?"""
while unit_1.is_alive and unit_2.is_alive:
unit_1.hit(unit_2)
unit_2.hit(unit_1)
return unit_1.is_alive
class Army(list):
"""Define an army."""
def __init__(self):
super().__init__()
self.index_first_warrior_alive = 0
def add_units(self, unit, amount_of_units):
"""Add an amount of specific units to the army."""
self += [unit() for k in range(amount_of_units)]
@property
def is_alive(self) -> bool:
"""Does the army have a living warrior?"""
return self.index_first_warrior_alive < len(self)
@property
def next(self):
self.index_first_warrior_alive += 1
@property
def first_warrior_alive(self):
return self[self.index_first_warrior_alive]
class Battle:
"""Define an epic battle."""
@staticmethod
def fight(army_1, army_2) -> bool:
while army_1.is_alive and army_2.is_alive:
if fight(army_1.first_warrior_alive,
army_2.first_warrior_alive):
army_2.next
else:
army_1.next
return army_1.is_alive
class Defender(Warrior):
"""Define a defender, it's a warrior with a defense parameter."""
def __init__(self):
self.health = 60
self.attack = 3
self.defense = 2
def decrease_health(self, damage):
damage = max(0, damage-self.defense)
self.health -= damage
return damage
class Vampire(Warrior):
"""Define a vampire, capable of vampirism, another way to heal himself."""
def __init__(self):
self.health = 40
self.attack = 4
self.vampirism = .5
def hit(self, other):
damage = super().hit(other)
self.health += int(damage * self.vampirism)
Aug. 8, 2018