Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Vampires by defensor
class Warrior:
def __init__(self, attack=5, health=50, is_alive=True, defense=0):
self.attack = attack
self.health = health
self.health_max = health
self._is_alive = is_alive
self.defense = defense
@property
def is_alive(self):
if self.health <= 0:
self._is_alive = False
return self._is_alive
def attacking(self, opponent):
opponent.health = opponent.health - self.attack + opponent.defense
if type(self) == Vampire:
self.health = self.health + int((self.attack - opponent.defense) * (self.vampirism / 100))
if opponent.health > opponent.health_max:
opponent.health = opponent.health_max
class Knight(Warrior):
def __init__(self, attack=7, health=50, is_alive=True, defense=0):
super().__init__(attack, health, is_alive, defense)
class Defender(Warrior):
def __init__(self, attack=3, health=60, is_alive=True, defense=2):
super().__init__(attack, health, is_alive, defense)
class Rookie(Warrior):
def __init__(self, attack=1, health=50, is_alive=True, defense=0):
super().__init__(attack, health, is_alive, defense)
class Vampire(Warrior):
def __init__(self, attack=4, health=40, is_alive=True, defense=0):
super().__init__(attack, health, is_alive, defense)
self.vampirism = 50
######################################################################
def fight(unit_01, unit_02):
while unit_01.is_alive and unit_02.is_alive:
unit_01.attacking(unit_02)
if unit_02.is_alive:
unit_02.attacking(unit_01)
return unit_01.is_alive
######################################################################
class Army:
def __init__(self, has_soldier=True):
self.troop = []
self._has_soldier = has_soldier
@property
def has_soldier(self):
if len(self.troop):
pass
else:
self._has_soldier = False
return self._has_soldier
def add_units(self, unit, number):
for i in range(number):
self.troop.append(unit())
return self.troop
class Battle:
def fight(self, troop_01, troop_02):
while troop_01.has_soldier and troop_02.has_soldier:
if fight(troop_01.troop[0], troop_02.troop[0]):
troop_02.troop.pop(0)
else:
troop_01.troop.pop(0)
return troop_01.has_soldier
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. 1, 2021
Comments: