Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
hasattr, getattr, setattr solution in Clear category for The Weapons by mAzrunnr
# Taken from mission Straight Fight
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.health = health
self.attack = attack
@property
def is_alive(self):
return self.health > 0
def hit(self, other):
other.loss(self.attack)
def damage(self, attack):
return attack
def loss(self, attack):
self.health -= self.damage(attack)
def equip_weapon(self, weapon):
for attr in ['health', 'attack', 'defense', 'vampirism', 'heal_power']:
if hasattr(self, attr) and hasattr(weapon, attr):
setattr(self, attr, max(0, getattr(self, attr) + getattr(weapon, attr, 0)))
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
def damage(self, attack):
return max(0, attack - self.defense)
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4)
self.vampirism = 50
def hit(self, other):
super().hit(other)
self.health += other.damage(self.attack) * self.vampirism // 100
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, other):
other.health += self.heal_power
def fight(unit_1, unit_2):
while 1:
unit_1.hit(unit_2)
if unit_2.health <= 0:
return True
unit_2.hit(unit_1)
if unit_1.health <= 0:
return False
class Army:
def __init__(self):
self.units = []
def add_units(self, unit_class, count):
for _ in range(count):
self.units.append(unit_class())
@property
def first_alive_unit(self):
for unit in self.units:
if unit.is_alive:
return unit
def next_unit(self, unit):
i = self.units.index(unit)
if i + 1 < len(self.units):
return self.units[i + 1]
@property
def is_alive(self):
return self.first_alive_unit is not None
@property
def alive_units(self):
return [u for u in self.units if u.is_alive]
class Battle:
@staticmethod
def hit(army_1, army_2):
unit_1 = army_1.first_alive_unit
unit_2 = army_2.first_alive_unit
unit_22 = army_2.next_unit(unit_2)
unit_1.hit(unit_2)
if unit_22 and isinstance(unit_1, Lancer):
unit_22.loss(unit_1.attack // 2)
unit_12 = army_1.next_unit(unit_1)
if unit_12 and isinstance(unit_12, Healer):
unit_12.heal(unit_1)
@classmethod
def fight(cls, army_1, army_2):
while army_1.is_alive and army_2.is_alive:
unit_1 = army_1.first_alive_unit
unit_2 = army_2.first_alive_unit
while 1:
cls.hit(army_1, army_2)
if unit_2.health <= 0:
break
cls.hit(army_2, army_1)
if unit_1.health <= 0:
break
return army_1.is_alive
@classmethod
def straight_fight(cls, army_1, army_2):
while army_1.is_alive and army_2.is_alive:
for unit_1, unit_2 in zip(army_1.alive_units, army_2.alive_units):
fight(unit_1, unit_2)
return army_1.is_alive
if __name__ == "__main__":
# These "asserts" using only for self-checking and not necessary for auto-testing
ogre = Warrior()
lancelot = Knight()
richard = Defender()
eric = Vampire()
freelancer = Lancer()
priest = Healer()
sword = Sword()
shield = Shield()
axe = GreatAxe()
katana = Katana()
wand = MagicWand()
super_weapon = Weapon(50, 10, 5, 150, 8)
ogre.equip_weapon(sword)
ogre.equip_weapon(shield)
ogre.equip_weapon(super_weapon)
lancelot.equip_weapon(super_weapon)
richard.equip_weapon(shield)
eric.equip_weapon(super_weapon)
freelancer.equip_weapon(axe)
freelancer.equip_weapon(katana)
priest.equip_weapon(wand)
priest.equip_weapon(shield)
ogre.health == 125
lancelot.attack == 17
richard.defense == 4
eric.vampirism == 200
freelancer.health == 15
priest.heal_power == 5
fight(ogre, eric) == False
fight(priest, richard) == False
fight(lancelot, freelancer) == True
my_army = Army()
my_army.add_units(Knight, 1)
my_army.add_units(Lancer, 1)
enemy_army = Army()
enemy_army.add_units(Vampire, 1)
enemy_army.add_units(Healer, 1)
my_army.units[0].equip_weapon(axe)
my_army.units[1].equip_weapon(super_weapon)
enemy_army.units[0].equip_weapon(katana)
enemy_army.units[1].equip_weapon(wand)
battle = Battle()
battle.fight(my_army, enemy_army) == True
print("Coding complete? Let's try tests!")
Sept. 10, 2023
Comments: