Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for The Defenders by David_Jones
class Warrior:
def __init__(self, health=50, attack=5, defense=0):
self.health = health
self.attack = attack
self.defense = defense
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
class Defender(Warrior):
def __init__(self):
super().__init__(health=60, attack=3, defense=2)
def fight(unit_1, unit_2):
while unit_1.is_alive:
unit_2.health -= max(0, unit_1.attack - unit_2.defense)
if not unit_2.is_alive:
return True
unit_1.health -= max(0, unit_2.attack - unit_1.defense)
return False
class Army(list):
def add_units(self, unit_type, n):
self.extend(unit_type() for _ in range(n))
class Battle:
def fight(self, army_1, army_2):
n, m = len(army_1), len(army_2)
i = j = 0
while i < n and j < m:
if fight(army_1[i], army_2[j]):
j += 1
else:
i += 1
return i < n
May 21, 2019