Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Army Battles by ssk8
class Warrior:
def __init__(self):
self.attack = 5
self.health = 50
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
def fight(unit_1, unit_2):
belligerents = [unit_1, unit_2]
while all([belligerent.is_alive for belligerent in belligerents]):
belligerents[1].health -= belligerents[0].attack
if belligerents[1].is_alive:
belligerents.reverse()
return unit_1.is_alive
class Army:
def __init__(self):
self.units = []
def add_units(self, warrior_type, warrior_count):
for _ in range(warrior_count):
self.units.append(warrior_type())
class Battle:
def fight(self, army_1, army_2):
belligerents = [army_1.units, army_2.units]
while all(belligerents):
belligerents[int(fight(*[b[-1] for b in belligerents]))].pop()
return bool(army_1.units)
Aug. 7, 2018