Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Army Battles by StanislauL
from collections import deque
class Warrior:
def __init__(self):
self.attack = self.set_attack()
self.health = 50
self.is_alive = True
def set_attack(self):
return 5
class Knight(Warrior):
def set_attack(self):
return 7
class Army:
def __init__(self):
self.units = deque()
def add_units(self, unitType, count):
for x in range(count):
self.units.append(unitType())
class Battle:
def fight(self, army1, army2):
while army1.units and army2.units:
if fight(army1.units[0], army2.units[0]):
army2.units.popleft()
else:
army1.units.popleft()
return True if army1.units else False
def fight(unit_1, unit_2):
while True:
unit_2.health-=unit_1.attack
if unit_2.health<=0:
unit_2.health = 0
unit_2.is_alive = False
return True
unit_1.health-=unit_2.attack
if unit_1.health<=0:
unit_1.health = 0
unit_1.is_alive = False
return False
Aug. 27, 2020