Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Vampires by woxdark
import math
class Warrior:
def __init__(self):
self.health = 50
self.attack = 5
@property
def is_alive(self):
return self.health > 0
def hit(self, enemy):
defense = 0
vampirism = 0
try:
defense = enemy.defense
except Exception:
pass
try:
vampirism = self.vampirism
except Exception:
pass
atack_power = 0 if defense >= self.attack else self.attack - defense
vampirism_power = math.floor(atack_power * vampirism)
enemy.health -= atack_power
self.health += vampirism_power
class Knight(Warrior):
def __init__(self):
super(Knight, self).__init__()
self.attack = 7
class Defender(Warrior):
def __init__(self):
super(Defender, self).__init__()
self.attack = 3
self.defense = 2
self.health = 60
class Vampire(Warrior):
def __init__(self):
super(Vampire, self).__init__()
self.attack = 4
self.health = 40
self.vampirism = 0.5
class Army():
def __init__(self):
self.set_army = []
def add_units(self, class_units, count):
self.set_army += [class_units() for i in range(count)]
def units_print(self):
for i in self.set_army:
print(i.health)
def get_next_unit(self):
if self.set_army:
if self.set_army[0].is_alive:
return self.set_army[0]
else:
self.set_army.pop(0)
return self.get_next_unit()
return None
class Battle():
def __init__(self):
pass
def fight(self, army1, army2):
unit1 = army1.get_next_unit()
unit2 = army2.get_next_unit()
if unit1 is None:
return False
if unit2 is None:
return True
fight(unit1, unit2)
return self.fight(army1, army2)
def fight(unit_1, unit_2):
while True:
if not unit_1.is_alive:
return False
unit_1.hit(unit_2)
if not unit_2.is_alive:
return True
unit_2.hit(unit_1)
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. 8, 2021