Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Lancers by wo.tomasz
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
self.defense = 0
self.is_alive = True
def defence(self, attacking_unit, is_secendary=False):
att = self.__get_correct_attack(attacking_unit, is_secendary)
if self.defense < att:
self.health -= (att - self.defense)
attacking_unit.recovery(att - self.defense)
if self.health <= 0:
self.is_alive = False
return self.is_alive
def __get_correct_attack(self, attacking_unit, is_secendary):
return attacking_unit.secondary_atack() if is_secendary else attacking_unit.attack
def recovery(self, damage):
pass
def secondary_atack(self):
return 0
class Defender(Warrior):
def __init__(self):
super().__init__()
self.health = 60
self.attack = 3
self.defense = 2
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
class Vampire(Warrior):
def __init__(self):
super().__init__()
self.health = 40
self.attack = 4
self.vampirism = 50
def recovery(self, damage):
self.health += damage * self.vampirism / 100
class Lancer(Warrior):
def __init__(self):
super().__init__()
self.health = 50
self.attack = 6
def secondary_atack(self):
return self.attack/2
class Army:
def __init__(self):
self.units_list = []
def add_units(self, units_type, quantity):
for i in range(quantity):
self.units_list.append(units_type())
def single_hit(attacking_unit, defenders):
first_unit_is_alive = defenders[0].defence(attacking_unit)
if defenders[1] != None:
defenders[1].defence(attacking_unit, True)
return first_unit_is_alive
def fight(unit_1, unit_2, next_unit_1 = None, next_unit_2 = None):
while (True):
if not single_hit(unit_1, [unit_2, next_unit_2]):
break
if not single_hit(unit_2, [unit_1, next_unit_1]):
break
return unit_1.is_alive
class Battle:
def fight(self, army1: Army, army2: Army):
while (len(army1.units_list) > 0 and len(army2.units_list) > 0):
if (fight(army1.units_list[0], army2.units_list[0], army1.units_list[1] if len(army1.units_list) > 1 else None, army2.units_list[1] if len(army2.units_list) > 1 else None)):
del army2.units_list[0]
else:
del army1.units_list[0]
return len(army1.units_list) > 0
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 18, 2021