Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Defenders by marcopunteri
# Taken from mission Army Battles
# Taken from mission The Warriors
from copy import deepcopy as copy
class EmptyArmy(Exception):
pass
class Warrior:
def __init__(self):
self._health = 50
self._attack = 5
@property
def health(self):
return self._health
@health.setter
def health(self,value):
self._health = value
@property
def attack(self):
return self._attack
@attack.setter
def attack(self,value):
self._attack = value
@property
def is_alive(self):
return self._health > 0
def hit(self):
return self._attack
def being_hit(self,damage):
self._health -= damage
return None
class Knight(Warrior):
def __init__(self):
super().__init__()
self._attack = 7
class Defender(Warrior):
def __init__(self):
self.health = 60
self.attack = 3
self._defense = 2
@property
def defense(self):
return self._defense
def being_hit(self,damage):
damage = max(0,damage-self._defense)
self._health -= damage
class Army:
def __init__(self):
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_soldier(self):
if self._units:
return self._units.pop(0)
else:
raise EmptyArmy()
class Battle:
def __init__(self):
self._armies = []
def get_soldier(self,id_):
return self._armies[id_].next_soldier()
def fight(self,army_1, army_2):
self._armies = [copy(army_1),copy(army_2)]
fighters = []
fighters.append(self.get_soldier(0))
fighters.append(self.get_soldier(1))
try:
#turn = 0
while True:
last_won = fight(fighters[0],fighters[1])
if last_won:
fighters[1] = self.get_soldier(1)
else:
fighters[0] = self.get_soldier(0)
except EmptyArmy:
self.res = last_won
return self.res
def fight(unit_1, unit_2):
turn = 0
fighters = [unit_1,unit_2]
while fighters[turn].is_alive:
fighters[(turn+1)%2].being_hit(fighters[turn].hit())
turn = (turn+1)%2
return turn%2 == 1
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()
#print(f'prima battaglia {battle.fight(my_army, enemy_army)}')
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!")
April 28, 2021
Comments: