Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Vampires by vit.aborigen
isDebug = False
def log(text):
if isDebug:
print(text)
class Army(object):
def __init__(self):
self.units = []
def add_units(self, unit_type, amount):
self.units.extend([unit_type() for i in range(amount)])
def __len__(self):
return len(self.units)
class Battle(object):
def fight(self, army_1, army_2):
log("Initial")
unit_1 = army_1.units.pop(0)
unit_2 = army_2.units.pop(0)
while unit_1.is_alive and unit_2.is_alive:
if Battle.fight_between_units(unit_1, unit_2):
if len(army_2) > 0:
log('\nNew unit from the army_2')
unit_2 = army_2.units.pop(0)
elif len(army_1) > 0:
log('\nNew unit from the army_1')
unit_1 = army_1.units.pop(0)
return unit_1.is_alive
@classmethod
def fight_between_units(cls, unit_1, unit_2):
while all([unit_1.is_alive, unit_2.is_alive]):
log("Unit_1 HP: {:2d}, Unit_2 HP: {:2d}".format(unit_1.health, unit_2.health))
Battle.calculate_damage_dealt(unit_1, unit_2)
if unit_2.is_alive:
Battle.calculate_damage_dealt(unit_2, unit_1)
return unit_1.is_alive
@classmethod
def calculate_damage_dealt(cls, unit_1, unit_2):
damage_dealt = max(unit_1.attack - unit_2.defence, 0)
unit_2.health -= damage_dealt
unit_1.health += int(damage_dealt * unit_1.vampirism)
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
self.defence = 0
self.vampirism = 0
@property
def is_alive(self):
return True if self.health > 0 else False
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
class Defender(Warrior):
def __init__(self):
super().__init__()
self.health = 60
self.attack = 3
self.defence = 2
class Vampire(Warrior):
def __init__(self):
super().__init__()
self.health = 40
self.attack = 4
self.vampirism = 0.5
fight = Battle.fight_between_units
Oct. 8, 2018