Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
keyword arguments solution in Clear category for The Weapons by David_Jones
class Weapon:
def __init__(
self, health=0, attack=0, defense=0, vampirism=0, heal_power=0
):
self.health = health
self.attack = attack
self.defense = defense
self.vampirism = vampirism
self.heal_power = heal_power
class Sword(Weapon):
def __init__(self):
super().__init__(health=5, attack=2)
class Shield(Weapon):
def __init__(self):
super().__init__(health=20, attack=-1, defense=2)
class GreatAxe(Weapon):
def __init__(self):
super().__init__(health=-15, attack=5, defense=-2, vampirism=10)
class Katana(Weapon):
def __init__(self):
super().__init__(health=-20, attack=6, defense=-5, vampirism=50)
class MagicWand(Weapon):
def __init__(self):
super().__init__(health=30, attack=3, heal_power=3)
class Warrior:
def __init__(self, health=50, attack=5):
self.max_health = self.health = health
self.attack = attack
@property
def is_alive(self):
return self.health > 0
def equip_weapon(self, weapon):
self.health += weapon.health
self.attack += weapon.attack
if type(self) is Defender:
self.defense += weapon.defense
elif type(self) is Vampire:
self.vampirism += weapon.vampirism
elif type(self) is Healer:
self.attack = 0
self.heal_power += weapon.heal_power
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
class Defender(Warrior):
def __init__(self):
super().__init__(health=60, attack=3)
self.defense = 2
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4)
self.vampirism = 50
class Lancer(Warrior):
def __init__(self):
super().__init__(attack=6)
class Healer(Warrior):
def __init__(self):
super().__init__(health=60, attack=0)
self.heal_power = 2
def heal(self, ally):
ally.health = min(ally.max_health, ally.health + self.heal_power)
class Army:
def __init__(self):
self.units = []
def add_units(self, unit_type, n):
self.units.extend(unit_type() for _ in range(n))
class Battle:
def fight(self, army_1, army_2):
while army_1.units:
if fight(army_1.units[0], army_2.units[0], army_1, army_2):
del army_2.units[0]
else:
del army_1.units[0]
if not army_2.units:
return True
return False
def straight_fight(self, army_1, army_2):
while army_1.units and army_2.units:
for unit_1, unit_2 in zip(army_1.units, army_2.units):
fight(unit_1, unit_2)
army_1.units = [unit for unit in army_1.units if unit.is_alive]
army_2.units = [unit for unit in army_2.units if unit.is_alive]
return bool(army_1.units)
def fight(unit_1, unit_2, army_1=None, army_2=None):
def hit(unit_1, army_1, unit_2, army_2):
if type(unit_2) is Defender:
damage = max(0, unit_1.attack - unit_2.defense)
else:
damage = unit_1.attack
unit_2.health -= damage
if type(unit_1) is Vampire:
unit_1.health += int(damage * unit_1.vampirism / 100)
elif (
type(unit_1) is Lancer
and army_2 is not None and len(army_2.units) > 1
):
army_2.units[1].health -= 0.5 * damage
if (
army_1 is not None and len(army_1.units) > 1
and type(army_1.units[1]) is Healer
):
army_1.units[1].heal(unit_1)
while unit_1.is_alive:
hit(unit_1, army_1, unit_2, army_2)
if not unit_2.is_alive:
return True
hit(unit_2, army_2, unit_1, army_1)
return False
June 20, 2019
Comments: