Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Weapons 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 and self.next_unit.is_alive:
self.next_unit.support(self)
def attacked_by(self, enemy):
self.health -= enemy.attack
def support(self, target):
pass
def equip_weapon(self, weapon):
self.health = max(self.health + weapon.health, 0)
self.max_health = max(self.max_health + weapon.health, 0)
self.attack = max(self.attack + weapon.attack, 0)
@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.defense = 2
self.max_health = self.health
def attacked_by(self, enemy):
self.health -= max(enemy.attack - self.defense, 0)
def equip_weapon(self, weapon):
super().equip_weapon(weapon)
self.defense = max(self.defense + weapon.defense, 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)
def equip_weapon(self, weapon):
super().equip_weapon(weapon)
self.vampirism = max(self.vampirism + weapon.vampirism, 0)
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.heal_power = 2
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 = min(target.health + self.heal_power,
target.max_health)
def equip_weapon(self, weapon):
super().equip_weapon(weapon)
self.heal_power = max(self.heal_power + weapon.heal_power, 0)
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 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)
class Army:
def __init__(self):
self.units = []
def __len__(self):
return len(self.surviving_units)
def add_units(self, unit, amount):
self.units += [unit() for i in range(amount)]
@property
def surviving_units(self):
units = []
for unit in self.units:
if unit.is_alive:
units.append(unit)
return units
class Battle:
def fight(self, army_1, army_2):
for army in [army_1, army_2]:
for i in range(len(army) - 1):
army.surviving_units[i].next_unit = army.surviving_units[i + 1]
while army_1.surviving_units and army_2.surviving_units:
fight(army_1.surviving_units[0], army_2.surviving_units[0])
return True if army_1.surviving_units else False
def straight_fight(self, army_1, army_2):
while army_1.surviving_units and army_2.surviving_units:
for unit_1, unit_2 in zip(army_1.surviving_units,
army_2.surviving_units):
fight(unit_1, unit_2)
return True if army_1.surviving_units else False
Jan. 2, 2020