Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Lancers by Alejandro_El_Diablo
# Taken from mission The Vampires
# Taken from mission The Defenders
# Taken from mission Army Battles
# Taken from mission The Warriors
EPS = 10 ** -9
class Warrior:
def __init__(self, attack=5, health=50):
self.health = health
self.attack = attack
self.is_alive = True
self.at_list = (1,)
def get_attack(self):
self.up_is_alive()
if not self.is_alive:
return tuple(0)
from math import floor
return (*list(int(floor(self.attack * i + EPS)) for i in self.at_list), )
def deffend(self, other, damage):
self.health -= damage
other.attack_callback(damage)
self.up_is_alive()
def up_is_alive(self):
self.is_alive = self.health > 0
def attack_callback(self, damage):
pass
def __str__(self):
return "(" + str(type(self)) + ", " + str(self.health) + ", " + str(self.attack) + ")"
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
def fight(unit_1, unit_2):
turSec = False
while unit_1.is_alive and unit_2.is_alive:
if turSec:
unit_1.deffend(unit_2, unit_2.get_attack()[0])
else:
unit_2.deffend(unit_1, unit_1.get_attack()[0])
turSec = not turSec
return unit_1.is_alive
class Army:
def __init__(self, a=[]):
self.a=[]
self.p=0
def add_units(self, typ, num):
for i in range(num):
self.a.append(typ())
def up_alive(self):
while not self.empty() and not self.a[self.p].is_alive:
self.p += 1
def get_unit(self):
self.up_alive()
return self.a[self.p] if not self.empty() else None
def deffend(self, enemy_unit):
z = enemy_unit.get_attack()
j = self.p
for i in range(len(z)):
while j < len(self.a) and not self.a[j].is_alive:
j += 1
if j == len(self.a):
return
self.a[j].deffend(enemy_unit, z[i])
if not self.a[self.p].is_alive:
self.up_alive()
return True
return False
def empty(self):
return self.p >= len(self.a)
class Battle():
def __init__(self):
pass
def fight(self, army_1, army_2):
u1, u2 = army_1.get_unit(), army_2.get_unit()
fir_tur = True
while u1 is not None and u2 is not None:
if fir_tur:
if army_2.deffend(u1):
fir_tur = False
else:
if army_1.deffend(u2):
fir_tur = False
u2 = army_2.get_unit()
u1 = army_1.get_unit()
fir_tur = not fir_tur
return u1 is not None
class Defender(Warrior):
def __init__(self):
super().__init__(attack=3, health=60)
self.defense = 2
def deffend(self, other, damage):
delta = max(damage - self.defense, 0)
self.health -= delta
other.attack_callback(delta)
self.up_is_alive()
class Vampire(Warrior):
def __init__(self):
super().__init__(attack=4, health=40)
self.vampirism = 50
def attack_callback(self, damage):
self.up_is_alive()
self.health += damage * self.vampirism // 100
class Lancer(Warrior):
def __init__(self):
super().__init__(attack=6, health=50)
self.at_list = (1, 1/2)
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!")
April 29, 2021