Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Scanner;
- public class third {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- HashMap<String, int[]> heroes = new HashMap<>();
- while (n-- > 0) {
- String[] tokens = scanner.nextLine().split("\\s+");
- String name = tokens[0];
- int hp = Integer.parseInt(tokens[1]);
- int mp = Integer.parseInt(tokens[2]);
- heroes.putIfAbsent(name, new int[]{hp, mp});
- }
- String command = scanner.nextLine();
- while (!"End".equals(command)) {
- String[] tokens = command.split(" - ");
- switch (tokens[0]) {
- case "CastSpell": {
- String hero = tokens[1];
- int mana = Integer.parseInt(tokens[2]);
- String spell = tokens[3];
- if (heroes.get(hero)[1] >= mana) {
- heroes.get(hero)[1] -= mana;
- System.out.println(String.format("%s has successfully cast %s and now has %d MP!",
- hero, spell, heroes.get(hero)[1]));
- } else {
- System.out.println(String.format("%s does not have enough MP to cast %s!",
- hero, spell));
- }
- }
- break;
- case "TakeDamage": {
- String name = tokens[1];
- int damage = Integer.parseInt(tokens[2]);
- String attacker = tokens[3];
- heroes.get(name)[0] -= damage;
- if (heroes.get(name)[0] > 0) {
- System.out.println(String.format("%s was hit for %d HP by %s and now has %d HP left!",
- name, damage, attacker, heroes.get(name)[0]));
- } else {
- heroes.remove(name);
- System.out.println(String.format("%s has been killed by %s!", name, attacker));
- }
- }
- break;
- case "Recharge": {
- String name = tokens[1];
- int mana = Integer.parseInt(tokens[2]);
- if (heroes.get(name)[1] + mana <= 200) {
- heroes.get(name)[1] += mana;
- System.out.println(String.format("%s recharged for %d MP!", name, mana));
- } else {
- int recovered = 200 - heroes.get(name)[1];
- heroes.get(name)[1] = 200;
- System.out.println(String.format("%s recharged for %d MP!", name, recovered));
- }
- // maybe check for not full mana
- }
- break;
- case "Heal":
- String name = tokens[1];
- int health = Integer.parseInt(tokens[2]);
- if (heroes.get(name)[0] + health <= 100) {
- heroes.get(name)[0] += health;
- System.out.println(String.format("%s healed for %d HP!", name, health));
- } else {
- int heal = 100 - heroes.get(name)[0];
- heroes.get(name)[0] = 100;
- System.out.println(String.format("%s healed for %d HP!", name, heal));
- }
- break;
- }
- command = scanner.nextLine();
- }
- heroes
- .entrySet()
- .stream()
- .sorted((h1, h2) -> {
- int one = h1.getValue()[0];
- int two = h2.getValue()[0];
- if (two - one == 0) {
- return h1.getKey().compareTo(h2.getKey());
- }
- return two - one;
- }).forEach(h -> {
- System.out.println(h.getKey());
- System.out.println(String.format(" HP: %d", h.getValue()[0]));
- System.out.println(String.format(" MP: %d", h.getValue()[1]));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement