Advertisement
Guest User

third

a guest
Apr 4th, 2020
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3.  
  4. public class third {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int n = Integer.parseInt(scanner.nextLine());
  9.  
  10.         HashMap<String, int[]> heroes = new HashMap<>();
  11.  
  12.         while (n-- > 0) {
  13.             String[] tokens = scanner.nextLine().split("\\s+");
  14.             String name = tokens[0];
  15.             int hp = Integer.parseInt(tokens[1]);
  16.             int mp = Integer.parseInt(tokens[2]);
  17.             heroes.putIfAbsent(name, new int[]{hp, mp});
  18.         }
  19.  
  20.         String command = scanner.nextLine();
  21.  
  22.         while (!"End".equals(command)) {
  23.             String[] tokens = command.split(" - ");
  24.             switch (tokens[0]) {
  25.                 case "CastSpell": {
  26.                     String hero = tokens[1];
  27.                     int mana = Integer.parseInt(tokens[2]);
  28.                     String spell = tokens[3];
  29.  
  30.                     if (heroes.get(hero)[1] >= mana) {
  31.                         heroes.get(hero)[1] -= mana;
  32.                         System.out.println(String.format("%s has successfully cast %s and now has %d MP!",
  33.                                 hero, spell, heroes.get(hero)[1]));
  34.                     } else {
  35.                         System.out.println(String.format("%s does not have enough MP to cast %s!",
  36.                                 hero, spell));
  37.                     }
  38.                 }
  39.                 break;
  40.  
  41.                 case "TakeDamage": {
  42.                     String name = tokens[1];
  43.                     int damage = Integer.parseInt(tokens[2]);
  44.                     String attacker = tokens[3];
  45.  
  46.                     heroes.get(name)[0] -= damage;
  47.  
  48.                     if (heroes.get(name)[0] > 0) {
  49.                         System.out.println(String.format("%s was hit for %d HP by %s and now has %d HP left!",
  50.                                 name, damage, attacker, heroes.get(name)[0]));
  51.                     } else {
  52.                         heroes.remove(name);
  53.                         System.out.println(String.format("%s has been killed by %s!", name, attacker));
  54.                     }
  55.                 }
  56.                 break;
  57.  
  58.                 case "Recharge": {
  59.  
  60.                     String name = tokens[1];
  61.                     int mana = Integer.parseInt(tokens[2]);
  62.  
  63.                     if (heroes.get(name)[1] + mana <= 200) {
  64.                         heroes.get(name)[1] += mana;
  65.                         System.out.println(String.format("%s recharged for %d MP!", name, mana));
  66.                     } else {
  67.                         int recovered = 200 - heroes.get(name)[1];
  68.                         heroes.get(name)[1] = 200;
  69.                         System.out.println(String.format("%s recharged for %d MP!", name, recovered));
  70.                     }
  71.                     // maybe check for not full mana
  72.                 }
  73.                 break;
  74.  
  75.                 case "Heal":
  76.                     String name = tokens[1];
  77.                     int health = Integer.parseInt(tokens[2]);
  78.  
  79.                     if (heroes.get(name)[0] + health <= 100) {
  80.                         heroes.get(name)[0] += health;
  81.                         System.out.println(String.format("%s healed for %d HP!", name, health));
  82.                     } else {
  83.                         int heal = 100 - heroes.get(name)[0];
  84.                         heroes.get(name)[0] = 100;
  85.                         System.out.println(String.format("%s healed for %d HP!", name, heal));
  86.                     }
  87.                     break;
  88.             }
  89.  
  90.             command = scanner.nextLine();
  91.         }
  92.  
  93.         heroes
  94.                 .entrySet()
  95.                 .stream()
  96.                 .sorted((h1, h2) -> {
  97.                     int one = h1.getValue()[0];
  98.                     int two = h2.getValue()[0];
  99.                     if (two - one == 0) {
  100.                         return h1.getKey().compareTo(h2.getKey());
  101.                     }
  102.                     return two - one;
  103.                 }).forEach(h -> {
  104.             System.out.println(h.getKey());
  105.             System.out.println(String.format("  HP: %d", h.getValue()[0]));
  106.             System.out.println(String.format("  MP: %d", h.getValue()[1]));
  107.         });
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement