|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +class Weapon: |
| 4 | + name: str |
| 5 | + damage: int |
| 6 | + |
| 7 | + def __init__(self: "Weapon", init_name: str, init_damage: int): |
| 8 | + self.name = init_name |
| 9 | + self.damage = init_damage |
| 10 | + |
| 11 | + def __repr__(self: "Weapon") -> str: |
| 12 | + return "Weapon(name=" + self.name + ", damage=" + str(self.damage) + ")" |
| 13 | + |
| 14 | +class Supplement: |
| 15 | + name: str |
| 16 | + healing: int |
| 17 | + |
| 18 | + def __init__(self: "Supplement", name: str, healing: int): |
| 19 | + self.name = name |
| 20 | + self.healing = healing |
| 21 | + |
| 22 | + def __repr__(self: "Supplement") -> str: |
| 23 | + return "Supplement(name=" + self.name + ", healing=" + str(self.healing) + ")" |
| 24 | + |
| 25 | +class Character: |
| 26 | + name: str |
| 27 | + health: int |
| 28 | + |
| 29 | + def __init__(self: "Character", name: str, health: int): |
| 30 | + self.name = name |
| 31 | + self.health = health |
| 32 | + |
| 33 | + def take(self: "Character", supplement: Supplement): |
| 34 | + self.health += supplement.healing |
| 35 | + |
| 36 | +class Monster(Character): |
| 37 | + def __init__(self: "Monster", name: str, health: int = 5): |
| 38 | + super().__init__(name, health) |
| 39 | + |
| 40 | + def __repr__(self: "Monster") -> str: |
| 41 | + return "Monster(name=" + self.name + ", health=" + str(self.health) + ")" |
| 42 | + |
| 43 | +class Player(Character): |
| 44 | + """A fearless wanderer, ready to attack.""" |
| 45 | + weapon: Optional[Weapon] = None |
| 46 | + |
| 47 | + def __init__(self: "Player", name: str, health: int = 10): |
| 48 | + super().__init__(name, health) |
| 49 | + |
| 50 | + def __repr__(self: "Player") -> str: |
| 51 | + return "Player(name=" + self.name + ", health=" + str(self.health) + ", weapon=" + repr(self.weapon) + ")" |
| 52 | + |
| 53 | + def attack(self: "Player", other: "Player | Monster"): |
| 54 | + if self.weapon is None: |
| 55 | + other.health -= 1 |
| 56 | + else: |
| 57 | + other.health -= self.weapon.damage |
| 58 | + |
| 59 | +class Villager(Character): |
| 60 | + def __init__(self: "Villager", name: str): |
| 61 | + super().__init__(name, 9) |
| 62 | + |
| 63 | +hero = Player("Hero") |
| 64 | +orky = Monster("Orky") |
| 65 | +sword = Weapon("Sword", 3) |
| 66 | +hero.weapon = sword |
| 67 | +hugo = Villager("Hugo Müller") |
| 68 | +hero.attack(hugo) |
| 69 | +heiltrank = Supplement("Donnergurgler", 10) |
| 70 | + |
0 commit comments