Advertisement
desislava_topuzakova

3. Heroes of Code and Logic VII

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