Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
self.vampirism /100 solution in Clear category for The Vampires by katnic
'''
WARRIOR CLASS
'''
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
@property
def is_alive(self):
return self.health > 0
def hit(self, other):
other.health -= other.damage(self.attack)
def damage(self, attack):
return attack
'''
CHILD CLASS OF WARRIOR
'''
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
class Defender(Warrior):
def __init__(self):
super().__init__()
self.health = 60
self.attack = 3
self.defense = 2
def damage(self, attack):
return max(attack - self.defense, 0)
class Vampire(Warrior):
def __init__(self):
super().__init__()
self.health = 40
self.attack = 4
self.vampirism = 50
def hit(self, other):
super().hit(other)
self.health += other.damage(self.attack)*(self.vampirism /100)
'''
UNITS FIGHT
'''
def fight(unit_1, unit_2):
first, second = unit_1, unit_2
while first.is_alive:
first.hit(second)
first, second = second, first
return unit_1.is_alive
'''
CLASS ARMY
'''
class Army:
def __init__(self):
self.units = []
@property
def is_alive(self):
return self.units != []
@property
def first(self):
return self.units[0]
@property
def bury_unit(self):
self.units.pop(0)
def add_units(self, unit, count):
self.units += [unit() for _ in range(count)]
'''
CLASS BATTLE
'''
class Battle:
@staticmethod
def fight(army_1, army_2):
while army_1.is_alive and army_2.is_alive:
if fight(army_1.first, army_2.first): army_2.bury_unit
else: army_1.bury_unit
return army_1.is_alive
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!")
Sept. 7, 2021
Comments: