Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
O( `_´)乂(`_´ )O solution in Clear category for The Defenders by vmiimu
class Warrior(object):
def __init__(self):
self.health = 50
self.attack = 5
@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):
self.health = 60
self.attack = 3
self.defense = 2
class Rookie(Warrior):
def __init__(self):
super().__init__()
self.health = 50
self.attack = 1
class Army(object):
def __init__(self):
self.fighters = []
@property
def is_armed(self):
return any([i.is_alive for i in self.fighters])
def add_units(self, fighter, qty):
self.fighters += [fighter() for i in range(qty)]
class Battle(object):
def fight(self, a, b):
while a.is_armed and b.is_armed:
if fight(a.fighters[-1], b.fighters[-1]):
b.fighters.pop()
else:
a.fighters.pop()
return a.is_armed
def fight(a, b):
while a.is_alive and b.is_alive:
if type(b) is Defender:
if b.defense < a.attack:
b.health -= (a.attack - b.defense)
else:
b.health -= a.attack
if b.is_alive:
if type(a) is Defender:
if a.defense < b.attack:
a.health -= (b.attack - a.defense)
else:
a.health -= b.attack
return a.is_alive
Jan. 22, 2019