Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Vampires by Sarina
# Taken from mission The Defenders
# Taken from mission Army Battles
class Warrior:
def __init__(self, health=50, attack=5, defense=0):
self.max_health = health
self.health = health
self.attack = attack
self.defense = defense
def dammage_done(self, defense):
return max(self.attack - defense, 0)
@property
def is_alive(self):
return self.health > 0
class Knight(Warrior):
def __init__(self):
super().__init__(attack=7)
class Defender(Warrior):
def __init__(self):
super().__init__(health=60, attack=3, defense=2)
class Vampire(Warrior):
def __init__(self):
super().__init__(health=40, attack=4)
self.vampirism = 50
def dammage_done(self, defense):
dammage = max(self.attack - defense, 0)
health_restore = (dammage * self.vampirism) // 100
self.health = min(self.max_health, self.health + health_restore)
return dammage
class Rookie(Warrior):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.health = 50
self.attack = 1
class Army():
def __init__(self):
self.army = []
def have_unit(self):
return bool(self.army)
def next_unit(self):
return self.army.pop() if self.have_unit() else None
def add_units(self, unit, number):
for _ in range(number):
self.army.append(unit())
class Battle():
def _init_(self):
pass
def fight(self, army1, army2):
if army1.have_unit():
unit1 = army1.next_unit()
else:
return False
if army2.have_unit():
unit2 = army2.next_unit()
else:
return True
while True:
result = fight(unit1, unit2)
# unit1 lost and army1 still have unit
if not result and army1.have_unit():
unit1 = army1.next_unit()
# unit1 lost and army1 don't have unit
elif not result and not army1.have_unit():
return False
# unit2 lost and army2 still have unit
elif result and army2.have_unit():
unit2 = army2.next_unit()
# unit2 lost and army2 don't have unit
else:
return True
def fight(unit_1, unit_2):
while True:
if unit_1.is_alive:
unit_2.health -= unit_1.dammage_done(unit_2.defense)
else:
return False
if unit_2.is_alive:
unit_1.health -= unit_2.dammage_done(unit_1.defense)
else:
return True
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!")
Dec. 26, 2019