Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Warlords by kalauroma7997
class Warrior:
def __init__(self, health=50, attack=5):
self.health = health
self.attack = attack
self.default_health = self.health
@property
def is_alive(self):
return self.health > 0
def to_hit(self, enemy):
return enemy.take_damage(self.attack)
def take_damage(self, dmg):
self.health -= self.damage(dmg)
def damage(self, dmg):
return dmg
def equip_weapon(self, weapon):
self.health = max(0, self.health + weapon.health)
self.attack = max(0, self.attack + weapon.attack)
if isinstance(self, (Defender, Warlord)):
self.defense = max(0, self.defense + weapon.defense)
elif isinstance(self, Vampire):
self.vampirism = max(0, self.vampirism + weapon.vampirism)
elif isinstance(self, Healer):
self.attack = 0
self.heal_power = max(0, self.heal_power + weapon.heal_power)
self.default_health = self.health
class Knight(Warrior):
def __init__(self):
super().__init__(health=50, attack=7)
class Defender(Warrior):
def __init__(self, defense=2):
super().__init__(health=60, attack=3)
self.defense = defense
def damage(self, damage):
return max(0, damage - self.defense)
class Vampire(Warrior):
def __init__(self, vampirism=50):
super().__init__(health=40, attack=4)
self.vampirism = vampirism
def to_hit(self, enemy, enemy2=None):
super().to_hit(enemy)
self.health += int((enemy.damage(self.attack) * self.vampirism) / 100)
class Lancer(Warrior):
def __init__(self, damage_neighbour=.5):
super().__init__(health=50, attack=6)
self.damage_neighbour = damage_neighbour
class Healer(Warrior):
def __init__(self, heal_power=2):
super().__init__(health=60, attack=0)
self.heal_power = heal_power
def heal(self, neighbour):
neighbour.health = min(neighbour.health + self.heal_power, neighbour.default_health)
class Warlord(Warrior):
def __init__(self, defense=2):
super().__init__(health=100, attack=4)
self.defense = defense
def damage(self, damage):
return max(0, damage - self.defense)
class Weapon:
def __init__(self, health=0, attack=0, defense=0, vampirism=0, heal_power=0):
self.health = health
self.attack = attack
self.defense = defense
self.vampirism = vampirism
self.heal_power = heal_power
class Sword(Weapon):
def __init__(self):
super().__init__(health=5, attack=2)
class Shield(Weapon):
def __init__(self):
super().__init__(health=20, attack=-1, defense=2)
class GreatAxe(Weapon):
def __init__(self):
super().__init__(health=-15, attack=5, defense=-2, vampirism=10)
class Katana(Weapon):
def __init__(self):
super().__init__(health=-20, attack=6, defense=-5, vampirism=50)
class MagicWand(Weapon):
def __init__(self):
super().__init__(health=30, attack=3, heal_power=3)
def fight(unit1, unit2):
while unit1.is_alive and unit2.is_alive:
unit1.to_hit(unit2)
if not unit2.is_alive:
break
unit2.to_hit(unit1)
return unit1.is_alive
class Army(object):
def __init__(self):
self.units = []
self.spotted = False
self.warlord = False
def add_units(self, warrior, amount):
for i in range(amount):
if isinstance(warrior(), Warlord):
if not self.warlord:
self.warlord = True
else:
continue
self.units.append(warrior())
def next_warrior(self, current):
ind = self.units.index(current)
if ind+1 < len(self.units): return self.units[ind+1]
@property
def still_alive(self):
return [w for w in self.units if w.is_alive]
@property
def any_survivors(self):
return bool([w for w in self.units if w.is_alive])
def move_units(self):
if not self.warlord:
return
lancers = [u for u in self.units if isinstance(u, Lancer)]
healers = [u for u in self.units if isinstance(u, Healer)]
attackers = [u for u in self.units if u.attack > 0 and not isinstance(u, Warlord) and not isinstance(u, Lancer)]
all_attackers = lancers + attackers
warlord = [u for u in self.units if isinstance(u, Warlord)]
self.units = all_attackers[:1] + healers + all_attackers[1:] + warlord
class Battle(object):
def clash(self, warrior1, warrior2, army1, army2):
next_warrior1 = army1.next_warrior(warrior1)
next_warrior2 = army2.next_warrior(warrior2)
warrior1.to_hit(warrior2)
if next_warrior2 and isinstance(warrior1, Lancer):
next_warrior2.take_damage(warrior1.attack * warrior1.damage_neighbour)
if next_warrior1 and isinstance(next_warrior1, Healer):
next_warrior1.heal(warrior1)
def fight(self, army1, army2):
army1.move_units()
army2.move_units()
while army1.any_survivors and army2.any_survivors:
warrior1 = army1.units[0]
warrior2 = army2.units[0]
while True:
self.clash(warrior1, warrior2, army1, army2)
if not warrior2.is_alive:
army2.units.pop(0)
army2.move_units()
break
self.clash(warrior2, warrior1, army2, army1)
if not warrior1.is_alive:
army1.units.pop(0)
army1.move_units()
break
return army1.any_survivors
def straight_fight(self, army1, army2):
while army1.any_survivors and army2.any_survivors:
for warrior1, warrior2 in zip(army1.units, army2.units):
fight(warrior1, warrior2)
army1.units = army1.still_alive
army2.units = army2.still_alive
return army1.any_survivors
Sept. 20, 2020