Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class HeroesOfCodeAndLogicVII {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int countHeroes = Integer.parseInt(scanner.nextLine());
- Map<String, Integer> hitPoints = new HashMap<>();
- Map<String, Integer> manaPoints = new HashMap<>();
- for (int index = 0; index < countHeroes; index++) {
- String[] heroes = scanner.nextLine().split("\\s+");
- String heroName = heroes[0];
- int hPoints = Integer.parseInt(heroes[1]);
- int mPoints = Integer.parseInt(heroes[2]);
- hitPoints.putIfAbsent(heroName, hPoints);
- manaPoints.putIfAbsent(heroName, mPoints);
- if (manaPoints.get(heroName) > 200) {
- manaPoints.put(heroName, 200);
- }
- if (hitPoints.get(heroName) > 100) {
- hitPoints.put(heroName, 100);
- }
- }
- String input = scanner.nextLine();
- while (!input.equals("End")) {
- String[] action = input.split("-");
- String command = action[0].trim();
- String name = action[1].trim();
- switch (command) {
- case "CastSpell": {
- int neededMP = Integer.parseInt(action[2].trim());
- String spellName = action[3].trim();
- int currentMP = manaPoints.get(name);
- int leftMP = currentMP - neededMP;
- if (leftMP >= 0) {
- System.out.printf("%s has successfully cast %s and now has %d MP!%n",
- name, spellName, leftMP);
- manaPoints.put(name, leftMP);
- } else {
- System.out.printf("%s does not have enough MP to cast %s!%n", name, spellName);
- }
- break;
- }
- case "TakeDamage": {
- int damage = Integer.parseInt(action[2].trim());
- String attacker = action[3].trim();
- int leftHP = hitPoints.get(name) - damage;
- if (leftHP > 0) {
- hitPoints.put(name, leftHP);
- System.out.printf("%s was hit for %d HP by %s and now has %d HP left!%n",
- name, damage, attacker, leftHP);
- } else {
- System.out.printf("%s has been killed by %s!%n", name, attacker);
- hitPoints.remove(name);
- manaPoints.remove(name);
- }
- break;
- }
- case "Recharge": {
- int amountMP = Integer.parseInt(action[2].trim());
- int currentMP = manaPoints.get(name);
- int newMP = currentMP + amountMP;
- if (newMP > 200) {
- manaPoints.put(name, 200);
- System.out.printf("%s recharged for %d MP!%n", name, 200 - currentMP);
- } else {
- manaPoints.put(name, newMP);
- System.out.printf("%s recharged for %d MP!%n", name, amountMP);
- }
- break;
- }
- case "Heal": {
- int amountHP = Integer.parseInt(action[2].trim());
- int currentHp = hitPoints.get(name);
- int newHP = currentHp + amountHP;
- if (newHP > 100) {
- hitPoints.put(name, 100);
- System.out.printf("%s healed for %d HP!%n", name, 100 - currentHp);
- } else {
- hitPoints.put(name, newHP);
- System.out.printf("%s healed for %d HP!%n", name, amountHP);
- }
- break;
- }
- }
- input = scanner.nextLine();
- }
- hitPoints
- .entrySet()
- .stream()
- .sorted((e1, e2) -> {
- int result = Integer.compare(e2.getValue(), e1.getValue());
- if (result == 0) {
- result = e1.getKey().compareTo(e2.getKey());
- }
- return result;
- }).forEach(item -> {
- System.out.printf("%s%n HP: %d%n MP: %d%n",
- item.getKey(), item.getValue(), manaPoints.get(item.getKey()));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment