Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
EAFP - IndexError solution in Creative category for Army Battles by suic
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
def fight(u1, u2):
while u1.is_alive and u2.is_alive:
u2.health -= u1.attack
if u2.is_alive:
u1.health -= u2.attack
return u1.is_alive
class Army:
def __init__(self):
self._units = []
def __getitem__(self, index):
return self._units[index]
def __len__(self):
return len(self._units)
def add_units(self, unit, count):
self._units.extend(unit() for _ in range(count))
class Battle:
def fight(self, a1, a2):
i1 = i2 = 0
try:
while True:
u1, u2 = a1[i1], a2[i2]
fight(u1, u2)
i1 += not u1.is_alive
i2 += not u2.is_alive
except IndexError:
return any(u.is_alive for u in reversed(a1))
May 5, 2019