Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Vampires by liuq901
class Warrior(object):
def __init__(self):
self.hp = 50
self.health = 50
self.attack = 5
self.defense = 0
self.vampirism = 0
def __getattr__(self, name):
if name == 'is_alive':
return self.health > 0
raise AttributeError
class Knight(Warrior):
def __init__(self):
super().__init__()
self.attack = 7
class Defender(Warrior):
def __init__(self):
super().__init__()
self.hp = 60
self.health = 60
self.attack = 3
self.defense = 2
class Vampire(Warrior):
def __init__(self):
super().__init__()
self.hp = 40
self.health = 40
self.attack = 4
self.vampirism = 50
def fight(unit_1, unit_2):
cnt = 0
while unit_1.is_alive and unit_2.is_alive:
damage = max(0, unit_1.attack - unit_2.defense)
unit_2.health -= damage
unit_1.health = min(unit_1.hp, unit_1.health + damage * unit_1.vampirism / 100)
unit_1, unit_2 = unit_2, unit_1
cnt += 1
return cnt % 2 == 1
class Army(object):
def __init__(self):
self.army = []
def add_units(self, units, amount):
self.army = [units() for _ in range(amount)] + self.army
class Battle(object):
def fight(self, x, y):
while x.army and y.army:
if fight(x.army[-1], y.army[-1]):
y.army.pop()
else:
x.army.pop()
return len(x.army) > 0
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!")
Nov. 19, 2021