Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.39 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class Exam4 {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int n = Integer.parseInt(scanner.nextLine());
  9.         Map<String, Map<Integer, Integer>> heroes = new HashMap<>();
  10.  
  11.         for (int i = 0; i < n; i++) {
  12.             String heroStats = scanner.nextLine();
  13.             String[] tokens = heroStats.split(" ");
  14.             String name = tokens[0];
  15.             int hp = Integer.parseInt(tokens[1]);
  16.             int mana = Integer.parseInt(tokens[2]);
  17.             HashMap<Integer, Integer> nestedMap = new HashMap<>();
  18.             nestedMap.put(hp, mana);
  19.             heroes.put(name, nestedMap);
  20.         }
  21.  
  22.         String command = scanner.nextLine();
  23.         while (!command.equals("End")) {
  24.             String[] tokens = command.split(" - ");
  25.             if (tokens[0].equals("CastSpell")) {
  26.                 String heroName = tokens[1];
  27.                 int MP = Integer.parseInt(tokens[2]);
  28.                 String spellName = tokens[3];
  29.                 int heroMana = heroes.get(heroName).entrySet().iterator().next().getValue();
  30.  
  31.                 if (heroMana >= MP) {
  32.                     heroMana -= MP;
  33.                     heroes.get(heroName).entrySet().iterator().next().setValue(heroMana);
  34.                     System.out.println(heroName + " has successfully cast " + spellName + " and now has " + heroMana + " MP!");
  35.                 } else {
  36.                     System.out.println(heroName + " does not have enough MP to cast " + spellName + "!");
  37.                 }
  38.             } else if (tokens[0].equals("TakeDamage")) {
  39.                 String heroName = tokens[1];
  40.                 int damage = Integer.parseInt(tokens[2]);
  41.                 String attacker = tokens[3];
  42.                 int heroToAttackHP = heroes.get(heroName).entrySet().iterator().next().getKey();
  43.                 int hpLeft = heroToAttackHP - damage;
  44.  
  45.  
  46.                 if (hpLeft <= 0) {
  47.                     heroes.remove(heroName);
  48.                     System.out.println(heroName + " has been killed by " + attacker + "!");
  49.                 } else {
  50.                     System.out.println(heroName + " was hit for " + damage + " HP by " + attacker + " and now has " + hpLeft + " HP left!");
  51.                 }
  52.             } else if (tokens[0].equals("Recharge")) {
  53.                 String heroName = tokens[1];
  54.                 int manaGained = Integer.parseInt(tokens[2]);
  55.                 int heroMana = heroes.get(heroName).entrySet().iterator().next().getValue();
  56.                 int increasedMana = manaGained + heroMana;
  57.  
  58.                 if (increasedMana > 200) {
  59.                     increasedMana = 200;
  60.                 }
  61.  
  62.                 System.out.println(heroName + " recharged for " + manaGained + "MP!");
  63.             } else if (tokens[0].equals("Heal")) {
  64.                 String heroName = tokens[1];
  65.                 int healthGained = Integer.parseInt(tokens[2]);
  66.                 int heroHealth = heroes.get(heroName).entrySet().iterator().next().getKey();
  67.                 int increasedHealth = healthGained + heroHealth;
  68.  
  69.                 if (increasedHealth > 100) {
  70.                     increasedHealth = 100;
  71.                 }
  72.  
  73.                 System.out.println(heroName + " healed for " + healthGained + "HP!");
  74.             }
  75.             command = scanner.nextLine();
  76.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement