
A group of adventurers has entered a mysterious dungeon, where only the strongest will survive!
The dungeon is filled with warriors, mages, and healers, each possessing unique combat abilities. As battles unfold, characters attack each other, take damage, and use their special abilities to stay alive.
Your task is to implement a system that handles character creation, combat, health management, and winner detection using inheritance.
Every character has a unique name, health points (hp), and attack power (power). There are three character classes:
| Class | Ability |
| Warrior |
Reduces incoming damage by armor |
| Mage |
Deals double attack damage |
| Healer |
Heals itself after attacking |
The following rules apply:
Warrior: Incoming damage is reduced by armor, but never below 0.
Mage: Deals power × 2 damage instead of normal damage.
Healer: Deals normal damage and restores heal_amount HP after attacking, without exceeding its starting HP (max_hp).
Health: A character's HP cannot fall below 0. Characters with 0 HP are defeated.
Attacks: Both characters must exist and be alive. A character cannot attack itself. Invalid attacks have no effect.
Winner: A winner is declared only when exactly one character remains alive.
The damage reported by an attack is the actual HP lost by the target, after armor reduction and HP limits.
Complete all TODO sections in the starter code using inheritance.
The Battle class maintains two data structures:
characters: A list storing characters in the order they were added.by_name: A dictionary mapping character names to their corresponding objects for efficient lookup.Both structures refer to the same character objects and must remain consistent.
Do not modify the provided main program.
class Character:
def __init__(self, name, hp, power):
# TODO: Implement this method.
raise NotImplementedError
def is_alive(self):
"""Return True if alive, otherwise False."""
# TODO: Implement this method.
raise NotImplementedError
def take_damage(self, amount):
"""Return the actual HP lost."""
# TODO: Implement this method.
raise NotImplementedError
def attack(self, target):
"""Return the actual damage dealt to the target."""
# TODO: Implement this method.
raise NotImplementedError
class Warrior(Character):
def __init__(self, name, hp, power, armor):
# TODO: Implement this method.
raise NotImplementedError
def take_damage(self, amount):
"""Return the actual HP lost after armor reduction."""
# TODO: Implement this method.
raise NotImplementedError
class Mage(Character):
def attack(self, target):
"""Return the actual damage dealt to the target."""
# TODO: Implement this method.
raise NotImplementedError
class Healer(Character):
def __init__(self, name, hp, power, heal_amount):
# TODO: Implement this method.
raise NotImplementedError
def attack(self, target):
"""Return the actual damage dealt to the target."""
# TODO: Implement this method.
raise NotImplementedError
class Battle:
def __init__(self):
self.characters = [] # Characters in insertion order.
self.by_name = {} # Maps character names to their objects.
def add_character(self, character):
# TODO: Implement this method.
raise NotImplementedError
def attack(self, attacker_name, target_name):
"""Return actual damage (int), or None if the attack is invalid."""
# TODO: Implement this method.
raise NotImplementedError
def winner(self):
"""Return the winner's name (str), or None if there is no winner."""
# TODO: Implement this method.
raise NotImplementedError
def status_lines(self):
"""Return one "<name> <hp>" or "<name> DEFEATED" string per character,
in the order they were added."""
# TODO: Implement this method.
raise NotImplementedError
def create_character(character_type, name, hp, power, extra):
"""Create a Warrior (WARRIOR), Mage (MAGE), or Healer (HEALER).
extra is armor for WARRIOR, heal_amount for HEALER, and None for MAGE."""
# TODO: Implement this function.
raise NotImplementedError
if __name__ == "__main__":
battle = Battle()
n = int(input())
for _ in range(n):
parts = input().split()
character_type = parts[0]
name = parts[1]
hp = int(parts[2])
power = int(parts[3])
if character_type == "MAGE":
extra = None
else:
extra = int(parts[4])
battle.add_character(
create_character(character_type, name, hp, power, extra)
)
q = int(input())
for _ in range(q):
parts = input().split()
command = parts[0]
if command == "ATTACK":
attacker_name = parts[1]
target_name = parts[2]
damage = battle.attack(attacker_name, target_name)
if damage is None:
print("INVALID")
else:
print(f"HIT {target_name} {damage}")
elif command == "WINNER":
winner = battle.winner()
if winner is None:
print("NONE")
else:
print(winner)
elif command == "STATUS":
for line in battle.status_lines():
print(line)
The first line contains an integer N, representing the number of characters.
The next N lines describe the characters in one of the following formats:
WARRIOR name hp power armor,
MAGE name hp power, or
HEALER name hp power heal_amount
name: The unique name of the characterhp: The character's starting health points.power: The character's base attack damage.armor: The Warrior's damage reduction.heal_amount: The Healer's HP recovery per attack.The next line contains an integer Q, representing the number of commands.
The following Q lines each contain one of three commands:
ATTACK attacker target: Attacker performs an attack towards target,
WINNER: Check whether exactly one character is still alive, or
STATUS: Display the current status of all characters.
Guarantees: All character names are unique. Initial HP and attack power are positive integers, while armor and healing amounts are nonnegative integers.
For each command, print the corresponding result:
HIT target damage: A successful attack, showing the actual HP lost.
INVALID: The attack cannot be performed.
For WINNER, print only the winner's name if exactly one character remains alive, or NONE otherwise.
For STATUS, print name hp for each living character or name DEFEATED for each defeated character, in registration order.
A valid attack that deals 0 damage must still print HIT target_name 0.