Advertisement
Guest User

Untitled

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