Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
other[:] = (x for x in other if x.is_alive) solution in Clear category for The Lancers by flpo
class Warrior:
health = 50
attack = 5
defense = 0
@property
def is_alive(self):
return self.health > 0
def turn_attack(self, other, *army):
if other.defense < self.attack:
other.health -= self.attack - other.defense
self.custom_attack(other, *army)
def custom_attack(self, *_): pass
class Knight(Warrior):
attack = 7
class Defender(Warrior):
health = 60
attack = 3
defense = 2
class Vampire(Warrior):
health = 40
attack = 4
def custom_attack(self, other, *army):
self.health += round((self.attack - other.defense) / 2, 0)
class Lancer(Warrior):
health = 50
attack = 6
def custom_attack(self, _, *army):
if not army: return
self.attack //= 2
self.turn_attack(army[0])
self.attack *= 2
def fight(unit_1, unit_2):
while unit_1.is_alive and unit_2.is_alive:
unit_1.turn_attack(unit_2)
if not unit_2.is_alive: return True
unit_2.turn_attack(unit_1)
return False
class Army(list):
def add_units(self, unit, n):
for _ in range(n): self.append(unit())
def fight(self, other):
while self and other:
killed = self.attack(other)
if not other: return True
if not killed: other.attack(self)
return False
def attack(self, other):
self[0].turn_attack(*other)
res = not other[0].is_alive
other[:] = (x for x in other if x.is_alive)
return res
class Battle:
def fight(self, a1, a2):
return a1.fight(a2)
Aug. 13, 2018