ralichka

FinalExam-03.08.2019-2-03.BattleManager

Dec 4th, 2019
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03.BattleManager
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> input = Console.ReadLine().Split(":").ToList();
  12.             string command = input[0];
  13.  
  14.             Dictionary<string, int> health = new Dictionary<string, int>();
  15.             Dictionary<string, int> energy = new Dictionary<string, int>();
  16.  
  17.             while (command != "Results")
  18.             {
  19.                
  20.                 if (command == "Add")
  21.                 {
  22.                     string name = input[1];
  23.                     int currentHealth = int.Parse(input[2]);
  24.                     int currentEnergy = int.Parse(input[3]);
  25.  
  26.                     if (!health.ContainsKey(name))
  27.                     {
  28.                         health.Add(name, currentHealth);
  29.                         energy.Add(name, currentEnergy);
  30.                     }
  31.                     else
  32.                     {
  33.                         health[name] += currentHealth;
  34.                        
  35.                     }
  36.  
  37.                 }
  38.                 else if (command == "Attack")
  39.                 {
  40.  
  41.                     string attackerName = input[1];
  42.                     string defenderName = input[2];
  43.                     int damage = int.Parse(input[3]);
  44.  
  45.                     if (health.ContainsKey(attackerName) && health.ContainsKey(defenderName))
  46.                     {
  47.                         health[defenderName] -= damage;
  48.                         energy[attackerName]--;
  49.  
  50.                         if (health[defenderName] <= 0)
  51.                         {
  52.                             health.Remove(defenderName);
  53.                             energy.Remove(defenderName);
  54.                             Console.WriteLine($"{defenderName} was disqualified!");
  55.                         }
  56.                         if (energy[attackerName] <= 0)
  57.                         {
  58.                             energy.Remove(attackerName);
  59.                             health.Remove(attackerName);
  60.                             Console.WriteLine($"{attackerName} was disqualified!");
  61.  
  62.                         }
  63.                     }
  64.                 }
  65.                 else if (command == "Delete")
  66.                 {
  67.                     string name = input[1];
  68.                     if (health.ContainsKey(name))
  69.                     {
  70.                         health.Remove(name);
  71.                         energy.Remove(name);
  72.                     }
  73.                     if(name == "All")
  74.                     {
  75.                         health.Clear();
  76.                         energy.Clear();
  77.                     }
  78.                 }
  79.                 input = Console.ReadLine().Split(":").ToList();
  80.                 command = input[0];
  81.             }
  82.             health = health.OrderByDescending(x => x.Value).ThenBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
  83.  
  84.             Console.WriteLine($"People count: {health.Count}");
  85.  
  86.             foreach (var person in health)
  87.             {
  88.                 Console.WriteLine($"{person.Key} - {person.Value} - {energy[person.Key]}");
  89.             }
  90.         }
  91.     }
  92. }
Add Comment
Please, Sign In to add comment