Last active
January 13, 2020 12:24
-
-
Save Th1sUs3rH4sN0N4m3/31db305485035407d7212fb08254ab49 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 Health: | |
def __init__(self, health, delta=10): | |
self.health = health | |
self.delta = delta | |
def decrease(self, ratio): | |
self.health -= ratio * self.delta | |
def check(self): | |
return True if self.health > 0 else False | |
def __str__(self): | |
return f"{self.health}" | |
class Warrior: | |
def __init__(self, name, health, ratio): | |
self.name = name | |
self.health = health | |
self.ratio = ratio | |
def __str__(self): | |
return self.name | |
def get_kick(self, ratio): | |
self.health.decrease(ratio) | |
def still_alive(self): | |
return self.health.check() | |
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) | |
Output.print_health_warriors(self.warriors) | |
if not attacked_warrior.still_alive(): | |
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.get_kick(attacking_warrior.ratio) | |
Output.print_attack((attacking_warrior, attacked_warrior)) | |
class Output: | |
@staticmethod | |
def print_health_warriors(warriors): | |
print(f"{warriors[0]} has health {warriors[0].health}, {warriors[1]} has health {warriors[1].health}") | |
def print_attack(warriors): | |
print(f"{warriors[0]} attacked {warriors[1]}") | |
def main(): | |
war1 = Warrior('Tom', Health(100), 2.0) | |
war2 = Warrior('Jerry', Health(100), 0.5) | |
battle = Battle(war1, war2) | |
battle.run() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment