Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.LinkedHashMap;
- import java.util.Map;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(System.in)
- );
- int n = Integer.parseInt(reader.readLine());
- Map<String, int[]> heroes = new LinkedHashMap<>();
- for (int i = 0; i < n; i++) {
- String[] tokens = reader.readLine().split("\\s+");
- String heroName = tokens[0];
- int health = Integer.parseInt(tokens[1]);
- if (health < 0) {
- health = 0;
- } else if (health > 100) {
- health = 100;
- }
- int mana = Integer.parseInt(tokens[2]);
- if (mana < 0) {
- mana = 0;
- } else if (mana > 200) {
- mana = 200;
- }
- heroes.put(heroName, new int[2]);
- heroes.get(heroName)[0] = health;
- heroes.get(heroName)[1] = mana;
- }
- String input;
- while (!"End".equals(input = reader.readLine())) {
- String[] tokens = input.split(" - ");
- String action = tokens[0];
- String hero = tokens[1];
- switch (action) {
- case "CastSpell":
- int requiredMana = Integer.parseInt(tokens[2]);
- String spellName = tokens[3];
- if (heroes.get(hero)[1] >= requiredMana) {
- heroes.get(hero)[1] -= requiredMana;
- System.out.println(String.format("%s has successfully cast %s and now has %d MP!",
- hero, spellName, heroes.get(hero)[1]));
- } else {
- System.out.println(String.format("%s does not have enough MP to cast %s!",
- hero, spellName));
- }
- break;
- case "TakeDamage":
- int damage = Integer.parseInt(tokens[2]);
- String attacker = tokens[3];
- if (heroes.get(hero)[0] > damage) {
- heroes.get(hero)[0] -= damage;
- System.out.println(String.format("%s was hit for %d HP by %s and now has %d HP left!",
- hero, damage, attacker, heroes.get(hero)[0]));
- } else {
- heroes.get(hero)[0] = 0;
- System.out.println(String.format("%s has been killed by %s!",
- hero, attacker));
- }
- break;
- case "Recharge":
- int manaAmount = Integer.parseInt(tokens[2]);
- if (heroes.get(hero)[1] < 200) {
- int recovered = Math.min(200 - heroes.get(hero)[1], manaAmount);
- heroes.get(hero)[1] += manaAmount;
- if (heroes.get(hero)[1] > 200) {
- heroes.get(hero)[1] = 200;
- }
- System.out.println(String.format("%s recharged for %d MP!",
- hero, recovered));
- } else {
- System.out.println(String.format("%s recharged for 0 MP!",
- hero));
- }
- break;
- case "Heal":
- int healAmount = Integer.parseInt(tokens[2]);
- if (heroes.get(hero)[0] < 100) {
- int recovered = Math.min(100 - heroes.get(hero)[0], healAmount);
- heroes.get(hero)[0] += healAmount;
- if (heroes.get(hero)[0] > 100) {
- heroes.get(hero)[0] = 100;
- }
- System.out.println(String.format("%s healed for %d HP!",
- hero, recovered));
- } else {
- System.out.println(String.format("%s healed for 0 HP!",
- hero));
- }
- break;
- default:
- break;
- }
- }
- heroes.entrySet().stream()
- .filter(f -> f.getValue()[0] > 0)
- .sorted((a, b) -> {
- if (a.getValue()[0] == b.getValue()[0]) {
- return a.getKey().compareTo(b.getKey());
- } else {
- return Integer.compare(b.getValue()[0], a.getValue()[0]);
- }
- })
- .forEach(x -> {
- System.out.println(x.getKey());
- System.out.println("HP: " + x.getValue()[0]);
- System.out.println("MP: " + x.getValue()[1]);
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement