Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- Map<String, List<Integer>> heroes = new HashMap<>();
- int n = Integer.parseInt(scan.nextLine());
- for (int i = 0; i < n; i++) {
- String[] inputTokens = scan.nextLine().split(" ");
- heroes.put(inputTokens[0], new ArrayList<>());
- heroes.get(inputTokens[0]).add(Integer.parseInt(inputTokens[1]));
- heroes.get(inputTokens[0]).add(Integer.parseInt(inputTokens[2]));
- }
- String input = scan.nextLine();
- while (!"End".equals(input)) {
- String[] tokens = input.split(" - ");
- String command = tokens[0];
- String heroName = tokens[1];
- switch (command) {
- case "CastSpell": {
- int neededManaPoints = Integer.parseInt(tokens[2]);
- String spellName = tokens[3];
- int currentMana = heroes.get(heroName).get(1);
- if (currentMana >= neededManaPoints) {
- int manaLeft = currentMana - neededManaPoints;
- System.out.println(String.format("%s has successfully cast %s and now has %d MP!",
- heroName, spellName, manaLeft));
- heroes.get(heroName).set(1, manaLeft);
- } else {
- System.out.println(String.format("%s does not have enough MP to cast %s!",
- heroName, spellName));
- }
- }
- break;
- case "TakeDamage": {
- int damage = Integer.parseInt(tokens[2]);
- String attacker = tokens[3];
- int currentHealth = heroes.get(heroName).get(0);
- if (currentHealth > damage) {
- int healthLeft = currentHealth - damage;
- System.out.println(String.format("%s was hit for %d HP by %s and now has %d HP left!",
- heroName, damage, attacker, healthLeft));
- heroes.get(heroName).set(0, healthLeft);
- } else {
- heroes.remove(heroName);
- System.out.println(String.format("%s has been killed by %s!",
- heroName, attacker));
- }
- }
- break;
- case "Recharge": {
- int amount = Integer.parseInt(tokens[2]);
- int currentMana = heroes.get(heroName).get(1);
- if (currentMana + amount >= 200) {
- amount = 200 - currentMana;
- currentMana = 200;
- } else {
- currentMana += amount;
- }
- heroes.get(heroName).set(1, currentMana);
- System.out.println(String.format("%s recharged for %d MP!", heroName, amount));
- }
- break;
- case "Heal": {
- int amount = Integer.parseInt(tokens[2]);
- int currentHealth = heroes.get(heroName).get(0);
- if (currentHealth + amount >= 100) {
- amount = 100 - currentHealth;
- currentHealth = 100;
- } else {
- currentHealth += amount;
- }
- heroes.get(heroName).set(0, currentHealth);
- System.out.println(String.format("%s healed for %d HP!", heroName, amount));
- }
- break;
- }
- input = scan.nextLine();
- }
- heroes
- .entrySet()
- .stream()
- .sorted((h1, h2) -> {
- int result = h2.getValue().get(0) - h1.getValue().get(0);
- if (result == 0) {
- result = h1.getKey().compareTo(h2.getKey());
- }
- return result;
- })
- .forEach(h -> System.out.println(String.format("%s%n HP: %d%n MP: %d",
- h.getKey(), h.getValue().get(0), h.getValue().get(1))));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement