Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Defenders by alexandrov.net
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
self.defense = 0
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
class Defender(Warrior):
def __init__(self):
super().__init__()
self.attack = 3
self.health = 60
self.defense = 2
def damage(unit_1, unit_2):
return unit_1.attack - unit_2.defense if unit_1.attack - unit_2.defense > 0 else 0
def fight(unit_1, unit_2):
while unit_1.is_alive and unit_2.is_alive:
unit_2.health -= damage(unit_1, unit_2)
if unit_2.is_alive:
unit_1.health -= damage(unit_2, unit_1)
return unit_1.is_alive
class Army():
def __init__(self):
self.stuff=[]
def add_units(self, unit, amount):
for i in range(amount):
self.stuff.append(unit())
@property
def is_alive(self) :
return self.stuff != []
class Battle():
def fight(self, army1, army2):
while army1.is_alive and army2.is_alive:
if fight(army1.stuff[0],army2.stuff[0]):
del army2.stuff[0]
else:
del army1.stuff[0]
return army1.is_alive
Oct. 15, 2018