Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Army Battles by kalauroma7997
class Warrior:
def __init__(self, health=50, attack=5):
self.health = health
self.attack = attack
@property
def is_alive(self):
return self.health > 0
def make_damage(self, damage):
self.health -= damage
class Knight(Warrior):
def __init__(self, health=50, attack=7):
super().__init__(health, attack)
def fight(unit1, unit2):
while unit1.is_alive and unit2.is_alive:
unit2.make_damage(unit1.attack)
if not unit1.is_alive or not unit2.is_alive:
break
unit1.make_damage(unit2.attack)
return unit1.is_alive
class Army(object):
def __init__(self):
self.warriors = []
def add_units(self, warrior, amount):
for i in range(amount):
self.warriors.append(warrior())
class Battle(object):
def __init__(self):
pass
def fight(self, army1, army2):
while army1.warriors and army2.warriors:
if fight(army1.warriors[-1], army2.warriors[-1]):
army2.warriors.pop()
else:
army1.warriors.pop()
return bool(army1.warriors)
Aug. 22, 2020