desislava_topuzakova

3. Heroes of Code and Logic VII

Aug 6th, 2021
1,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.36 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.TreeMap;
  5.  
  6. public class HeroesofCodeAndLogic_03 {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int n = Integer.parseInt(scanner.nextLine()); //count of heroes
  10.         //1 ЧАСТ: ДАННИ ЗА ГЕРОЯ
  11.  
  12.         //герои(текст) -> mp(цяло число)
  13.         Map<String, Integer> heroesMP = new TreeMap<>();
  14.  
  15.         //герои(текст) -> hp(цяло число)
  16.         Map<String, Integer> heroesHP = new TreeMap<>();
  17.  
  18.         for (int hero = 1; hero <= n; hero++) {
  19.             String data = scanner.nextLine();//"Solmyr 85 120"
  20.             String[] splitData = data.split("\\s+"); // ["Solmyr", "85", "120"]
  21.             String heroName = splitData[0];
  22.             int hp = Integer.parseInt(splitData[1]);
  23.             int mp = Integer.parseInt(splitData[2]);
  24.  
  25.             //max hp e 100
  26.             if (hp <= 100) {
  27.                 heroesHP.put(heroName, hp);
  28.             }
  29.  
  30.             //map mp e 200
  31.             if (mp <= 200) {
  32.                 heroesMP.put(heroName, mp);
  33.             }
  34.         }
  35.  
  36.  
  37.         //2. КОМАНДИ
  38.         String command = scanner.nextLine();
  39.         //"CastSpell – {hero name} – {MP needed} – {spell name}" -> ["CastSpell", "{hero name}", "{MP needed}", "{spell name}"]
  40.         //"TakeDamage – {hero name} – {damage} – {attacker}" ->     ["TakeDamage", "{hero name}", "{damage}", "{attacker}"]
  41.         //"Recharge – {hero name} – {amount}" ->                    ["Recharge", "{hero name}", "{amount}"]
  42.         //"Heal – {hero name} – {amount}" ->                        ["Heal", "{hero name}", "{amount}"]
  43.         while(!command.equals("End")) {
  44.             String [] commandParams = command.split(" - ");
  45.             String commandName = commandParams[0];//"CastSpell", "TakeDamage", "Recharge", "Heal"
  46.             String heroName = commandParams[1];
  47.             switch(commandName) {
  48.                 case "CastSpell":
  49.                     int mpNeeded = Integer.parseInt(commandParams[2]);
  50.                     String spellName = commandParams[3];
  51.                     int currentMp = heroesMP.get(heroName);
  52.                     //изпълняваме магията
  53.                     if(currentMp >= mpNeeded) {
  54.                         int mpLeft = currentMp - mpNeeded;
  55.                         heroesMP.put(heroName, mpLeft);
  56.                         System.out.printf("%s has successfully cast %s and now has %d MP!%n", heroName, spellName, mpLeft);
  57.                     }
  58.                     //ако не я изпълняваме
  59.                     else {
  60.                         System.out.printf("%s does not have enough MP to cast %s!%n", heroName, spellName);
  61.                     }
  62.                     break;
  63.                 case "TakeDamage":
  64.                     int damage = Integer.parseInt(commandParams[2]);
  65.                     String attacker = commandParams[3];
  66.                     int currentHp = heroesHP.get(heroName);
  67.                     int leftHp = currentHp - damage;
  68.                     //жив
  69.                     if(leftHp > 0) {
  70.                         heroesHP.put(heroName, leftHp);
  71.                         System.out.printf("%s was hit for %d HP by %s and now has %d HP left!%n", heroName, damage, attacker, leftHp);
  72.                     }
  73.                     //умрял
  74.                     else {
  75.                         System.out.printf("%s has been killed by %s!%n", heroName, attacker);
  76.                         heroesHP.remove(heroName);
  77.                         heroesMP.remove(heroName);
  78.                     }
  79.                     break;
  80.                 case "Recharge": //increase mp
  81.                     int amountRecharge = Integer.parseInt(commandParams[2]);
  82.                     int currentMpForRecharge = heroesMP.get(heroName);
  83.                     int increaseMP = currentMpForRecharge + amountRecharge;
  84.                     if(increaseMP > 200) {
  85.                         increaseMP = 200;
  86.                     }
  87.                     heroesMP.put(heroName, increaseMP);
  88.                     System.out.printf("%s recharged for %d MP!%n", heroName, increaseMP - currentMpForRecharge);
  89.                     break;
  90.                 case "Heal":
  91.                     int amountHeal = Integer.parseInt(commandParams[2]);
  92.                     int currentHPForHeal = heroesHP.get(heroName);
  93.                     int increasedHP = currentHPForHeal + amountHeal;
  94.                     if(increasedHP > 100) {
  95.                         increasedHP = 100;
  96.                     }
  97.                     heroesHP.put(heroName, increasedHP);
  98.                     System.out.printf("%s healed for %d HP!%n", heroName, increasedHP - currentHPForHeal);
  99.                     break;
  100.             }
  101.             command = scanner.nextLine();
  102.         }
  103.  
  104.         heroesHP.entrySet().stream()
  105.                 //име на героя -> hp
  106.                 .sorted((h1, h2) -> Integer.compare(h2.getValue(), h1.getValue()))
  107.                 .forEach(heroEntry -> {
  108.                     String heroName = heroEntry.getKey();
  109.                     System.out.println(heroName);
  110.                     System.out.println("  HP: " + heroEntry.getValue());
  111.                     System.out.println("  MP: " + heroesMP.get(heroName));
  112.                 });
  113.  
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment