Advertisement
Guest User

heroes

a guest
Apr 4th, 2020
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.03 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class heroes {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         Map<String, List<Integer>> heroesTo_Hp_To_Mp = new TreeMap<>();
  7.         int numOfHeroes = Integer.parseInt(scan.nextLine());
  8.         for (int i = 0; i < numOfHeroes ; i++) {
  9.             String [] splitOfNumOfHeroes = scan.nextLine().split("\\s+");
  10.  
  11.             String heroe = splitOfNumOfHeroes[0];
  12.             int hitPoints = Integer.parseInt(splitOfNumOfHeroes[1]);
  13.             int manaPoints = Integer.parseInt(splitOfNumOfHeroes[2]);
  14.  
  15.             List<Integer> listForAdd = new ArrayList<>();
  16.             heroesTo_Hp_To_Mp.put(heroe,listForAdd);
  17.             heroesTo_Hp_To_Mp.get(heroe).add(0,hitPoints);
  18.             heroesTo_Hp_To_Mp.get(heroe).add(1,manaPoints);
  19.         }
  20.         String secondInput = scan.nextLine();
  21.         while (!secondInput.equals("End")){
  22.             String [] splitSecondInput = secondInput.split(" - ");
  23.  
  24.             String typeOfCommand = splitSecondInput[0];
  25.             String nameOfhero = splitSecondInput[1];
  26.  
  27.             switch (typeOfCommand){
  28.                 case "CastSpell": {
  29.                     String spellname = splitSecondInput[3];
  30.                     int MPneeded = Integer.parseInt(splitSecondInput[2]);
  31.                     int oldMP = heroesTo_Hp_To_Mp.get(nameOfhero).get(1);
  32.  
  33.                     if (oldMP - MPneeded < 0) {
  34.                         System.out.printf("%s does not have enough MP to cast %s!", nameOfhero, spellname).println();
  35.                     } else {
  36.                         heroesTo_Hp_To_Mp.get(nameOfhero).set(1, oldMP - MPneeded);
  37.                         System.out.printf("%s has successfully cast %s and now has %d MP!", nameOfhero, spellname,
  38.                                 heroesTo_Hp_To_Mp.get(nameOfhero).get(1)).println();
  39.                     }
  40.                 }
  41.                     break;
  42.                 case "TakeDamage": {
  43.                     String attacker = splitSecondInput[3];
  44.                     int damage = Integer.parseInt(splitSecondInput[2]);
  45.                     int HPafterDamage = heroesTo_Hp_To_Mp.get(nameOfhero).get(0) - damage;
  46.                     if (HPafterDamage > 0) {
  47.                         heroesTo_Hp_To_Mp.get(nameOfhero).set(0, HPafterDamage);
  48.                         System.out.printf("%s was hit for %d " +
  49.                                 "HP by %s and now has %d HP left!"
  50.                                 , nameOfhero, damage, attacker, HPafterDamage).println();
  51.                     } else {
  52.                         heroesTo_Hp_To_Mp.remove(nameOfhero);
  53.                         System.out.printf("%s has been killed by %s!", nameOfhero, attacker).println();
  54.                     }
  55.                     break;
  56.                 }
  57.                 case "Recharge": {
  58.                     int lastMP = 0;
  59.                     int amountForRecharge = Integer.parseInt(splitSecondInput[2]);
  60.                     int currentMP = heroesTo_Hp_To_Mp.get(nameOfhero).get(1);
  61.                     int MPafterRecharged = heroesTo_Hp_To_Mp.get(nameOfhero).get(1) + amountForRecharge;
  62.                    
  63.                     if (MPafterRecharged > 200) {
  64.                         lastMP = 200 - currentMP;
  65.                         MPafterRecharged = 200;
  66.                         heroesTo_Hp_To_Mp.get(nameOfhero).set(1, MPafterRecharged);
  67.                     } else {
  68.                         lastMP = amountForRecharge;
  69.                         heroesTo_Hp_To_Mp.get(nameOfhero).set(1, MPafterRecharged);
  70.                     }
  71.                     System.out.printf("%s recharged for %d MP!", nameOfhero, lastMP).println();
  72.                 }
  73.  
  74.                     break;
  75.  
  76.                 case "Heal": {
  77.                     int lastHP = 0;
  78.                     int amountHP = Integer.parseInt(splitSecondInput[2]);
  79.                     int currentHp = heroesTo_Hp_To_Mp.get(nameOfhero).get(0);
  80.                     int newHP = heroesTo_Hp_To_Mp.get(nameOfhero).get(0) + amountHP;
  81.  
  82.                     if (newHP > 100) {
  83.                         lastHP = 100 - currentHp;
  84.                         newHP = 100;
  85.                     } else {
  86.                         lastHP = amountHP;
  87.                     }
  88.                     heroesTo_Hp_To_Mp.get(nameOfhero).set(0, newHP);
  89.                     System.out.printf("%s healed for " +
  90.                             "%d HP!", nameOfhero, lastHP).println();
  91.                 }
  92.                     break;
  93.  
  94.             }
  95.             secondInput =scan.nextLine();
  96.         }
  97.        
  98.         heroesTo_Hp_To_Mp
  99.                 .entrySet()
  100.                 .stream()
  101.                 .sorted((a,b)->Integer.compare(b.getValue().get(0),a.getValue().get(0)))
  102.                 .forEach(e->{
  103.                     String hero = e.getKey();
  104.                     System.out.println(hero);
  105.                     System.out.println(String.format("HP: %d",e.getValue().get(0)));
  106.                     System.out.println(String.format("MP: %d",e.getValue().get(1)));
  107.  
  108.                 });
  109.  
  110.  
  111.  
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement