Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Healers by Millau
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
self.max_health = self.health
self.next_unit = None
def action(self, enemy):
enemy.attacked_by(self)
if self.next_unit:
self.next_unit.support(self)
def attacked_by(self, enemy):
self.health -= enemy.attack
def support(self, target):
pass
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
self.max_health = self.health
class Defender(Warrior):
def __init__(self):
super().__init__()
self.health = 60
self.attack = 3
self.defence = 2
self.max_health = self.health
def attacked_by(self, enemy):
self.health -= max(enemy.attack - self.defence, 0)
class Vampire(Warrior):
def __init__(self):
super().__init__()
self.health = 40
self.attack = 4
self.vampirism = 50
self.max_health = self.health
def action(self, enemy):
original_enemy_health = enemy.health
enemy.attacked_by(self)
enemy_dmg = original_enemy_health - enemy.health
self.health += int(enemy_dmg * (self.vampirism / 100))
if self.next_unit:
self.next_unit.support(self)
class Lancer(Warrior):
def __init__(self):
super().__init__()
self.health = 50
self.attack = 6
self.max_health = self.health
def action(self, enemy):
enemy.attacked_by(self)
if enemy.next_unit:
original_atk = self.attack
self.attack = int(self.attack * 0.5)
enemy.next_unit.attacked_by(self)
self.attack = original_atk
if self.next_unit:
self.next_unit.support(self)
class Healer(Warrior):
def __init__(self):
super().__init__()
self.health = 60
self.attack = 0
self.max_health = self.health
def action(self, enemy):
if self.next_unit:
self.next_unit.support(self)
def support(self, target):
self.heal(target)
def heal(self, target):
target.health += 2
if target.health > target.max_health:
target.health = target.max_health
def fight(unit_1, unit_2):
round = 1
while unit_1.is_alive and unit_2.is_alive:
if round % 2 == 1:
unit_1.action(unit_2)
else:
unit_2.action(unit_1)
round += 1
return unit_1.is_alive
class Army:
def __init__(self):
self.units = []
def add_units(self, unit, amount):
self.units += [unit() for i in range(amount)]
for i in range(len(self.units) - 1):
self.units[i].next_unit = self.units[i + 1]
@property
def first_unit(self):
for unit in self.units:
if unit.is_alive:
return unit
return None
@property
def is_alive(self):
for unit in self.units:
if unit.is_alive:
return True
return False
class Battle:
def fight(self, army_1, army_2):
while army_1.is_alive and army_2.is_alive:
fight(army_1.first_unit, army_2.first_unit)
return army_1.is_alive
Jan. 1, 2020