Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.TreeMap;
- public class _03_HeroesOfCodeAndLogicVII {
- public static class Pair {
- private int hit;
- private int mana;
- Pair() {
- this.hit = 0;
- this.mana = 0;
- }
- public int getHit() { return this.hit; }
- public int getMana() {
- return this.mana;
- }
- public void updateHit(int hit) {
- this.hit += hit;
- }
- public void updateMana(int mana) {
- this.mana += mana;
- }
- }
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- int steps = Integer.parseInt(console.nextLine());
- TreeMap<String,Pair> heroes = new TreeMap<>(); // Map<name, Pair[HP (hit point) , MP (mana point)]>
- while (steps-- > 0) {
- String[] token = console.nextLine().split (" ");
- heroes.putIfAbsent (token[0], new Pair());
- heroes.get (token[0]).updateHit(Integer.parseInt (token[1]));
- heroes.get (token[0]).updateMana(Integer.parseInt (token[2]));
- }
- String input;
- while (!"End".equals(input = console.nextLine())) {
- String[] token = input.split(" - ");
- switch (token[0]) {
- case "CastSpell":
- castspell(heroes, token[1], Integer.parseInt(token[2]), token[3]);
- break;
- case "TakeDamage":
- takedamage(heroes, token[1], Integer.parseInt(token[2]), token[3]);
- break;
- case "Recharge":
- recharge(heroes, token[1], Integer.parseInt(token[2]));
- break;
- case "Heal":
- heal(heroes, token[1], Integer.parseInt(token[2]));
- break;
- }
- }
- heroes.entrySet()
- .stream()
- .sorted((h1, h2) -> h2.getValue().getHit() - h1.getValue().getHit())
- .forEach(h ->
- System.out.printf("%s%n HP: %d%n MP: %d%n",
- h.getKey(), h.getValue().getHit(), h.getValue().getMana()));
- }
- private static void castspell(TreeMap<String, Pair> heroes, String name, int manaPoint, String spellName) {
- if (heroes.get(name).getMana() >= manaPoint){
- heroes.get(name).updateMana(-manaPoint);
- System.out.printf("%s has successfully cast %s and now has %d MP!%n", name, spellName, heroes.get(name).getMana());
- }else
- System.out.printf("%s does not have enough MP to cast %s!%n", name, spellName);
- }
- private static void takedamage(TreeMap<String, Pair> heroes, String name, int damage, String attacker) {
- heroes.get(name).updateHit(-damage);
- if (heroes.get(name).getHit()>0)
- System.out.printf("%s was hit for %d HP by %s and now has %d HP left!%n",
- name, damage, attacker, heroes.get(name).getHit());
- else{
- heroes.remove(name);
- System.out.printf("%s has been killed by %s!%n", name, attacker);
- }
- }
- private static void recharge(TreeMap<String, Pair> heroes, String name, int amount) {
- int current = heroes.get(name).getMana();
- int recharged = current + amount > 200 ? 200 - current : amount;
- heroes.get(name).updateMana(recharged);
- System.out.printf("%s recharged for %d MP!%n", name, recharged);
- }
- private static void heal(TreeMap<String, Pair> heroes, String name, int amount) {
- int current = heroes.get(name).getHit();
- int healed = current + amount > 100 ? 100 - current : amount;
- heroes.get(name).updateHit(healed);
- System.out.printf("%s healed for %d HP!%n", name, healed);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement