Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
The Battle of the Five Armies solution in Clear category for The Lancers by von.Oak
class Army:
def __init__(self):
self.units = []
def add_units(self, unit, quantity):
self.units += [unit] * quantity
class Battle:
def fight(self, unit_1, unit_2):
formation_1 = []
formation_2 = []
while (unit_1.units or formation_1) and (unit_2.units or formation_2):
if not formation_1:
formation_1.append(unit_1.units.pop(0)())
if unit_1.units:
formation_1.append(unit_1.units.pop(0)())
if not formation_1[0].is_alive:
formation_1.pop(0)
if unit_1.units:
formation_1.append(unit_1.units.pop(0)())
if not formation_2:
formation_2.append(unit_2.units.pop(0)())
if unit_2.units:
formation_2.append(unit_2.units.pop(0)())
if not formation_2[0].is_alive:
formation_2.pop(0)
if unit_2.units:
formation_2.append(unit_2.units.pop(0)())
fight(formation_1, formation_2)
return bool(unit_1.units or formation_1)
class Warrior:
def __init__(self, health=50, attack=5, defense=0, vampirism=0, lancer=False):
self.health = health
self.attack = attack
self.defense = defense
self.vampirism = vampirism
self.lancer = lancer
is_alive = property(lambda self: self.health > 0)
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
class Defender(Warrior):
def __init__(self):
super().__init__(health=60, attack=3, defense=2)
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4, vampirism=0.5)
class Lancer(Warrior):
def __init__(self):
super().__init__(attack=6, lancer=True)
def fight(form_1, form_2):
attack_form, defend_form = (form_1, form_2) if type(form_1) == list else ([form_1], [form_2])
while attack_form and defend_form and attack_form[0].is_alive and defend_form[0].is_alive:
defend_form[0].health -= max(attack_form[0].attack - defend_form[0].defense, 0)
attack_form[0].health += max(attack_form[0].attack - defend_form[0].defense, 0) * attack_form[0].vampirism
if type(attack_form[0]) == Lancer and len(defend_form) == 2:
defend_form[1].health -= max(attack_form[0].attack - defend_form[0].defense, 0) * 0.5
attack_form, defend_form = defend_form, attack_form
return None if type(form_1) == list else form_1.is_alive
Aug. 12, 2018
Comments: