Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
the_lancers solution in Clear category for The Lancers by dannedved
# Taken from mission The Vampires
# Taken from mission The Defenders
# Taken from mission Army Battles
# Taken from mission The Warriors
###########################
# THE BASIC WARRIOR CLASS #
###########################
class Warrior:
def __init__(self, health = 50, attack = 5, is_alive = True, army = None):
self.health = health
self.attack = attack
self.is_alive = is_alive
self.army = army
# Does harm-related calculations
def will_be_damaged(self, amount):
# Defense-related block STARTS
if hasattr(self, "defense"):
block = self.defense
else:
block = 0
# Defense-related block ENDS
return (amount - block) * (amount > block)
# Reduces health and updates alive status ONLY
def hurt(self, amount):
self.health -= self.will_be_damaged(amount)
self.is_alive = self.health > 0
# Harms oponent and performs attack-related abilities
def strike(self, oponent):
oponent.hurt(self.attack)
# Vampirism-related block STARTS
if hasattr(self, "vampirism"):
healed = oponent.will_be_damaged(self.attack) * self.vampirism
dummy = type(self)() # Dummy will be used as the initial health reference
if self.health + healed <= dummy.health:
self.health += healed
elif self.health < dummy.health:
self.health = dummy.health
del dummy # Bye bye, dummy!
# Vampirism-related block ENDS
# Lancing-related block STARTS
if hasattr(self, "lancing") and oponent.army != None:
if oponent.army.troops.index(oponent) < - self.lancing[0]:
for hit in range(len(self.lancing[0])):
oponent.army.troops[oponent.army.troops.index(oponent) + hit + 1].hurt(self.attack / self.lancing[1][hit])
# Lancing-related block ENDS
###################
# DERIVED CLASSES #
###################
class Knight(Warrior):
def __init__(self):
Warrior.__init__(self, attack = 7)
class Defender(Warrior):
def __init__(self):
Warrior.__init__(self, health = 60, attack = 3)
self.defense = 2
class Vampire(Warrior):
def __init__(self):
Warrior.__init__(self, health = 40, attack = 4)
self.vampirism = 0.5
class Lancer(Warrior):
def __init__(self):
Warrior.__init__(self, attack = 6)
# Lancing has two parametres - the first one (int) tells us how many additional units are stroken; the second one (list) tells us how dammage to each additional unit
# is modified.
self.lancing = (1, [0.5])
#######################################
# FIGHT-RELATED FUNCTIONS AND CLASSES #
#######################################
def fight(unit_1, unit_2):
turn = 1
while unit_1.is_alive and unit_2.is_alive:
unit_1.strike(unit_2) if turn % 2 == 1 else unit_2.strike(unit_1)
turn += 1
return True if unit_1.is_alive else False
class Army():
def __init__(self):
self.troops = []
def add_units(self, unit_class, amount):
for i in range(amount):
self.troops.append(unit_class())
for troop in self.troops:
troop.army = self
def has_troops(self):
return len(self.troops) > 0
class Battle():
def __init__(self):
return None
def fight(self, army_1, army_2):
while army_1.has_troops() and army_2.has_troops():
duel = fight(army_1.troops[0], army_2.troops[0])
if duel:
army_2.troops.pop(0)
else:
army_1.troops.pop(0)
return army_1.has_troops()
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!")
June 3, 2019