Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """03. Heroes of Code and Logic VII
- 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:
- "{hero name} {HP} {MP}"
- - HP stands for hit points and MP for mana points
- - a hero can have a maximum of 100 HP and 200 MP
- 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.
- There are several actions that the heroes can perform:
- "CastSpell – {hero name} – {MP needed} – {spell name}"
- • If the hero has the required MP, he casts the spell, thus reducing his MP. Print this message:
- o "{hero name} has successfully cast {spell name} and now has {mana points left} MP!"
- • If the hero is unable to cast the spell print:
- o "{hero name} does not have enough MP to cast {spell name}!"
- "TakeDamage – {hero name} – {damage} – {attacker}"
- • Reduce the hero HP by the given damage amount. If the hero is still alive (his HP is greater than 0) print:
- o "{hero name} was hit for {damage} HP by {attacker} and now has {current HP} HP left!"
- • If the hero has died, remove him from your party and print:
- o "{hero name} has been killed by {attacker}!"
- "Recharge – {hero name} – {amount}"
- • 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).
- • Print the following message:
- o "{hero name} recharged for {amount recovered} MP!"
- "Heal – {hero name} – {amount}"
- • The hero increases his HP. If a command is given that would bring the HP of the hero above the maximum value (100),
- HP is increased to 100 (the HP can't go over the maximum value).
- • Print the following message:
- o "{hero name} healed for {amount recovered} HP!"
- Input
- • On the first line of the standard input, you will receive an integer n
- • On the following n lines, the heroes themselves will follow with their hit points and
- mana points separated by a space in the following format
- • You will be receiving different commands, each on a new line, separated by " – ", until the "End" command is given
- Output
- • Print all members of your party who are still alive,
- in the following format (their HP/MP need to be indented 2 spaces):
- "{hero name}
- HP: {current HP}
- MP: {current MP}"
- """
- # Heal function.
- # Adding the given amount of HP to the given hero HP value.
- # If the amount is exceeding the maximum allowed HP value (100), the recovered amount is calculated and
- # it is then added to the current HP.
- # Prints confirmation message.
- def heal(party, current_hero, amount):
- if party[current_hero]['HP'] + amount > 100:
- amount_recovered = 100 - party[current_hero]['HP']
- party[current_hero]['HP'] += amount_recovered
- print(f"{current_hero} healed for {amount_recovered} HP!")
- else:
- party[current_hero]['HP'] += amount
- print(f"{current_hero} healed for {amount} HP!")
- # Recharge function.
- # Adding the given amount of MP to the given hero MP value.
- # If the amount is exceeding the maximum allowed MP value (200), the recovered amount is calculated and
- # it is then added to the current MP.
- # Prints confirmation message.
- def recharge(party, current_hero, amount):
- if party[current_hero]['MP'] + amount > 200:
- amount_recovered = 200 - party[current_hero]['MP']
- party[current_hero]['MP'] += amount_recovered
- print(f"{current_hero} recharged for {amount_recovered} MP!")
- else:
- party[current_hero]['MP'] += amount
- print(f"{current_hero} recharged for {amount} MP!")
- # Take damage function.
- # Receives damage and attacker and it is applied to the current hero in the party.
- # Decreases current hero's HP by the given damage and checks the hero's current HP.
- # If the hero has remaining HP, the function prints the confirmation message accordingly.
- # If the hero's HP is lower than 0, the hero is killed. Hero is deleted from the party and confirmation message
- # is printed accordingly.
- def take_damage(party, current_hero, damage, attacker):
- party[current_hero]['HP'] -= damage
- if party[current_hero]['HP'] > 0:
- print(f"{current_hero} was hit for {damage} HP by {attacker} and now has {party[current_hero]['HP']} HP left!")
- else:
- print(f"{current_hero} has been killed by {attacker}!")
- del party[current_hero]
- # Checks if the current hero has enough MP, if so hero's MP is decreased and the spell is casted.
- # Confirmation message is printed accordingly.
- # If the hero does not have enough MP to cast the spell, it's MP is not decreased. Confirmation message
- # is printed accordingly.
- def cast_spell(party, current_hero, mp_needed, spell_name):
- if party[current_hero]['MP'] >= mp_needed:
- party[current_hero]['MP'] -= mp_needed
- print(f"{current_hero} has successfully cast {spell_name} and now has {party[current_hero]['MP']} MP!")
- else:
- print(f"{current_hero} does not have enough MP to cast {spell_name}!")
- # Main function.
- def main_func(heroes_count):
- # Dictionary, containing all heroes in the party.
- # Each hero has two attributes - HP and MP.
- party = {}
- # Looping through the given number of heroes in the party and adding each of them into the pre-made dictionary.
- # For every count, hero data is received as a string, and it is then separated onto different elements as follows:
- # name = hero_data[0], hp = hero_data[1], mp = hero_data[2]
- for count in range(heroes_count):
- hero_data = input().split()
- hero, hp, mp = hero_data[0], int(hero_data[1]), int(hero_data[2])
- if hero not in party.keys():
- party[hero] = {}
- party[hero]['HP'] = 0
- party[hero]['MP'] = 0
- party[hero]['HP'] = hp
- party[hero]['MP'] = mp
- # Receiving different commands, which are then split by " - ".
- # If command "End" is received, the loop ends and every live hero is printed along with its attributes.
- while True:
- command = input().split(' - ')
- if command[0] == 'End':
- for key, value in party.items():
- print(f"{key}\n HP: {value['HP']}\n MP: {value['MP']}")
- break
- # Calling out the pre-made functions accordingly.
- elif command[0] == 'CastSpell':
- current_hero, mp_needed, spell_name = command[1], int(command[2]), command[3]
- cast_spell(party, current_hero, mp_needed, spell_name)
- elif command[0] == 'TakeDamage':
- current_hero, damage, attacker = command[1], int(command[2]), command[3]
- take_damage(party, current_hero, damage, attacker)
- elif command[0] == 'Recharge':
- current_hero, amount = command[1], int(command[2])
- recharge(party, current_hero, amount)
- elif command[0] == 'Heal':
- current_hero, amount = command[1], int(command[2])
- heal(party, current_hero, amount)
- # Receiving number of heroes to be added to the party.
- number_of_heroes = int(input())
- # Calling out main function.
- main_func(number_of_heroes)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement