Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
fight() as class method solution in Clear category for The Warriors by kkkkk
class Warrior:
"""Base class for fighting unit."""
def __init__(self):
self._health = 50
self.attack_points = 5
self.is_alive = True
def fight(self, opponent):
"""Loop striking opponent and being struck back until someone dies."""
while self.is_alive and opponent.is_alive:
opponent.wounded(self)
if opponent.is_alive:
self.wounded(opponent)
return self.is_alive
def wounded(self, opponent):
"""Update status when opponent strikes, but don't strike back."""
self._health -= opponent.attack_points
if self._health <= 0:
self.is_alive = False
class Knight(Warrior):
"""Specialized fighting unit based on Warrior."""
def __init__(self):
super().__init__()
self.attack_points = 7
def fight(unit_1, unit_2):
return unit_1.fight(unit_2)
Nov. 12, 2019