Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
ArmyReordering solution in Clear category for The Warlords by Kouri
# Taken from mission The Weapons
class Warrior:
def __init__(self, health=50, attack=5, equiped_weapon=[]):
self.max_health = health
self.health = health
self.attack = attack
self.equiped_weapons = equiped_weapon
@property
def is_alive(self):
return self.health > 0
def hit(self, other):
other.loss(self.attack)
def loss(self, attack):
self.health -= attack
return attack
def equip_weapon(self, weap):
self.equiped_weapons.append(weap)
for charac in ["health", "attack", "defense", "vampirism", "heal_power"]:
try:
exec("self.%s = max(0, self.%s+weap.%s)"%(charac, charac, charac))
except AttributeError:
continue
self.max_health += weap.health
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 loss(self, attack):
self.health -= max(0, attack - self.defense)
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):
self.health += min(self.max_health, (self.vampirism/100)*other.loss(self.attack))
class Lancer(Warrior):
def __init__(self):
super().__init__(health=50, attack=6)
self.second_attack = 0.5
def second_hit(self, other):
other.loss(self.attack*self.second_attack)
class Healer(Warrior):
def __init__(self):
super().__init__(health=60, attack=0)
self.heal_power = 2
def heal(self, other):
other.health = min(other.max_health, other.health+self.heal_power)
class Warlord(Warrior):
def __init__(self):
super().__init__(health=100, attack=4)
self.defense = 2
def loss(self, attack):
self.health -= max(0, attack - self.defense)
return max(0, attack - self.defense)
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)
def fight(unit_1, unit_2, unit_3=None, unit_4=None):
while 1:
unit_1.hit(unit_2)
if isinstance(unit_1, Lancer) and unit_4:
unit_1.second_hit(unit_4)
if isinstance(unit_3, Healer):
unit_3.heal(unit_1)
if unit_2.health <= 0:
return True
unit_2.hit(unit_1)
if isinstance(unit_2, Lancer) and unit_3:
unit_2.second_hit(unit_3)
if isinstance(unit_4, Healer):
unit_4.heal(unit_2)
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):
if unit_class == Warlord:
if not any([isinstance(unit, Warlord) for unit in self.units]):
self.units.append(unit_class())
break
else:
break
else:
self.units.append(unit_class())
@property
def first_alive_unit(self):
for unit in self.units:
if unit.is_alive:
return unit
@property
def second_alive_unit(self):
self.count = 0
for unit in self.units:
if unit.is_alive:
if self.count < 1:
self.count += 1
else:
return unit
def move_units(self):
if any([isinstance(unit, Warlord) for unit in self.units]):
new_unit_order = []
for unit in self.units:
if isinstance(unit, Lancer) and unit.is_alive:
new_unit_order.append(unit)
if len(new_unit_order)==1:
for healer in self.units:
if isinstance(healer, Healer) and unit.is_alive:
new_unit_order.append(healer)
for unit in self.units:
if not isinstance(unit, Warlord) and not isinstance(unit, Healer) and not isinstance(unit, Lancer) and unit.is_alive:
new_unit_order.append(unit)
if len(new_unit_order)==1:
for healer in self.units:
if isinstance(healer, Healer) and unit.is_alive:
new_unit_order.append(healer)
if len(new_unit_order)==0:
for unit in self.units:
if isinstance(unit, Healer) and unit.is_alive:
new_unit_order.append(unit)
for unit in self.units:
if isinstance(unit, Warlord) and unit.is_alive:
new_unit_order.append(unit)
for unit in self.units:
if not unit.is_alive:
new_unit_order.append(unit)
return new_unit_order
else:
return self.units
@property
def is_alive(self):
return self.first_alive_unit is not None
class Battle:
@staticmethod
def fight(army_1, army_2):
while army_1.is_alive and army_2.is_alive:
army_1.units = army_1.move_units()[:]
army_2.units = army_2.move_units()[:]
fight(army_1.first_alive_unit, army_2.first_alive_unit, army_1.second_alive_unit, army_2.second_alive_unit)
return army_1.is_alive
@staticmethod
def straight_fight(army_1, army_2):
while army_1.is_alive and army_2.is_alive:
army_1.units = army_1.move_units()[:]
army_2.units = army_2.move_units()[:]
alive_units_army_1 = [army_1.units[rank1] for rank1 in range(len(army_1.units)) if army_1.units[rank1].is_alive]
alive_units_army_2 = [army_2.units[rank2] for rank2 in range(len(army_2.units)) if army_2.units[rank2].is_alive]
for rank in range(min((len(alive_units_army_1), len(alive_units_army_2)))):
fight(alive_units_army_1[rank], alive_units_army_2[rank])
return army_1.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()
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
#battle tests
my_army = Army()
my_army.add_units(Defender, 2)
my_army.add_units(Vampire, 2)
my_army.add_units(Warrior, 1)
enemy_army = Army()
enemy_army.add_units(Warrior, 2)
enemy_army.add_units(Defender, 2)
enemy_army.add_units(Vampire, 3)
army_3 = Army()
army_3.add_units(Warrior, 1)
army_3.add_units(Defender, 4)
army_4 = Army()
army_4.add_units(Vampire, 3)
army_4.add_units(Warrior, 2)
battle = Battle()
assert battle.fight(my_army, enemy_army) == False
assert battle.fight(army_3, army_4) == True
print("Coding complete? Let's try tests!")
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()
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
#battle tests
my_army = Army()
my_army.add_units(Defender, 2)
my_army.add_units(Vampire, 2)
my_army.add_units(Lancer, 4)
my_army.add_units(Warrior, 1)
enemy_army = Army()
enemy_army.add_units(Warrior, 2)
enemy_army.add_units(Lancer, 2)
enemy_army.add_units(Defender, 2)
enemy_army.add_units(Vampire, 3)
army_3 = Army()
army_3.add_units(Warrior, 1)
army_3.add_units(Lancer, 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(Lancer, 2)
battle = Battle()
assert battle.fight(my_army, enemy_army) == True
assert battle.fight(army_3, army_4) == False
print("Coding complete? Let's try tests!")
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)
battle = Battle()
assert battle.fight(my_army, enemy_army) == False
assert battle.fight(army_3, army_4) == True
print("Coding complete? Let's try tests!")
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!")
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
ronald = Warlord()
heimdall = Knight()
fight(heimdall, ronald) == False
my_army = Army()
my_army.add_units(Warlord, 1)
my_army.add_units(Warrior, 2)
my_army.add_units(Lancer, 2)
my_army.add_units(Healer, 2)
enemy_army = Army()
enemy_army.add_units(Warlord, 3)
enemy_army.add_units(Vampire, 1)
enemy_army.add_units(Healer, 2)
enemy_army.add_units(Knight, 2)
my_army.move_units()
enemy_army.move_units()
type(my_army.units[0]) == Lancer
type(my_army.units[1]) == Healer
type(my_army.units[-1]) == Warlord
type(enemy_army.units[0]) == Vampire
type(enemy_army.units[-1]) == Warlord
type(enemy_army.units[-2]) == Knight
#6, not 8, because only 1 Warlord per army could be
len(enemy_army.units) == 6
battle = Battle()
battle.fight(my_army, enemy_army) == True
print("Coding complete? Let's try tests!")
Sept. 3, 2018
Comments: