Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Weapons by tokiojapan55
# Taken from mission Straight Fight
# Taken from mission The Healers
# Taken from mission The Lancers
# Taken from mission The Vampires
# Taken from mission The Defenders
# Taken from mission Army Battles
# Taken from mission The Warriors
def get_arg(index, keyword, default, *args, **kwargs):
result = default
if keyword in kwargs:
result = kwargs[keyword]
if args and index < len(args):
result = args[index]
return result
def set_kwargs(kwargs, param):
for p in param:
if p not in kwargs:
kwargs[p] = param[p]
class Warrior:
def __init__(self, *args, **kwargs):
#(hp=50, atk=5, dfs=0, vamp=0):
self.health = get_arg(0, "health", 50, *args, **kwargs)
self.max_hp = self.health
self.attack = get_arg(1, "attack", 5, *args, **kwargs)
self.defense = get_arg(2, "defense", 0, *args, **kwargs)
self.vampirism = get_arg(3, "vampirism", 0, *args, **kwargs)
self.lance = get_arg(4, "lance", 0, *args, **kwargs)
self.heal_power = get_arg(5, "heal_power", 0, *args, **kwargs)
self.army = get_arg(6, "army", None, *args, **kwargs)
@property
def is_alive(self):
return self.health > 0
def attackedBy(self, unit, behind=True):
if unit.attack > self.defense:
damage = unit.attack - self.defense
if damage > 0:
self.health -= damage
unit.health += int(damage * unit.vampirism / 100)
if behind and self.behind() != None:
damage = int(damage * unit.lance / 100) - self.behind().defense
if damage > 0:
self.behind().health -= damage
if behind and unit.behind() != None and unit.behind().heal_power > 0:
unit.behind().heal(unit)
return self.is_alive
def behind(self):
if self.army == None:
return None
for i,u in enumerate(self.army.units):
if u == self and i < len(self.army.units)-1:
return self.army.units[i+1]
return None
def equip_weapon(self, weapon):
if self.health > 0:
self.health = max(0, self.health+weapon.health)
self.max_hp = self.health
if self.attack > 0:
self.attack = max(0, self.attack+weapon.attack)
if self.defense > 0:
self.defense = max(0, self.defense+weapon.defense)
if self.vampirism > 0:
self.vampirism = max(0, self.vampirism+weapon.vampirism)
if self.heal_power > 0:
self.heal_power = max(0, self.heal_power+weapon.heal_power)
class Knight(Warrior):
def __init__(self, *args, **kwargs):
set_kwargs(kwargs, {'attack': 7})
super().__init__(*args, **kwargs)
class Defender(Warrior):
def __init__(self, *args, **kwargs):
set_kwargs(kwargs, {'health': 60, 'attack': 3, 'defense': 2})
super().__init__(*args, **kwargs)
class Vampire(Warrior):
def __init__(self, *args, **kwargs):
set_kwargs(kwargs, {'health': 40, 'attack': 4, 'vampirism': 50})
super().__init__(*args, **kwargs)
class Lancer(Warrior):
def __init__(self, *args, **kwargs):
set_kwargs(kwargs, {'health': 50, 'attack': 6, 'lance': 50})
super().__init__(*args, **kwargs)
class Healer(Warrior):
def __init__(self, *args, **kwargs):
set_kwargs(kwargs, {'health': 60, 'attack': 0, 'heal_power': 2})
super().__init__(*args, **kwargs)
def heal(self, unit):
if self.is_alive and unit.is_alive:
unit.health = min(unit.max_hp, unit.health + self.heal_power)
class Rookie(Warrior):
def __init__(self, *args, **kwargs):
set_kwargs(kwargs, {'attack': 7})
super().__init__(*args, **kwargs)
class Weapon:
def __init__(self, *args, **kwargs):
self.health = get_arg(0, "health", 0, *args, **kwargs)
self.attack = get_arg(1, "attack", 0, *args, **kwargs)
self.defense = get_arg(2, "defense", 0, *args, **kwargs)
self.vampirism = get_arg(3, "vampirism", 0, *args, **kwargs)
# weapon not has lance parameter
# self.lance = get_arg(4, "lance", 0, *args, **kwargs)
self.heal_power = get_arg(4, "heal_power", 0, *args, **kwargs)
class Sword(Weapon):
def __init__(self, *args, **kwargs):
set_kwargs(kwargs, {'health': 5, 'attack': 2})
super().__init__(*args, **kwargs)
class Shield(Weapon):
def __init__(self, *args, **kwargs):
set_kwargs(kwargs, {'health': 20, 'attack': -1, 'defense': 2})
super().__init__(*args, **kwargs)
class GreatAxe(Weapon):
def __init__(self, *args, **kwargs):
set_kwargs(kwargs, {'health': -15, 'attack': 5, 'defense': -2, 'vampirism': 10})
super().__init__(*args, **kwargs)
class Katana(Weapon):
def __init__(self, *args, **kwargs):
set_kwargs(kwargs, {'health': -20, 'attack': 6, 'defense': -5, 'vampirism': 50})
super().__init__(*args, **kwargs)
class MagicWand(Weapon):
def __init__(self, *args, **kwargs):
set_kwargs(kwargs, {'health': 30, 'attack': 3, 'heal_power': 3})
super().__init__(*args, **kwargs)
class Army:
def __init__(self):
self.units = list()
def add_units(self, cls, count):
for i in range(count):
# self.units.append(cls(army=self))
_cls = cls()
_cls.army = self
self.units.append(_cls)
def next(self):
for unit in self.units:
if unit.is_alive:
return unit
return None
def teams(self):
return [unit for unit in self.units if unit.is_alive]
class Battle:
def __init__(self):
pass
def fight(self, army_1, army_2):
while True:
unit_1 = army_1.next()
if unit_1 == None:
return False
unit_2 = army_2.next()
if unit_2 == None:
return True
fight(unit_1, unit_2)
def straight_fight(self, army_1, army_2):
while True:
team_1 = army_1.teams()
if not team_1:
return False
team_2 = army_2.teams()
if not team_2:
return True
for unit_1,unit_2 in zip(team_1,team_2):
fight(unit_1, unit_2, behind=False)
def fight(unit_1, unit_2, behind=True):
while True:
if not unit_2.attackedBy(unit_1, behind):
return True
if not unit_1.attackedBy(unit_2, behind):
return False
return not unit_2.is_alive
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
#fight tests
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
bob = Defender()
mike = Knight()
rog = Warrior()
lancelot = Defender()
eric = Vampire()
adam = Vampire()
richard = Defender()
ogre = Warrior()
freelancer = Lancer()
vampire = Vampire()
priest = Healer()
assert fight(chuck, bruce) == True
assert fight(dave, carl) == False
assert chuck.is_alive == True
assert bruce.is_alive == False
assert carl.is_alive == True
assert dave.is_alive == False
assert fight(carl, mark) == False
assert carl.is_alive == False
assert fight(bob, mike) == False
assert fight(lancelot, rog) == True
assert fight(eric, richard) == False
assert fight(ogre, adam) == True
assert fight(freelancer, vampire) == True
assert freelancer.is_alive == True
assert freelancer.health == 14
priest.heal(freelancer)
assert freelancer.health == 16
#battle tests
my_army = Army()
my_army.add_units(Defender, 2)
my_army.add_units(Healer, 1)
my_army.add_units(Vampire, 2)
my_army.add_units(Lancer, 2)
my_army.add_units(Healer, 1)
my_army.add_units(Warrior, 1)
enemy_army = Army()
enemy_army.add_units(Warrior, 2)
enemy_army.add_units(Lancer, 4)
enemy_army.add_units(Healer, 1)
enemy_army.add_units(Defender, 2)
enemy_army.add_units(Vampire, 3)
enemy_army.add_units(Healer, 1)
army_3 = Army()
army_3.add_units(Warrior, 1)
army_3.add_units(Lancer, 1)
army_3.add_units(Healer, 1)
army_3.add_units(Defender, 2)
army_4 = Army()
army_4.add_units(Vampire, 3)
army_4.add_units(Warrior, 1)
army_4.add_units(Healer, 1)
army_4.add_units(Lancer, 2)
army_5 = Army()
army_5.add_units(Warrior, 10)
army_6 = Army()
army_6.add_units(Warrior, 6)
army_6.add_units(Lancer, 5)
battle = Battle()
assert battle.fight(my_army, enemy_army) == False
assert battle.fight(army_3, army_4) == True
assert battle.straight_fight(army_5, army_6) == False
print("Coding complete? Let's try tests!")
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!")
March 25, 2020