Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Army Battles by tokiojapan55
class Warrior:
def __init__(self, hp=50, atk=5):
self._hp = hp
self._atk = atk
@property
def is_alive(self):
return self._hp > 0
def attackedBy(self, unit):
self._hp -= unit._atk
return self.is_alive
class Knight(Warrior):
def __init__(self, hp=50, atk=7):
super().__init__(hp, atk)
class Army:
def __init__(self):
self.units = list()
def add_units(self, cls, count):
for i in range(count):
self.units.append(cls())
def next(self):
for unit in self.units:
if unit.is_alive:
return unit
return None
class Battle:
def __init__(self):
pass
def fight(self, army_1, army_2):
while True:
unit_1 = army_1.next()
if unit_1 == None:
return False
unit_2 = army_2.next()
if unit_2 == None:
return True
fight(unit_1, unit_2)
def fight(unit_1, unit_2):
while True:
if not unit_2.attackedBy(unit_1):
return True
if not unit_1.attackedBy(unit_2):
return False
return not unit_2.is_alive
June 23, 2020