Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
for attr in vars(weapon): solution in Clear category for The Weapons by flpo
class Warrior:
health = 50
attack = 5
defense = 0
last_hit = True
@property
def is_alive(self):
return self.health > 0
def turn_attack(self, other, *army):
self.last_hit = False
if other.defense < self.attack:
self.last_hit = True
other.health -= self.attack - other.defense
self.custom_attack(other, *army)
def custom_attack(self, *_): pass
def equip_weapon(self, weapon):
for attr in vars(weapon):
if getattr(self, attr, None):
setattr(self, attr, max(0, (getattr(self, attr) + getattr(weapon, attr))))
class Knight(Warrior):
attack = 7
class Defender(Warrior):
health = 60
attack = 3
defense = 2
class Vampire(Warrior):
health = 40
attack = 4
vampirism = 50
def custom_attack(self, other, *army):
if self.vampirism:
self.health += int((self.attack - other.defense) / (100 / self.vampirism))
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
class Healer(Warrior):
health = 60
attack = 0
heal_power = 2
def heal(self, unit):
unit.health = min(type(unit).health, unit.health + self.heal_power)
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):
units = property(lambda self: self)
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)
if self[0].last_hit and len(self) > 1 and type(self[1]) is Healer:
self[1].heal(self[0])
res = not other[0].is_alive
other.remove_deaths()
return res
def remove_deaths(self):
while self and not self[0].is_alive:
del self[0]
class Battle:
def fight(self, a1, a2):
return a1.fight(a2)
def straight_fight(self, a1, a2):
while a1 and a2:
for s1, s2 in zip(a1, a2):
fight(s1, s2)
a1.remove_deaths()
a2.remove_deaths()
return bool(a1)
class Weapon:
def __init__(self, health, attack, defense, vampirism, heal_power):
vars(self).update(locals())
class Sword(Weapon):
def __init__(self):
super().__init__(5, 2, 0, 0, 0)
class Shield(Weapon):
def __init__(self):
super().__init__(20, -1, 2, 0, 0)
class GreatAxe(Weapon):
def __init__(self):
super().__init__(-15, 5, 2, 10, 0)
class Katana(Weapon):
def __init__(self):
super().__init__(-20, 6, -5, 50, 0)
class MagicWand(Weapon):
def __init__(self):
super().__init__(30, 3, 0, 0, 3)
Aug. 14, 2018
Comments: