Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Vampires by kalauroma7997
class Warrior:
def __init__(self, health=50, attack=5):
self.health = health
self.attack = attack
@property
def is_alive(self):
return self.health > 0
def to_hit(self, enemy):
return enemy.take_damage(self.attack)
def take_damage(self, dmg):
self.health -= self.damage(dmg)
def damage(self, dmg):
return dmg
class Knight(Warrior):
def __init__(self, health=50, attack=7):
super().__init__(health, attack)
class Defender(Warrior):
def __init__(self, health=60, attack=3, defense=2):
super().__init__(health, attack)
self.defense = defense
def damage(self, damage):
return max(0, damage - self.defense)
class Vampire(Warrior):
def __init__(self, health=40, attack=4, vampirism=50):
super().__init__(health, attack)
self.vampirism = vampirism
def to_hit(self, enemy):
super().to_hit(enemy)
self.health += enemy.damage(self.attack) * self.vampirism // 100
def fight(unit1, unit2):
while unit1.is_alive and unit2.is_alive:
unit1.to_hit(unit2)
if not unit1.is_alive or not unit2.is_alive:
break
unit2.to_hit(unit1)
return unit1.is_alive
class Army(object):
def __init__(self):
self.warriors = []
def add_units(self, warrior, amount):
for i in range(amount):
self.warriors.append(warrior())
class Battle(object):
def __init__(self):
pass
def fight(self, army1, army2):
while army1.warriors and army2.warriors:
if fight(army1.warriors[-1], army2.warriors[-1]):
army2.warriors.pop()
else:
army1.warriors.pop()
return bool(army1.warriors)
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!")
Aug. 22, 2020