Advertisement
teodor_deks

Problem 3. Heroes of Code and Logic VII

Apr 4th, 2020
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.07 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class P03 {
  5.     static Map<String, Integer> heroHitPoints = new HashMap<>();
  6.     static Map<String, Integer> heroManaPoints = new HashMap<>();
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner(System.in);
  10.         int n = Integer.parseInt(sc.nextLine());
  11.  
  12.         for (int i = 0; i < n; i++) {
  13.             String hero = sc.nextLine();
  14.             String[] heroTokens = hero.split("\\s+");
  15.             String heroName = heroTokens[0];
  16.             int hPoints = Integer.parseInt(heroTokens[1]);
  17.             int mPoints = Integer.parseInt(heroTokens[2]);
  18.             add(heroName, hPoints, mPoints);
  19.         }
  20.         String input = sc.nextLine();
  21.         while (!input.equals("End")) {
  22.             String[] tokens = input.split(" - ");
  23.             String command = tokens[0];
  24.             String heroName = tokens[1];
  25.             int points = Integer.parseInt(tokens[2]);
  26.             switch (command) {
  27.                 case "CastSpell": {
  28.                     String spellName = tokens[3];
  29.  
  30.                     spell(heroName, points, spellName);
  31.                 }
  32.                 break;
  33.                 case "TakeDamage": {
  34.                     String attacker = tokens[3];
  35.  
  36.                     damage(heroName, points, attacker);
  37.                 }
  38.                 break;
  39.                 case "Recharge": {
  40.                     recharge(heroName, points);
  41.                 }
  42.                 break;
  43.                 case "Heal": {
  44.                     heal(heroName, points);
  45.                 }
  46.                 break;
  47.             }
  48.             input = sc.nextLine();
  49.         }
  50.         List<String> names = heroHitPoints.entrySet().stream()
  51.                 .sorted((a, b) -> {
  52.                     int healthA = a.getValue();
  53.                     String nameA = a.getKey();
  54.                     int healthB = b.getValue();
  55.                     String nameB = b.getKey();
  56.  
  57.                     if (healthA != healthB) {
  58.                         return Integer.compare(healthB, healthA);
  59.                     } else {
  60.                         return nameB.compareTo(nameA);
  61.                     }
  62.                 })
  63.                 .map(Map.Entry::getKey)
  64. //                .map(e -> e.getKey())
  65.                 .collect(Collectors.toList());
  66.  
  67.         for (String name : names) {
  68.             System.out.printf("%s%n HP: %d%n MP: %d%n"
  69.                     , name, heroHitPoints.get(name), heroManaPoints.get(name));
  70.         }
  71.     }
  72.     private static void heal(String heroName, int points) {
  73.         if (heroHitPoints.containsKey(heroName)) {
  74.             int hP = heroHitPoints.get(heroName);
  75.             int newHP = hP + points;
  76.             int healedPoints = 100 - hP;
  77.             if (newHP > 100) {
  78.                 newHP = 100;
  79.                 System.out.printf("%s healed for %d hP!%n", heroName, healedPoints);
  80.             }else {
  81.                 System.out.printf("%s healed for %d hP!%n", heroName, points);
  82.             }
  83.             heroHitPoints.put(heroName, newHP);
  84.         }
  85.     }
  86.     private static void recharge(String heroName, int points) {
  87.         if (heroManaPoints.containsKey(heroName)) {
  88.             int mP = heroManaPoints.get(heroName);
  89.             int newMP = mP + points;
  90.             int rechargedPoints = 200 - mP;
  91.             if (newMP > 200) {
  92.                 newMP = 200;
  93.                 System.out.printf("%s recharged for %d MP!%n", heroName, rechargedPoints);
  94.             }else {
  95.                 System.out.printf("%s recharged for %d MP!%n", heroName, points);
  96.  
  97.             }
  98.             heroManaPoints.put(heroName, newMP);
  99.         }
  100.     }
  101.     private static void damage(String heroName, int points, String attacker) {
  102.         if (heroHitPoints.containsKey(heroName)) {
  103.             int hP = heroHitPoints.get(heroName);
  104.             int newHP = hP - points;
  105.             if (newHP <= 0) {
  106.                 System.out.printf("%s has been killed by %s!%n", heroName, attacker);
  107.                 heroHitPoints.remove(heroName);
  108.                 heroManaPoints.remove(heroName);
  109.             } else {
  110.                 heroHitPoints.put(heroName, newHP);
  111.                 System.out.printf("%s was hit for %d HP by %s and now has %d HP left!%n"
  112.                         , heroName, points, attacker, newHP);
  113.             }
  114.         }
  115.     }
  116.     private static void spell(String heroName, int mpNeeded, String spellName) {
  117.         int mP = heroManaPoints.get(heroName);
  118.         int newMP = mP - mpNeeded;
  119.         if (heroManaPoints.containsKey(heroName)) {
  120.             if (newMP <= 0) {
  121.                 System.out.printf("%s does not have enough MP to cast %s!%n", heroName, spellName);
  122.             } else {
  123.                 System.out.printf("%s has successfully cast %s and now has %d MP!%n", heroName, spellName, newMP);
  124.             }
  125.         }
  126.         heroManaPoints.put(heroName, newMP);
  127.     }
  128.     private static void add(String heroName, int hPoints, int mPoints) {
  129.         heroHitPoints.put(heroName, hPoints);
  130.         heroManaPoints.put(heroName, mPoints);
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement