Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class Exam4 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- Map<String, Map<Integer, Integer>> heroes = new HashMap<>();
- for (int i = 0; i < n; i++) {
- String heroStats = scanner.nextLine();
- String[] tokens = heroStats.split(" ");
- String name = tokens[0];
- int hp = Integer.parseInt(tokens[1]);
- int mana = Integer.parseInt(tokens[2]);
- HashMap<Integer, Integer> nestedMap = new HashMap<>();
- nestedMap.put(hp, mana);
- heroes.put(name, nestedMap);
- }
- String command = scanner.nextLine();
- while (!command.equals("End")) {
- String[] tokens = command.split(" - ");
- if (tokens[0].equals("CastSpell")) {
- String heroName = tokens[1];
- int MP = Integer.parseInt(tokens[2]);
- String spellName = tokens[3];
- int heroMana = heroes.get(heroName).entrySet().iterator().next().getValue();
- if (heroMana >= MP) {
- heroMana -= MP;
- heroes.get(heroName).entrySet().iterator().next().setValue(heroMana);
- System.out.println(heroName + " has successfully cast " + spellName + " and now has " + heroMana + " MP!");
- } else {
- System.out.println(heroName + " does not have enough MP to cast " + spellName + "!");
- }
- } else if (tokens[0].equals("TakeDamage")) {
- String heroName = tokens[1];
- int damage = Integer.parseInt(tokens[2]);
- String attacker = tokens[3];
- int heroToAttackHP = heroes.get(heroName).entrySet().iterator().next().getKey();
- int hpLeft = heroToAttackHP - damage;
- if (hpLeft <= 0) {
- heroes.remove(heroName);
- System.out.println(heroName + " has been killed by " + attacker + "!");
- } else {
- System.out.println(heroName + " was hit for " + damage + " HP by " + attacker + " and now has " + hpLeft + " HP left!");
- }
- } else if (tokens[0].equals("Recharge")) {
- String heroName = tokens[1];
- int manaGained = Integer.parseInt(tokens[2]);
- int heroMana = heroes.get(heroName).entrySet().iterator().next().getValue();
- int increasedMana = manaGained + heroMana;
- if (increasedMana > 200) {
- increasedMana = 200;
- }
- System.out.println(heroName + " recharged for " + manaGained + "MP!");
- } else if (tokens[0].equals("Heal")) {
- String heroName = tokens[1];
- int healthGained = Integer.parseInt(tokens[2]);
- int heroHealth = heroes.get(heroName).entrySet().iterator().next().getKey();
- int increasedHealth = healthGained + heroHealth;
- if (increasedHealth > 100) {
- increasedHealth = 100;
- }
- System.out.println(heroName + " healed for " + healthGained + "HP!");
- }
- command = scanner.nextLine();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement