Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
heavy_inventory solution in Clear category for The Weapons by Tical_1000
# Taken from mission Straight Fight
# Taken from mission The Healers
class Warrior:
def __init__(self, hlth=50, attk=5, defn=0, vamp=0, heal=0):
self.health = hlth
self.attack = attk
self.defense = defn
self.vampirism = vamp
self.heal_power = heal
self.max_h = self.health
@property
def is_alive(self):
return True if self.health > 0 else False
def equip_weapon(self, wp):
self.health += wp.health
self.attack += wp.attack
self.defense += wp.defense if self.defense > 0 else 0
self.vampirism += wp.vampirism/100 if self.vampirism > 0 else 0
self.heal_power += wp.heal_power if self.heal_power > 0 else 0
self.max_h = self.health
class Knight(Warrior):
def __init__(self):
super().__init__(attk=7)
class Defender(Warrior):
def __init__(self):
super().__init__(hlth=60, attk=3, defn=2)
class Vampire(Warrior):
def __init__(self):
super().__init__(hlth=40, attk=4, vamp=0.5)
class Lancer(Warrior):
def __init__(self):
super().__init__(attk=6)
class Healer(Warrior):
def __init__(self):
super().__init__(hlth=60, attk=0, heal=2)
def heal(self, other):
if isinstance(other, Defender):
maxh = 60
elif isinstance(other, Vampire):
maxh = 40
elif isinstance(other, Healer):
maxh = 60
else:
maxh = 50
other.health = min(other.health + self.heal_power, maxh)
class Weapon:
def __init__(self, hlth=0, attk=0, defn=0, vamp=0, heal=0):
self.health = hlth
self.attack = attk
self.defense = defn
self.vampirism = vamp
self.heal_power = heal
class Sword(Weapon):
def __init__(self):
super().__init__(hlth=5, attk=2)
class Shield(Weapon):
def __init__(self):
super().__init__(hlth=20, attk=-1, defn=2)
class GreatAxe(Weapon):
def __init__(self):
super().__init__(hlth=-15, attk=5, defn=-2, vamp=10)
class Katana(Weapon):
def __init__(self):
super().__init__(hlth=-20, attk=6, defn=-5, vamp=50)
class MagicWand(Weapon):
def __init__(self):
super().__init__(hlth=30, attk=3, heal=3)
def fight(one, two, o_next=None, t_next=None):
if not two.is_alive:
return True
elif not one.is_alive:
return False
att_1_2 = max(0, one.attack - two.defense)
att_2_1 = max(0, two.attack - one.defense)
while True:
two.health -= att_1_2
one.health += min(one.max_h, int(att_1_2 * one.vampirism))
if isinstance(o_next, Healer):
o_next.heal(one)
if isinstance(one, Lancer) and t_next:
t_next.health -= max(0, att_1_2 * 0.5 - t_next.defense)
if two.health <= 0:
two.health = 0
return True
one.health -= att_2_1
two.health += min(two.max_h, int(att_2_1 * two.vampirism))
if isinstance(t_next, Healer):
t_next.heal(two)
if isinstance(two, Lancer) and o_next:
o_next.health -= max(0, att_2_1 * 0.5 - o_next.defense)
if one.health <= 0:
one.health = 0
return False
class Army:
def __init__(self):
self.units = []
def add_units(self, fighter, unts):
for i in range(unts):
self.units.append(fighter())
class Battle:
def __init__(self):
pass
def fight(self, army_1, army_2):
while len(army_1.units) > 0 and len(army_2.units) > 0:
cont = [army_1.units[0], army_2.units[0], None, None]
if len(army_1.units) > 1:
cont[2] = army_1.units[1]
if len(army_2.units) > 1:
cont[3] = army_2.units[1]
if fight(*cont):
army_2.units.pop(0)
else:
army_1.units.pop(0)
return False if len(army_1.units) == 0 else True
def straight_fight(self, army_1, army_2):
while army_1.units and army_2.units:
duels = min(len(army_1.units), len(army_2.units))
surv_1, surv_2 = [], []
for i in range(duels):
one, two = army_1.units.pop(0), army_2.units.pop(0)
if fight(one, two):
surv_1.append(one)
else:
surv_2.append(two)
surv_1.extend(army_1.units)
surv_2.extend(army_2.units)
army_1.units, army_2.units = surv_1, surv_2
return False if not army_1.units else True
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!")
May 28, 2019