Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
new_series solution in Clear category for Army Battles by MBM_1607
class Warrior:
health: int = 50
is_alive: bool = True
damage: int = 5
@property
def is_alive(self):
return self.health > 0
def attack(self, target):
target.health -= self.damage
class Knight(Warrior):
damage: int = 7
class Army:
def __init__(self):
self.units = []
def add_units(self, unit, numbers):
for _ in range(numbers):
self.units.append(unit())
def units_check(self):
for unit in self.units:
if not unit.is_alive:
self.units.remove(unit)
class Battle:
@staticmethod
def fight(army_1, army_2):
while army_1.units and army_2.units:
fight(army_1.units[-1], army_2.units[-1])
for army in (army_1, army_2):
army.units_check()
if not army_2.units and army_1.units:
return True
elif not army_1.units and army_2.units:
return False
def fight(unit_1, unit_2):
while unit_1.is_alive and unit_2.is_alive:
unit_1.attack(unit_2)
if not unit_2.is_alive:
return True
unit_2.attack(unit_1)
if not unit_1.is_alive:
return False
Aug. 14, 2018
Comments: