Advertisement
elena1234

Heroes of Code and Logic VII-ExamPreparation

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