Advertisement
differen71

Heroes of Code and Logic VII

Nov 28th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.30 KB | None | 0 0
  1. """03. Heroes of Code and Logic VII
  2. On the first line of the standard input, you will receive an integer n – the number of heroes that you can choose for your party. On the next n lines, the heroes themselves will follow with their hit points and mana points separated by a single space in the following format:
  3. "{hero name} {HP} {MP}"
  4. -   HP stands for hit points and MP for mana points
  5. -   a hero can have a maximum of 100 HP and 200 MP
  6. After you have successfully picked your heroes, you can start playing the game. You will be receiving different commands, each on a new line, separated by " – ", until the "End" command is given.
  7. There are several actions that the heroes can perform:
  8. "CastSpell – {hero name} – {MP needed} – {spell name}"
  9. • If the hero has the required MP, he casts the spell, thus reducing his MP. Print this message:
  10. o   "{hero name} has successfully cast {spell name} and now has {mana points left} MP!"
  11. • If the hero is unable to cast the spell print:
  12. o   "{hero name} does not have enough MP to cast {spell name}!"
  13. "TakeDamage – {hero name} – {damage} – {attacker}"
  14. • Reduce the hero HP by the given damage amount. If the hero is still alive (his HP is greater than 0) print:
  15. o   "{hero name} was hit for {damage} HP by {attacker} and now has {current HP} HP left!"
  16. • If the hero has died, remove him from your party and print:
  17. o   "{hero name} has been killed by {attacker}!"
  18. "Recharge – {hero name} – {amount}"
  19. • The hero increases his MP. If it brings the MP of the hero above the maximum value (200), MP is increased to 200. (the MP can't go over the maximum value).
  20. •  Print the following message:
  21. o   "{hero name} recharged for {amount recovered} MP!"
  22. "Heal – {hero name} – {amount}"
  23. • The hero increases his HP. If a command is given that would bring the HP of the hero above the maximum value (100),
  24. HP is increased to 100 (the HP can't go over the maximum value).
  25. •  Print the following message:
  26. o   "{hero name} healed for {amount recovered} HP!"
  27. Input
  28. • On the first line of the standard input, you will receive an integer n
  29. • On the following n lines, the heroes themselves will follow with their hit points and
  30. mana points separated by a space in the following format
  31. • You will be receiving different commands, each on a new line, separated by " – ", until the "End" command is given
  32. Output
  33. • Print all members of your party who are still alive,
  34. in the following format (their HP/MP need to be indented 2 spaces):
  35. "{hero name}
  36.  HP: {current HP}
  37.  MP: {current MP}"
  38. """
  39.  
  40.  
  41. # Heal function.
  42. # Adding the given amount of HP to the given hero HP value.
  43. # If the amount is exceeding the maximum allowed HP value (100), the recovered amount is calculated and
  44. # it is then added to the current HP.
  45. # Prints confirmation message.
  46. def heal(party, current_hero, amount):
  47.     if party[current_hero]['HP'] + amount > 100:
  48.         amount_recovered = 100 - party[current_hero]['HP']
  49.         party[current_hero]['HP'] += amount_recovered
  50.         print(f"{current_hero} healed for {amount_recovered} HP!")
  51.     else:
  52.         party[current_hero]['HP'] += amount
  53.         print(f"{current_hero} healed for {amount} HP!")
  54.  
  55.  
  56. # Recharge function.
  57. # Adding the given amount of MP to the given hero MP value.
  58. # If the amount is exceeding the maximum allowed MP value (200), the recovered amount is calculated and
  59. # it is then added to the current MP.
  60. # Prints confirmation message.
  61. def recharge(party, current_hero, amount):
  62.     if party[current_hero]['MP'] + amount > 200:
  63.         amount_recovered = 200 - party[current_hero]['MP']
  64.         party[current_hero]['MP'] += amount_recovered
  65.         print(f"{current_hero} recharged for {amount_recovered} MP!")
  66.     else:
  67.         party[current_hero]['MP'] += amount
  68.         print(f"{current_hero} recharged for {amount} MP!")
  69.  
  70.  
  71. # Take damage function.
  72. # Receives damage and attacker and it is applied to the current hero in the party.
  73. # Decreases current hero's HP by the given damage and checks the hero's current HP.
  74. # If the hero has remaining HP, the function prints the confirmation message accordingly.
  75. # If the hero's HP is lower than 0, the hero is killed. Hero is deleted from the party and confirmation message
  76. # is printed accordingly.
  77. def take_damage(party, current_hero, damage, attacker):
  78.     party[current_hero]['HP'] -= damage
  79.  
  80.     if party[current_hero]['HP'] > 0:
  81.         print(f"{current_hero} was hit for {damage} HP by {attacker} and now has {party[current_hero]['HP']} HP left!")
  82.     else:
  83.         print(f"{current_hero} has been killed by {attacker}!")
  84.         del party[current_hero]
  85.  
  86.  
  87. # Checks if the current hero has enough MP, if so hero's MP is decreased and the spell is casted.
  88. # Confirmation message is printed accordingly.
  89. # If the hero does not have enough MP to cast the spell, it's MP is not decreased. Confirmation message
  90. # is printed accordingly.
  91. def cast_spell(party, current_hero, mp_needed, spell_name):
  92.     if party[current_hero]['MP'] >= mp_needed:
  93.         party[current_hero]['MP'] -= mp_needed
  94.         print(f"{current_hero} has successfully cast {spell_name} and now has {party[current_hero]['MP']} MP!")
  95.     else:
  96.         print(f"{current_hero} does not have enough MP to cast {spell_name}!")
  97.  
  98.  
  99. # Main function.
  100. def main_func(heroes_count):
  101.     # Dictionary, containing all heroes in the party.
  102.     # Each hero has two attributes - HP and MP.
  103.     party = {}
  104.     # Looping through the given number of heroes in the party and adding each of them into the pre-made dictionary.
  105.     # For every count, hero data is received as a string, and it is then separated onto different elements as follows:
  106.     # name = hero_data[0], hp = hero_data[1], mp = hero_data[2]
  107.     for count in range(heroes_count):
  108.         hero_data = input().split()
  109.         hero, hp, mp = hero_data[0], int(hero_data[1]), int(hero_data[2])
  110.         if hero not in party.keys():
  111.             party[hero] = {}
  112.             party[hero]['HP'] = 0
  113.             party[hero]['MP'] = 0
  114.         party[hero]['HP'] = hp
  115.         party[hero]['MP'] = mp
  116.     # Receiving different commands, which are then split by " - ".
  117.     # If command "End" is received, the loop ends and every live hero is printed along with its attributes.
  118.     while True:
  119.         command = input().split(' - ')
  120.         if command[0] == 'End':
  121.             for key, value in party.items():
  122.                 print(f"{key}\n  HP: {value['HP']}\n  MP: {value['MP']}")
  123.             break
  124.         # Calling out the pre-made functions accordingly.
  125.         elif command[0] == 'CastSpell':
  126.             current_hero, mp_needed, spell_name = command[1], int(command[2]), command[3]
  127.             cast_spell(party, current_hero, mp_needed, spell_name)
  128.         elif command[0] == 'TakeDamage':
  129.             current_hero, damage, attacker = command[1], int(command[2]), command[3]
  130.             take_damage(party, current_hero, damage, attacker)
  131.         elif command[0] == 'Recharge':
  132.             current_hero, amount = command[1], int(command[2])
  133.             recharge(party, current_hero, amount)
  134.         elif command[0] == 'Heal':
  135.             current_hero, amount = command[1], int(command[2])
  136.             heal(party, current_hero, amount)
  137.  
  138.  
  139. # Receiving number of heroes to be added to the party.
  140. number_of_heroes = int(input())
  141. # Calling out main function.
  142. main_func(number_of_heroes)
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement