Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Warlords by boxdima1
class Warrior:
def __init__(self, health=50, attack=5):
self.health = health
self.attack = attack
self.max_health = health
@property
def is_alive(self):
return self.health > 0
def hit(self, other):
other.loss(self.attack)
def loss(self, attack):
self.health -= self.damage(attack)
def damage(self, attack):
return attack
def equip_weapon(self, weapon):
self.health = max(0, self.health + weapon.health)
self.max_health = max(0, self.max_health + weapon.health)
self.attack = max(0, self.attack + weapon.attack)
class Rookie(Warrior):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.health = 50
self.attack = 1
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, self.damage(attack))
def damage(self, attack):
return attack - self.defense
def equip_weapon(self, weapon):
super().equip_weapon(weapon)
self.defense = max(0, self.defense + weapon.defense)
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4)
self.vampirism = 50
def hit(self, other):
super().hit(other)
health = self.health + other.damage(self.attack) * self.vampirism // 100
self.health = self.max_health if health > self.max_health else health
def equip_weapon(self, weapon):
super().equip_weapon(weapon)
self.vampirism = max(0, self.vampirism + weapon.vampirism)
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, unit):
if unit.health + self.heal_power > unit.max_health:
unit.health = unit.max_health
else:
unit.health += self.heal_power
def equip_weapon(self, weapon):
super().equip_weapon(weapon)
self.heal_power = max(0, self.heal_power + weapon.heal_power)
class Warlord(Defender):
def __init__(self):
Defender.__init__(self)
self.health = 100
self.attack = 4
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):
while unit_1.is_alive and unit_2.is_alive:
unit_1.hit(unit_2)
if unit_2.is_alive:
unit_2.hit(unit_1)
return unit_1.is_alive
class Army:
def __init__(self):
self.units = []
def add_units(self, unit_type, amount):
self.units += [unit_type() for _ in range(amount)]
def pop_dead(self):
for unit in self.units[:]:
if not unit.is_alive:
self.units.remove(unit)
def next_unit(self, unit):
i = self.units.index(unit)
if i + 1 < len(self.units):
return self.units[i + 1]
@property
def alive_units(self):
is_alive = [unit for unit in self.units if unit.is_alive]
return is_alive
@property
def is_alive(self):
return self.units != []
def count_warlords(self):
count = sum(isinstance(unit, Warlord) for unit in self.units)
for idx, unit in enumerate(self.units):
if isinstance(unit, Warlord):
self.units.append(self.units.pop(idx))
break
for unit in self.units[:]:
if count == 1:
break
if isinstance(unit, Warlord):
self.units.remove(unit)
count -= 1
def move_units(self):
self.count_warlords()
warrior_idx = 0
healer_idx = 1
flag = False
for idx, unit in enumerate(self.units[:]):
if isinstance(unit, Lancer) and unit.is_alive:
self.units.insert(warrior_idx, self.units.pop(idx))
warrior_idx += 1
flag = True
if not flag:
for idx, unit in enumerate(self.units[:]):
if not isinstance(unit, (Lancer, Warlord, Healer)) and unit.is_alive:
self.units.insert(warrior_idx, self.units.pop(idx))
warrior_idx += 1
flag = True
if flag:
for idx, unit in enumerate(self.units[:]):
if isinstance(unit, Healer) and unit.is_alive:
self.units.insert(healer_idx, self.units.pop(idx))
healer_idx += 1
def __getitem__(self, item):
return self.units[item]
def __iter__(self):
return self.units.__iter__()
class Battle:
@staticmethod
def hit(unit_1, unit_2, army_def):
unit_3 = army_def.next_unit(unit_2)
unit_1.hit(unit_2)
if unit_3 and isinstance(unit_1, Lancer):
unit_3.loss(unit_1.attack // 2)
@staticmethod
def heal(unit_1, army_attack):
unit_healer = army_attack.next_unit(unit_1)
if unit_healer and isinstance(unit_healer, Healer):
unit_healer.heal(unit_1)
@staticmethod
def check_warlord(army):
return bool([unit for unit in army if isinstance(unit, Warlord)])
def straight_fight(self, 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)
army_1.pop_dead()
army_2.pop_dead()
return army_1.is_alive
def fight(self, army1: Army, army2: Army):
while army1.is_alive and army2.is_alive:
if self.check_warlord(army1):
army1.move_units()
if self.check_warlord(army2):
army2.move_units()
unit_1 = army1[0]
unit_2 = army2[0]
while True:
self.hit(unit_1, unit_2, army2)
self.heal(unit_1, army1)
if not unit_2.is_alive:
break
self.hit(unit_2, unit_1, army1)
self.heal(unit_2, army2)
if not unit_1.is_alive:
break
army1.pop_dead()
army2.pop_dead()
return army1.is_alive
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = 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
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()
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
#battle tests
my_army = Army()
my_army.add_units(Knight, 3)
enemy_army = Army()
enemy_army.add_units(Warrior, 3)
army_3 = Army()
army_3.add_units(Warrior, 20)
army_3.add_units(Knight, 5)
army_4 = Army()
army_4.add_units(Warrior, 30)
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()
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
#battle tests
my_army = Army()
my_army.add_units(Defender, 1)
enemy_army = Army()
enemy_army.add_units(Warrior, 2)
army_3 = Army()
army_3.add_units(Warrior, 1)
army_3.add_units(Defender, 1)
army_4 = Army()
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()
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()
assert 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()
assert type(my_army.units[0]) == Lancer
assert type(my_army.units[1]) == Healer
assert type(my_army.units[-1]) == Warlord
assert type(enemy_army.units[0]) == Vampire
assert type(enemy_army.units[-1]) == Warlord
assert type(enemy_army.units[-2]) == Knight
#6, not 8, because only 1 Warlord per army could be
assert len(enemy_army.units) == 6
battle = Battle()
assert battle.fight(my_army, enemy_army) == True
print("Coding complete? Let's try tests!")
Feb. 3, 2021
Comments: