Advertisement
AngelVasilev

Softuni Fundamentals - Battle manager

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