Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First, I think it is clear :) solution in Clear category for Army Battles by ArchTauruS
class Warrior:
health = 50
attack = 5
@property
def is_alive(self):
return self.health > 0
def __sub__(self, other):
self.health -= other.attack if other.is_alive else 0
return self
class Knight(Warrior):
attack = 7
def fight(soldier1, soldier2):
while soldier1.is_alive and soldier2.is_alive:
soldier2 -= soldier1
soldier1 -= soldier2
return soldier1.is_alive
class Army:
def __init__(self):
self.troop = []
def add_units(self, unit, number):
self.troop.extend(unit() for _ in range(number))
def __sub__(self, other):
if self.troop and other.troop:
if fight(self.troop[0], other.troop[0]):
other.troop = other.troop[1:]
else:
self.troop = self.troop[1:]
return self
class Battle:
@staticmethod
def fight(amy1, amy2):
while amy1.troop and amy2.troop:
amy1 -= amy2
return amy1.troop != []
Oct. 31, 2018