# Heroes of Code and Logic VII

Sep 18th, 2023 (edited)
1,084
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. class Hero:
2.     def __init__(self, name, hp, mp):
3.         self.name = name
4.         self.max_hp = 100
5.         self.max_mp = 200
6.         self.hp = min(hp, self.max_hp)
7.         self.mp = min(mp, self.max_mp)
8.
9.     def take_damage(self, damage, attacker):
10.         if self.hp > 0:
11.             self.hp -= damage
12.             if self.hp <= 0:
13.                 print(f"{self.name} has been killed by {attacker}!")
14.             else:
15.                 print(f"{self.name} was hit for {damage} HP by {attacker} and now has {self.hp} HP left!")
16.
17.     def cast_spell(self, spell_name, mp_needed):
18.         if self.mp >= mp_needed:
19.             self.mp -= mp_needed
20.             print(f"{self.name} has successfully cast {spell_name} and now has {self.mp} MP!")
21.             return True
22.         else:
23.             print(f"{self.name} does not have enough MP to cast {spell_name}!")
24.             return False
25.
26.     def recharge(self, amount):
27.         mp_before = self.mp
28.         self.mp = min(self.mp + amount, self.max_mp)
29.         amount_recovered = self.mp - mp_before
30.         print(f"{self.name} recharged for {amount_recovered} MP!")
31.
32.     def heal(self, amount):
33.         hp_before = self.hp
34.         self.hp = min(self.hp + amount, self.max_hp)
35.         amount_recovered = self.hp - hp_before
36.         print(f"{self.name} healed for {amount_recovered} HP!")
37.
38.     def is_alive(self):
39.         return self.hp > 0
40.
41.     def __str__(self):
42.         return f"{self.name}\n  HP: {self.hp}\n  MP: {self.mp}"
43.
44.
45. n = int(input())
46. heroes = {}
47.
48. for _ in range(n):
49.     name, hp, mp = input().split()
50.     hp, mp = int(hp), int(mp)
51.     heroes[name] = Hero(name, hp, mp)
52.
53. while True:
54.     command = input()
55.     if command == "End":
56.         break
57.
58.     action, *args = command.split(" - ")
59.     hero_name = args[0]
60.
61.     if hero_name in heroes:
62.         hero = heroes[hero_name]
63.         if action == "TakeDamage":
64.             damage, attacker = int(args[1]), args[2]
65.             hero.take_damage(damage, attacker)
66.         elif action == "CastSpell":
67.             spell_name, mp_needed = args[2], int(args[1])
68.             hero.cast_spell(spell_name, mp_needed)
69.         elif action == "Recharge":
70.             amount = int(args[1])
71.             hero.recharge(amount)
72.         elif action == "Heal":
73.             amount = int(args[1])
74.             hero.heal(amount)
75.
76. alive_heroes = [hero for hero in heroes.values() if hero.is_alive()]
77. for hero in alive_heroes:
78.     print(hero)
Tags: