Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class P03 {
- static Map<String, Integer> heroHitPoints = new HashMap<>();
- static Map<String, Integer> heroManaPoints = new HashMap<>();
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = Integer.parseInt(sc.nextLine());
- for (int i = 0; i < n; i++) {
- String hero = sc.nextLine();
- String[] heroTokens = hero.split("\\s+");
- String heroName = heroTokens[0];
- int hPoints = Integer.parseInt(heroTokens[1]);
- int mPoints = Integer.parseInt(heroTokens[2]);
- add(heroName, hPoints, mPoints);
- }
- String input = sc.nextLine();
- while (!input.equals("End")) {
- String[] tokens = input.split(" - ");
- String command = tokens[0];
- String heroName = tokens[1];
- int points = Integer.parseInt(tokens[2]);
- switch (command) {
- case "CastSpell": {
- String spellName = tokens[3];
- spell(heroName, points, spellName);
- }
- break;
- case "TakeDamage": {
- String attacker = tokens[3];
- damage(heroName, points, attacker);
- }
- break;
- case "Recharge": {
- recharge(heroName, points);
- }
- break;
- case "Heal": {
- heal(heroName, points);
- }
- break;
- }
- input = sc.nextLine();
- }
- List<String> names = heroHitPoints.entrySet().stream()
- .sorted((a, b) -> {
- int healthA = a.getValue();
- String nameA = a.getKey();
- int healthB = b.getValue();
- String nameB = b.getKey();
- if (healthA != healthB) {
- return Integer.compare(healthB, healthA);
- } else {
- return nameB.compareTo(nameA);
- }
- })
- .map(Map.Entry::getKey)
- // .map(e -> e.getKey())
- .collect(Collectors.toList());
- for (String name : names) {
- System.out.printf("%s%n HP: %d%n MP: %d%n"
- , name, heroHitPoints.get(name), heroManaPoints.get(name));
- }
- }
- private static void heal(String heroName, int points) {
- if (heroHitPoints.containsKey(heroName)) {
- int hP = heroHitPoints.get(heroName);
- int newHP = hP + points;
- int healedPoints = 100 - hP;
- if (newHP > 100) {
- newHP = 100;
- System.out.printf("%s healed for %d hP!%n", heroName, healedPoints);
- }else {
- System.out.printf("%s healed for %d hP!%n", heroName, points);
- }
- heroHitPoints.put(heroName, newHP);
- }
- }
- private static void recharge(String heroName, int points) {
- if (heroManaPoints.containsKey(heroName)) {
- int mP = heroManaPoints.get(heroName);
- int newMP = mP + points;
- int rechargedPoints = 200 - mP;
- if (newMP > 200) {
- newMP = 200;
- System.out.printf("%s recharged for %d MP!%n", heroName, rechargedPoints);
- }else {
- System.out.printf("%s recharged for %d MP!%n", heroName, points);
- }
- heroManaPoints.put(heroName, newMP);
- }
- }
- private static void damage(String heroName, int points, String attacker) {
- if (heroHitPoints.containsKey(heroName)) {
- int hP = heroHitPoints.get(heroName);
- int newHP = hP - points;
- if (newHP <= 0) {
- System.out.printf("%s has been killed by %s!%n", heroName, attacker);
- heroHitPoints.remove(heroName);
- heroManaPoints.remove(heroName);
- } else {
- heroHitPoints.put(heroName, newHP);
- System.out.printf("%s was hit for %d HP by %s and now has %d HP left!%n"
- , heroName, points, attacker, newHP);
- }
- }
- }
- private static void spell(String heroName, int mpNeeded, String spellName) {
- int mP = heroManaPoints.get(heroName);
- int newMP = mP - mpNeeded;
- if (heroManaPoints.containsKey(heroName)) {
- if (newMP <= 0) {
- System.out.printf("%s does not have enough MP to cast %s!%n", heroName, spellName);
- } else {
- System.out.printf("%s has successfully cast %s and now has %d MP!%n", heroName, spellName, newMP);
- }
- }
- heroManaPoints.put(heroName, newMP);
- }
- private static void add(String heroName, int hPoints, int mPoints) {
- heroHitPoints.put(heroName, hPoints);
- heroManaPoints.put(heroName, mPoints);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement