Advertisement
Guest User

HeroesOfCodeAndLogicVII

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