Advertisement
Lyubohd

Heroes of Code and Logic VII

Apr 4th, 2020
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         Map<String, List<Integer>> heroes = new HashMap<>();
  7.         int n = Integer.parseInt(scan.nextLine());
  8.  
  9.         for (int i = 0; i < n; i++) {
  10.             String[] inputTokens = scan.nextLine().split(" ");
  11.             heroes.put(inputTokens[0], new ArrayList<>());
  12.             heroes.get(inputTokens[0]).add(Integer.parseInt(inputTokens[1]));
  13.             heroes.get(inputTokens[0]).add(Integer.parseInt(inputTokens[2]));
  14.         }
  15.  
  16.         String input = scan.nextLine();
  17.         while (!"End".equals(input)) {
  18.             String[] tokens = input.split(" - ");
  19.             String command = tokens[0];
  20.             String heroName = tokens[1];
  21.             switch (command) {
  22.                 case "CastSpell": {
  23.                     int neededManaPoints = Integer.parseInt(tokens[2]);
  24.                     String spellName = tokens[3];
  25.                     int currentMana = heroes.get(heroName).get(1);
  26.                     if (currentMana >= neededManaPoints) {
  27.                         int manaLeft = currentMana - neededManaPoints;
  28.                         System.out.println(String.format("%s has successfully cast %s and now has %d MP!",
  29.                                 heroName, spellName, manaLeft));
  30.                         heroes.get(heroName).set(1, manaLeft);
  31.                     } else {
  32.                         System.out.println(String.format("%s does not have enough MP to cast %s!",
  33.                                 heroName, spellName));
  34.                     }
  35.                 }
  36.                 break;
  37.                 case "TakeDamage": {
  38.                     int damage = Integer.parseInt(tokens[2]);
  39.                     String attacker = tokens[3];
  40.                     int currentHealth = heroes.get(heroName).get(0);
  41.                     if (currentHealth > damage) {
  42.                         int healthLeft = currentHealth - damage;
  43.                         System.out.println(String.format("%s was hit for %d HP by %s and now has %d HP left!",
  44.                                 heroName, damage, attacker, healthLeft));
  45.                         heroes.get(heroName).set(0, healthLeft);
  46.                     } else {
  47.                         heroes.remove(heroName);
  48.                         System.out.println(String.format("%s has been killed by %s!",
  49.                                 heroName, attacker));
  50.                     }
  51.                 }
  52.                 break;
  53.                 case "Recharge": {
  54.                     int amount = Integer.parseInt(tokens[2]);
  55.                     int currentMana = heroes.get(heroName).get(1);
  56.                     if (currentMana + amount >= 200) {
  57.                         amount = 200 - currentMana;
  58.                         currentMana = 200;
  59.                     } else {
  60.                         currentMana += amount;
  61.                     }
  62.                     heroes.get(heroName).set(1, currentMana);
  63.                     System.out.println(String.format("%s recharged for %d MP!", heroName, amount));
  64.                 }
  65.                 break;
  66.                 case "Heal": {
  67.                     int amount = Integer.parseInt(tokens[2]);
  68.                     int currentHealth = heroes.get(heroName).get(0);
  69.                     if (currentHealth + amount >= 100) {
  70.                         amount = 100 - currentHealth;
  71.                         currentHealth = 100;
  72.                     } else {
  73.                         currentHealth += amount;
  74.                     }
  75.                     heroes.get(heroName).set(0, currentHealth);
  76.                     System.out.println(String.format("%s healed for %d HP!", heroName, amount));
  77.                 }
  78.                 break;
  79.             }
  80.             input = scan.nextLine();
  81.         }
  82.  
  83.         heroes
  84.                 .entrySet()
  85.                 .stream()
  86.                 .sorted((h1, h2) -> {
  87.                     int result = h2.getValue().get(0) - h1.getValue().get(0);
  88.                     if (result == 0) {
  89.                         result = h1.getKey().compareTo(h2.getKey());
  90.                     }
  91.                     return result;
  92.                 })
  93.                 .forEach(h -> System.out.println(String.format("%s%n  HP: %d%n  MP: %d",
  94.                         h.getKey(), h.getValue().get(0), h.getValue().get(1))));
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement