desislava_topuzakova

03. Heroes of Code and Logic VII

Mar 26th, 2021
1,632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 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 HeroesOfCodesAndLogic_03 {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         Map<String, Integer> heroesHP = new TreeMap<>(); //Име на героя -> HP
  10.         Map<String, Integer> heroesMP = new TreeMap<>(); //Име на героя -> MP
  11.  
  12.         int countHeroes = Integer.parseInt(scanner.nextLine());
  13.         for (int hero = 1; hero <= countHeroes ; hero++) {
  14.             String input = scanner.nextLine();
  15.             //"Solmyr 85 120" -> split " " -> ["Solmyr", "85", "120"]
  16.             String [] tokens = input.split("\\s+");
  17.             //["Solmyr", "85", "120"]
  18.             String heroName = tokens[0]; //"Solmyr"
  19.             int hp = Integer.parseInt(tokens[1]); //"85" -> parse -> 85
  20.             int mp = Integer.parseInt(tokens[2]); //"120" -> parse -> 120
  21.  
  22.             //hp
  23.             if(hp <= 100) {
  24.                 //име на герой -> hp
  25.                 heroesHP.put(heroName, hp);
  26.             }
  27.  
  28.             //mp
  29.             if(mp <= 200) {
  30.                 heroesMP.put(heroName, mp);
  31.             }
  32.         }
  33.  
  34.  
  35.         String command = scanner.nextLine();
  36.  
  37.         while(!command.equals("End")){
  38.             String [] tokens = command.split(" - ");
  39.             String commandName = tokens[0];
  40.             String heroName = tokens[1];
  41.  
  42.             switch(commandName) {
  43.                 case "CastSpell":
  44.                    //tokens = ["CastSpell", "{heroName}", "80", "Dracarys"]
  45.                     int mpNeeded = Integer.parseInt(tokens[2]);
  46.                     String spellName = tokens[3];
  47.                     int mpCurrent = heroesMP.get(heroName);
  48.  
  49.                     if(mpCurrent >= mpNeeded){
  50.                         //мога да направя магията -> текущото MP >= нужното
  51.                         int mpLeft = mpCurrent - mpNeeded;
  52.                         heroesMP.put(heroName, mpLeft);
  53.                         System.out.printf("%s has successfully cast %s and now has %d MP!%n", heroName, spellName, mpLeft);
  54.                     } else {
  55.                         //не мога
  56.                         System.out.printf("%s does not have enough MP to cast %s!%n", heroName, spellName);
  57.                     }
  58.                     break;
  59.                 case "TakeDamage":
  60.                     //tokens = ["TakeDamage", "{hero name}", "{damage}", "{attacker}"]
  61.                     int damage = Integer.parseInt(tokens[2]);
  62.                     String attacker = tokens[3];
  63.                     int currentHP = heroesHP.get(heroName) - damage;
  64.                     if(currentHP > 0) {
  65.                         //жив
  66.                         heroesHP.put(heroName, currentHP);
  67.                         System.out.printf("%s was hit for %d HP by %s and now has %d HP left!%n", heroName, damage, attacker, currentHP);
  68.                     } else {
  69.                         //умрял
  70.                         System.out.printf("%s has been killed by %s!%n", heroName, attacker);
  71.                         heroesHP.remove(heroName);
  72.                         heroesMP.remove(heroName);
  73.                     }
  74.                     break;
  75.                 case "Recharge":
  76.                     //tokens = ["Recharge", "{hero name}", "{amount}"];
  77.                     int amountIncrease = Integer.parseInt(tokens[2]);
  78.                     int currentMp = heroesMP.get(heroName);
  79.                     int increaseMp = currentMp + amountIncrease;
  80.                     if(increaseMp > 200) {
  81.                         increaseMp = 200;
  82.                     }
  83.                     heroesMP.put(heroName, increaseMp);
  84.                     System.out.printf("%s recharged for %d MP!%n", heroName, increaseMp - currentMp);
  85.                     break;
  86.                 case "Heal":
  87.                     //tokens = ["Heal", "{hero name}", "{amount}"];
  88.                     int amount = Integer.parseInt(tokens[2]);
  89.                     int currentHp = heroesHP.get(heroName);
  90.                     int increaseHp = currentHp + amount;
  91.                     if(increaseHp > 100) {
  92.                         increaseHp = 100;
  93.                     }
  94.                     heroesHP.put(heroName, increaseHp);
  95.                     System.out.printf("%s healed for %d HP!%n", heroName, increaseHp - currentHp);
  96.                     break;
  97.             }
  98.  
  99.  
  100.             command = scanner.nextLine();
  101.         }
  102.  
  103.         //име на герой -> hp
  104.         heroesHP.entrySet().stream()
  105.                 .sorted((h1, h2) -> Integer.compare(h2.getValue(), h1.getValue()))
  106.                 //име -> hp
  107.                 .forEach(hero -> {
  108.                     String heroName = hero.getKey();
  109.                     System.out.println(heroName);
  110.                     System.out.println("  HP: " + hero.getValue());
  111.                     System.out.println("  MP: " + heroesMP.get(heroName));
  112.                 });
  113.  
  114.  
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment