Didart

Heroes of Code and Logic VII

Nov 28th, 2021
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4.  
  5. public class HeroesOfCodeAndLogicVII {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         int countHeroes = Integer.parseInt(scanner.nextLine());
  10.  
  11.         Map<String, Integer> hitPoints = new HashMap<>();
  12.         Map<String, Integer> manaPoints = new HashMap<>();
  13.  
  14.         for (int index = 0; index < countHeroes; index++) {
  15.             String[] heroes = scanner.nextLine().split("\\s+");
  16.  
  17.             String heroName = heroes[0];
  18.             int hPoints = Integer.parseInt(heroes[1]);
  19.             int mPoints = Integer.parseInt(heroes[2]);
  20.  
  21.             hitPoints.putIfAbsent(heroName, hPoints);
  22.             manaPoints.putIfAbsent(heroName, mPoints);
  23.  
  24.             if (manaPoints.get(heroName) > 200) {
  25.                 manaPoints.put(heroName, 200);
  26.             }
  27.             if (hitPoints.get(heroName) > 100) {
  28.                 hitPoints.put(heroName, 100);
  29.             }
  30.         }
  31.  
  32.         String input = scanner.nextLine();
  33.         while (!input.equals("End")) {
  34.             String[] action = input.split("-");
  35.             String command = action[0].trim();
  36.             String name = action[1].trim();
  37.             switch (command) {
  38.                 case "CastSpell": {
  39.                     int neededMP = Integer.parseInt(action[2].trim());
  40.                     String spellName = action[3].trim();
  41.                     int currentMP = manaPoints.get(name);
  42.                     int leftMP = currentMP - neededMP;
  43.                     if (leftMP >= 0) {
  44.                         System.out.printf("%s has successfully cast %s and now has %d MP!%n",
  45.                                 name, spellName, leftMP);
  46.                         manaPoints.put(name, leftMP);
  47.                     } else {
  48.                         System.out.printf("%s does not have enough MP to cast %s!%n", name, spellName);
  49.                     }
  50.                     break;
  51.                 }
  52.                 case "TakeDamage": {
  53.                     int damage = Integer.parseInt(action[2].trim());
  54.                     String attacker = action[3].trim();
  55.                     int leftHP = hitPoints.get(name) - damage;
  56.                     if (leftHP > 0) {
  57.                         hitPoints.put(name, leftHP);
  58.                         System.out.printf("%s was hit for %d HP by %s and now has %d HP left!%n",
  59.                                 name, damage, attacker, leftHP);
  60.                     } else {
  61.                         System.out.printf("%s has been killed by %s!%n", name, attacker);
  62.                         hitPoints.remove(name);
  63.                         manaPoints.remove(name);
  64.                     }
  65.                     break;
  66.                 }
  67.                 case "Recharge": {
  68.                     int amountMP = Integer.parseInt(action[2].trim());
  69.                     int currentMP = manaPoints.get(name);
  70.                     int newMP = currentMP + amountMP;
  71.                     if (newMP > 200) {
  72.                         manaPoints.put(name, 200);
  73.                         System.out.printf("%s recharged for %d MP!%n", name, 200 - currentMP);
  74.                     } else {
  75.                         manaPoints.put(name, newMP);
  76.                         System.out.printf("%s recharged for %d MP!%n", name, amountMP);
  77.                     }
  78.                     break;
  79.                 }
  80.                 case "Heal": {
  81.                     int amountHP = Integer.parseInt(action[2].trim());
  82.                     int currentHp = hitPoints.get(name);
  83.                     int newHP = currentHp + amountHP;
  84.                     if (newHP > 100) {
  85.                         hitPoints.put(name, 100);
  86.                         System.out.printf("%s healed for %d HP!%n", name, 100 - currentHp);
  87.                     } else {
  88.                         hitPoints.put(name, newHP);
  89.                         System.out.printf("%s healed for %d HP!%n", name, amountHP);
  90.                     }
  91.                     break;
  92.                 }
  93.             }
  94.             input = scanner.nextLine();
  95.         }
  96.         hitPoints
  97.                 .entrySet()
  98.                 .stream()
  99.                 .sorted((e1, e2) -> {
  100.                     int result = Integer.compare(e2.getValue(), e1.getValue());
  101.                     if (result == 0) {
  102.                         result = e1.getKey().compareTo(e2.getKey());
  103.                     }
  104.                     return result;
  105.                 }).forEach(item -> {
  106.             System.out.printf("%s%n  HP: %d%n  MP: %d%n",
  107.                     item.getKey(), item.getValue(), manaPoints.get(item.getKey()));
  108.         });
  109.     }
  110. }
  111.  
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment