Advertisement
desislava_topuzakova

3. Heroes of Code and Logic VII

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