Advertisement
Daniel_007

03. Heroes of Code and Logic VII

Apr 4th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace ConsoleApp2
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int howMany = int.Parse(Console.ReadLine());
  13.             SortedDictionary<string, int> herosHP = new SortedDictionary<string, int>();
  14.             SortedDictionary<string, int> herosMP = new SortedDictionary<string, int>();
  15.             for (int i = 0; i < howMany; i++)
  16.             {
  17.                 string[] input = Console.ReadLine().Split().ToArray();
  18.                 string heroName = input[0];
  19.                 int heroHP = int.Parse(input[1]);
  20.                 int heroMP = int.Parse(input[2]);
  21.                 herosHP[heroName] = heroHP;
  22.                 herosMP[heroName] = heroMP;
  23.             }
  24.             while (true)
  25.             {
  26.                 string[] command = Console.ReadLine().Split(" - ").ToArray();
  27.                 string heroName = string.Empty;
  28.                 int amount = 0;
  29.                 switch (command[0])
  30.                 {
  31.                     case "CastSpell":
  32.                         heroName = command[1];
  33.                         int neededMP = int.Parse(command[2]);
  34.                         string spellName = command[3];
  35.                         castSpell(heroName, herosMP, spellName, neededMP);
  36.                         break;
  37.                     case "TakeDamage":
  38.                         heroName = command[1];
  39.                         int damage = int.Parse(command[2]);
  40.                         string attacker = command[3];
  41.                         takeDamage(herosHP, herosMP, heroName, damage, attacker);
  42.                         break;
  43.                     case "Recharge":
  44.                         heroName = command[1];
  45.                         amount = int.Parse(command[2]);
  46.                         recharge(heroName, amount, herosMP);
  47.                         break;
  48.                     case "Heal":
  49.                         heroName = command[1];
  50.                         amount = int.Parse(command[2]);
  51.                         heal(herosHP, heroName, amount);
  52.                         break;
  53.                     case "End":
  54.                         print(herosHP, herosMP);
  55.                         return;
  56.                 }
  57.             }
  58.         }
  59.  
  60.         private static void print(SortedDictionary<string, int> herosHP, SortedDictionary<string, int> herosMP)
  61.         {
  62.             foreach (var hero in herosHP.OrderByDescending(i => i.Value))
  63.             {
  64.                 Console.WriteLine(hero.Key);
  65.  
  66.                 Console.WriteLine("  HP: " + hero.Value);
  67.                 Console.WriteLine("  MP: " + herosMP[hero.Key]);
  68.             }
  69.         }
  70.  
  71.         private static void heal(SortedDictionary<string, int> herosHP, string heroName, int amount)
  72.         {
  73.             if (herosHP[heroName] + amount >= 100)
  74.             {
  75.                 Console.WriteLine($"{heroName} healed for {100 - herosHP[heroName]} HP!");
  76.                 herosHP[heroName] = 100;
  77.                
  78.             }
  79.             else
  80.             {
  81.                 herosHP[heroName] += amount;
  82.                 Console.WriteLine($"{heroName} healed for {amount} HP!");
  83.             }
  84.         }
  85.  
  86.         private static void recharge(string heroName, int amount, SortedDictionary<string, int> herosMP)
  87.         {
  88.             if (herosMP[heroName] + amount >= 200)
  89.             {
  90.                 Console.WriteLine($"{heroName} recharged for {200-herosMP[heroName]} MP!");
  91.                 herosMP[heroName] = 200;
  92.                
  93.             }
  94.             else
  95.             {
  96.                 herosMP[heroName] += amount;
  97.                 Console.WriteLine($"{heroName} recharged for {amount} MP!");
  98.             }
  99.         }
  100.  
  101.         private static void takeDamage(SortedDictionary<string, int> herosHP, SortedDictionary<string, int> herosMP, string heroName, int damage, string attacker)
  102.         {
  103.             if (herosHP[heroName] - damage > 0)
  104.             {
  105.                 herosHP[heroName] -= damage;
  106.                 Console.WriteLine($"{heroName} was hit for {damage} HP by {attacker} and now has {herosHP[heroName]} HP left!");
  107.             }
  108.             else
  109.             {
  110.                 herosHP.Remove(heroName);
  111.                 herosMP.Remove(heroName);
  112.                 Console.WriteLine($"{heroName} has been killed by {attacker}!");
  113.             }
  114.         }
  115.  
  116.         private static void castSpell(string heroName, SortedDictionary<string, int> herosMP, string spellName, int neededMP)
  117.         {
  118.             if (herosMP[heroName] >= neededMP)
  119.             {
  120.                 herosMP[heroName] -= neededMP;
  121.                 Console.WriteLine($"{heroName} has successfully cast {spellName} and now has {herosMP[heroName]} MP!");
  122.             }
  123.             else
  124.             {
  125.                 Console.WriteLine($"{heroName} does not have enough MP to cast {spellName}!");
  126.             }
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement