Advertisement
abushik

Problem_3._Heroes_of_Code_and_Logic_VII

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