Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
list.append, fight, list.pop solution in Clear category for Army Battles by Olpag
class Warrior:
def __init__(self):
self.health = 50
self.damage = 5
self.is_alive = True
def attack(self, enemy):
enemy.health -= self.damage
if enemy.health < 1:
enemy.is_alive = False
class Knight(Warrior):
def __init__(self):
super().__init__()
self.damage = 7
class Army:
def __init__(self):
self.units = []
def add_units(self, unit, num):
for _ in range(num):
self.units.append(unit())
class Battle:
def fight(self, army1, army2):
while army1.units and army2.units:
if fight(army1.units[0], army2.units[0]):
army2.units.pop(0)
else:
army1.units.pop(0)
return bool(army1.units)
def fight(unit1, unit2):
while unit1.is_alive and unit2.is_alive:
unit1.attack(unit2)
if unit2.is_alive:
unit2.attack(unit1)
return unit1.is_alive
Jan. 23, 2020