Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Weapons by Jaime
# Taken from mission Straight Fight
class Warrior:
health = 50
attack = 5
maxhealth = 50
@property
def is_alive(self):
return self.health > 0
def ataque(self, enemy):
enemy[0].defensa(self.attack)
def defensa(self, hit):
self.health -= hit
def max_health(self):
if self.health > self.maxhealth: self.health = self.maxhealth
def verify_healer(self, friends):
if len(friends) >= 2:
if isinstance(friends[1], Healer):
friends[1].heal(self)
self.max_health()
def equip_weapon(self,weapon_name):
self.health+=weapon_name.health
self.maxhealth+=weapon_name.health
self.attack+=weapon_name.attack
if hasattr(self,'defense'):
self.defense+=weapon_name.defense
if self.defense<0: self.defense=0
if hasattr(self,'vampirism'):
self.vampirism+=weapon_name.vampirism
if self.vampirism<0: self.vampirism=0
if hasattr(self,'heal_power'):
self.heal_power+=weapon_name.heal_power
if self.heal_power<0: self.heal_power=0
if self.health<0: self.health=0
if self.attack<0: self.attack=0
if self.maxhealth<0: self.maxhealth=0
class Knight(Warrior):
def __init__(self):
self.attack = 7
class Defender(Warrior):
def __init__(self):
self.health = 60
self.attack = 3
self.defense = 2
self.maxhealth = 60
def defensa(self, hit):
if hit > self.defense: self.health -= (hit - self.defense)
class Vampire(Warrior):
def __init__(self):
self.health = 40
self.attack = 4
self.vampirism = 50
self.maxhealth = 40
def ataque(self, enemy):
if hasattr(enemy[0], 'defense') and self.attack > enemy[0].defense:
self.health += int((self.attack - enemy[0].defense) * (self.vampirism/100))
else:
self.health += int(self.attack * (self.vampirism/100))
self.max_health()
enemy[0].defensa(self.attack)
class Lancer(Warrior):
def __init__(self):
self.attack = 6
def ataque(self, enemy):
if len(enemy) >= 2: enemy[1].defensa(self.attack * 0.5)
enemy[0].defensa(self.attack)
class Healer(Warrior):
def __init__(self):
self.health = 60
self.attack = 0
self.maxhealth = 60
self.heal_power = 2
def heal(self, friend):
friend.health = friend.health + self.heal_power
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):
Weapon.__init__(self,5,2)
class Shield(Weapon):
def __init__(self):
Weapon.__init__(self,20,-1,2)
class GreatAxe(Weapon):
def __init__(self):
Weapon.__init__(self,-15,5,-2,10)
class Katana(Weapon):
def __init__(self):
Weapon.__init__(self,-20,6,-5,50)
class MagicWand(Weapon):
def __init__(self):
Weapon.__init__(self,health=30,attack=3,heal_power=3)
def fight(unit1, unit2):
uno = unit1
dos = unit2
if type(unit1) != list:
uno = [unit1]
dos = [unit2]
if uno[0].is_alive and dos[0].is_alive:
uno[0].ataque(dos)
uno[0].verify_healer(uno)
fight(dos, uno)
return uno[0].is_alive
class Army:#Es ejército, no armada ni arma.
def __init__(self):
self.units = []
def add_units(self, unit, number):
for e in list(range(number)): self.units += [unit()]
class Battle:
def fight(self, army1, army2):
if army1.units and army2.units:
if fight(army1.units, army2.units):
army2.units.pop(0)
else:
army1.units.pop(0)
return self.fight(army1, army2)
else:
return bool(army1.units)
def straight_fight(self, army1, army2):
if army1.units and army2.units:
if len(army1.units) >= len(army2.units):
for x, y in zip(army1.units[:], army2.units[:]):
if fight(x, y):
army2.units.remove(y)
else:
army1.units.remove(x)
else:
for x, y in zip(army2.units[:], army1.units[:]):
if fight(y, x):
army2.units.remove(x)
else:
army1.units.remove(y)
return self.straight_fight(army1, army2)
else:
return bool(army1.units)
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!")
Aug. 19, 2018
Comments: