Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for The Warriors by ibonyun
class Warrior:
max_health = 50
attack_strength = 5
def __init__(self):
self.health = self.max_health
@property
def is_alive(self):
'''Checks if self is still alive, ie health > 0.'''
return self.health > 0
def attack(self, other):
'''Inflicts damage on other. Returns boolean indicating if the attack was fatal.'''
return other.take_damage(self.attack_strength)
def take_damage(self, damage):
'''Subtracts health. Returns boolean indicating if the damage was fatal.'''
self.health -= damage
return not self.is_alive
class Knight(Warrior):
attack_strength = 7
def fight(unit_1, unit_2):
'''Conducts duel between 2 characters. Returns True if the 1st wins, else False.'''
while True:
if unit_1.attack(unit_2):
return True
if unit_2.attack(unit_1):
return False
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = 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
print("Coding complete? Let's try tests!")
June 23, 2020