Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for The Lancers by marcopunteri
# Taken from mission The Vampires
# Taken from mission The Defenders
# Taken from mission Army Battles
# Taken from mission The Warriors
class EmptyArmy(Exception):
def __init__(self,obj=None):
self.obj = obj
class Warrior:
def __init__(self):
self._health = 50
self.attack = 5
def __str__(self):
return f"Warrior, salute {self.health}"
@property
def health(self):
#print('mi chiedono health',self._health)
return self._health
@health.setter
def health(self,value):
#self._health = min(value,type(self)._health)
self._health = value
@property
def is_alive(self):
return self._health > 0
def hit(self, other):
other.being_hit(self.attack)
def being_hit(self,damage):
self._health -= damage
return damage
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
class Defender(Warrior):
_health = 60
def __init__(self):
self.health = Defender._health
self.attack = 3
self.defense = 2
def __str__(self):
return f"Defender, salute {self.health}"
def being_hit(self,damage):
damage = max(0,damage-self.defense)
self.health -= damage
return damage
class Vampire(Warrior):
_health = 40
def __init__(self):
self.health = Vampire._health
self.attack = 4
self._vampirism = 50
def __str__(self):
return f"Vampire, salute {self.health}"
@property
def health(self):
#print('mi chiedono health',self._health)
return self._health
@health.setter
def health(self,value):
self._health = min(value,type(self)._health)
@property
def vampirism(self):
return self._vampirism
def hit(self, other):
damage = other.being_hit(self.attack)
self.regenerate(damage)
def regenerate(self,damage_dealt):
self.health += self.vampirism * damage_dealt / 100
class Lancer(Warrior):
_health = 50
def __init__(self):
self.health = Lancer._health
self.attack = 6
def __str__(self):
return f"Lancer, salute {self.health}"
def hit(self, other):
other.being_hit(self.attack)
if hasattr(other,'next'):
other.next.being_hit(self.attack/2)
class Army:
def __init__(self):
self._units = []
def __str__(self):
return ' - '.join([unit.__str__() for unit in self._units])
def add_units(self,type_of_unit,num_of_units):
for n in range(num_of_units):
self._units.append(type_of_unit())
def next_unit(self):
if self._units:
self.active_unit = self._units.pop(0)
return self.active_unit
else:
raise EmptyArmy(self)
def set_column_formation(self):
for pos, soldier in enumerate(self._units[:-1]):
soldier.next = self._units[pos+1]
class Battle:
def __init__(self):
self._armies = []
def get_soldier(self,id_):
return self._armies[id_].next_unit()
def fight(self,army_1, army_2):
self._armies = [army_1,army_2]
for army in self._armies:
army.set_column_formation()
self.fighters = []
self.fighters.append(self.get_soldier(0))
self.fighters.append(self.get_soldier(1))
try:
while True:
last_won = fight(self.fighters[0],self.fighters[1])
if last_won:
self.fighters[1] = self.get_soldier(1)
else:
self.fighters[0] = self.get_soldier(0)
except EmptyArmy:
self.res = last_won
return self.res
def fight(unit_1, unit_2):
attack = 0
defense = 1
fighters = [unit_1,unit_2]
while fighters[attack].is_alive:
fighters[attack].hit(fighters[defense])
attack, defense = defense, attack
#attack is the actual looser, but if attack=0 i have to return False, and False is 0
return attack
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!")
May 8, 2021
Comments: