Advertisement
desislava_topuzakova

03. Heroes of Code and Logic VII

Nov 25th, 2022
1,406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.71 KB | None | 0 0
  1. package exam_preparation;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class FirstTask {
  8.     public static void main(String[] args) {
  9.        Scanner scanner = new Scanner(System.in);
  10.        //име на героя -> hp
  11.         Map<String, Integer> heroesHP = new LinkedHashMap<>();
  12.  
  13.         //име на героя -> mp
  14.         Map<String, Integer> heroesMP = new LinkedHashMap<>();
  15.  
  16.         int countHeroes = Integer.parseInt(scanner.nextLine()); //брой на героите
  17.         for (int hero = 1; hero <=  countHeroes; hero++) {
  18.             String heroInfo = scanner.nextLine(); //"Solmyr 85 120"
  19.             //"Solmyr 85 120".split(" ") -> ["Solymur", "85", "120"]
  20.             String [] heroData = heroInfo.split("\\s+"); //["Solymur", "85", "120"]
  21.             String heroName = heroData[0]; //име на героя
  22.             int hp = Integer.parseInt(heroData[1]); //хит точки на героя
  23.             int mp = Integer.parseInt(heroData[2]); //мана точки на героя
  24.  
  25.             //проверка за hp -> hp <= 100
  26.             if (hp <= 100) {
  27.                 //име на герой -> hp
  28.                 heroesHP.put(heroName, hp);
  29.             }
  30.  
  31.             //проверка за mp -> mp <= 200
  32.             if (mp <= 200) {
  33.                 //име на герой -> mp
  34.                 heroesMP.put(heroName, mp);
  35.             }
  36.         }
  37.         //всеки един герой колко hp има (heroesHP)
  38.         //всеки един герой колко mp има (heroesMP)
  39.  
  40.         String command = scanner.nextLine();
  41.  
  42.         while (!command.equals("End")) {
  43.  
  44.             if (command.contains("CastSpell")) {
  45.                 //1. command = "CastSpell – {hero name} – {MP needed} – {spell name}".split("\\s+-\\s+") -> ["CastSpell", "{hero name}", "{MP needed}", "{spell name}"]
  46.                 String heroName = command.split("\\s+-\\s+")[1]; //герой
  47.                 int mpNeeded = Integer.parseInt(command.split("\\s+-\\s+")[2]); //нужни точки за магията
  48.                 String spellName = command.split("\\s+-\\s+")[3]; //магията
  49.  
  50.                 int currentMP = heroesMP.get(heroName); //mp на героя
  51.  
  52.                 //1. да можем да направим магията -> текущотоMP >= mpNeeded
  53.                 if(currentMP >= mpNeeded) {
  54.                     //ДА! ПРАВИМ МАГИЯ!
  55.                     int mpLeft = currentMP - mpNeeded; //точки останали след направата на магията
  56.                     heroesMP.put(heroName, mpLeft); //намаляме стойността на mp
  57.                     System.out.printf("%s has successfully cast %s and now has %d MP!%n", heroName, spellName, mpLeft);
  58.                 }
  59.                 //2. НЕ правим магията -> текущотоMP < mpNeeded
  60.                 else {
  61.                     //НE!!!
  62.                     System.out.printf("%s does not have enough MP to cast %s!%n", heroName, spellName);
  63.                 }
  64.             } else if (command.contains("TakeDamage")) {
  65.                 //2. command = "TakeDamage – {hero name} – {damage} – {attacker}".split("\\s+-\\s+") -> ["TakeDamage", "{hero name}", "{damage}", "{attacker}"]
  66.                 String heroName = command.split("\\s+-\\s+")[1]; //герой
  67.                 int damage = Integer.parseInt(command.split("\\s+-\\s+")[2]); //отнети точки
  68.                 String attacker = command.split("\\s+-\\s+")[3]; //атакуващия
  69.  
  70.                 int currentHP = heroesHP.get(heroName);
  71.                 //атакa
  72.                 currentHP -= damage;
  73.  
  74.                 //проверка дали е жив след атаката
  75.                 if (currentHP > 0) {
  76.                     //жив
  77.                     System.out.printf("%s was hit for %d HP by %s and now has %d HP left!%n", heroName, damage, attacker, currentHP);
  78.                     heroesHP.put(heroName, currentHP);
  79.                 } else {
  80.                     //умрял
  81.                     System.out.printf("%s has been killed by %s!%n", heroName, attacker);
  82.                     heroesHP.remove(heroName);
  83.                     heroesMP.remove(heroName);
  84.                 }
  85.  
  86.             } else if (command.contains("Recharge")) {
  87.                 //3. command = "Recharge – {hero name} – {amount}".split("\\s+-\\s+") -> ["Recharge", "{hero name}", "{amount}"]
  88.                 String heroName = command.split("\\s+-\\s+")[1]; //име на героя
  89.                 int amount = Integer.parseInt(command.split("\\s+-\\s+")[2]); //количество за възстановяване (MP)
  90.  
  91.                 int currentMP = heroesMP.get(heroName);
  92.                 //възстановяване на точки
  93.                 currentMP += amount;
  94.  
  95.                 if (currentMP > 200) {
  96.                     currentMP = 200;
  97.                 }
  98.  
  99.                 //с колко се е увеличило: увеличеното - първоначалното
  100.                 System.out.printf("%s recharged for %d MP!%n", heroName, currentMP - heroesMP.get(heroName));
  101.                 heroesMP.put(heroName, currentMP);
  102.  
  103.             } else if (command.contains("Heal")) {
  104.                 //4. command = "Heal – {hero name} – {amount}".split("\\s+=\\s+)" -> ["Heal", "{heroName}", "{amount}"]
  105.                 String heroName = command.split("\\s+-\\s+")[1]; //име на героя
  106.                 int amount = Integer.parseInt(command.split("\\s+-\\s+")[2]); //количество за възстановяване (hp)
  107.  
  108.                 int currentHP = heroesHP.get(heroName);
  109.                 //възстановявам
  110.                 currentHP += amount;
  111.  
  112.                 if (currentHP > 100) {
  113.                     currentHP = 100;
  114.                 }
  115.  
  116.                 //с колко се е увеличило: увеличеното - първоначалното
  117.                 System.out.printf("%s healed for %d HP!%n", heroName, currentHP - heroesHP.get(heroName));
  118.                 heroesHP.put(heroName, currentHP);
  119.             }
  120.             command = scanner.nextLine();
  121.         }
  122.  
  123.         //heroesHP: име на герой -> hp
  124.         //heroesMP: име на герой -> mp
  125.  
  126.         heroesHP.entrySet()
  127.                 //име на герой (key) -> hp (value)
  128.                 .forEach(entry -> {
  129.                     String heroName = entry.getKey();
  130.                     System.out.println(heroName);
  131.                     System.out.println(" HP: " + entry.getValue());
  132.                     System.out.println(" MP: " + heroesMP.get(heroName));
  133.                 });
  134.  
  135.     }
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement