Last active
January 13, 2020 11:50
-
-
Save Th1sUs3rH4sN0N4m3/79075616f463660e41515805b44d100f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class HealthManager: | |
def __init__(self, health, delta_health=20): | |
self.__health = health | |
self.delta_health = delta_health | |
@property | |
def health(self): | |
return self.__health | |
def decrease_health(): | |
self.__health -= self.delta_health | |
def check_health(self): | |
return True if self.__health > 0 else False | |
class Warrior: | |
def __init__(self, name, health): | |
self.name = name | |
self.__health = health | |
@property | |
def health(self): | |
return self.__health.health | |
def __str__(self): | |
return self.name | |
def decrease_health(self): | |
return self.__health.decrease_health() | |
def check_health(self): | |
return self.__health.check_health() | |
class Battle: | |
def __init__(self, *warriors): | |
self.warriors = warriors | |
def run(self): | |
while 1: | |
attacking_warrior, attacked_warrior = self.get_warriors() | |
self.do_attack(attacking_warrior, attacked_warrior) | |
print(f"{attacking_warrior} has health {attacking_warrior.health}") | |
print(f"{attacked_warrior} has health {attacked_warrior.health}") | |
if not attacked_warrior.check_health(): | |
print(f"{attacking_warrior.name} won!") | |
break | |
def get_warriors(self): | |
return random.sample(self.warriors, 2) | |
def do_attack(self, attacking_warrior, attacked_warrior): | |
attacked_warrior.decrease_health() | |
print(f"{attacking_warrior} attacked {attacked_warrior}") | |
def main(): | |
war1 = Warrior('Tom', HealthManager(100)) | |
war2 = Warrior('Jerry', HealthManager(100)) | |
battle = Battle(war1, war2) | |
battle.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment