Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Army Battles by wyf.141060
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
self.is_alive = True
class Knight(Warrior):
def __init__(self):
Warrior.__init__(self)
self.attack = 7
class Army:
def __init__(self):
self.units = []
def add_units(self, unit, num):
[self.units.append(unit()) for i in range(num)]
class Battle:
def fight(self, army1, army2):
if not len(army1.units):
return False
if not len(army2.units):
return True
solider1 = army1.units.pop(0)
solider2 = army2.units.pop(0)
while True:
score = fight(solider1, solider2)
if score:
if not len(army2.units):
return True
solider2 = army2.units.pop(0)
else:
if not len(army1.units):
return False
solider1 = army1.units.pop(0)
def fight(unit_1, unit_2):
while True:
unit_2.health -= unit_1.attack
if unit_2.health <= 0:
unit_2.is_alive = False
return True
unit_1.health -= unit_2.attack
if unit_1.health <= 0:
unit_1.is_alive = False
return False
Dec. 10, 2018