Teo_Yanchev

Dictionaries - 03. MOBA Challenger - C# Fundamentals

Oct 8th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _3._MOBA_Challenger
  6. {
  7. class Program
  8. {
  9. static void Main()
  10. {
  11. Dictionary<string, Dictionary<string, int>> players = new Dictionary<string, Dictionary<string, int>>();
  12.  
  13. string command = Console.ReadLine();
  14. while (command != "Season end")
  15. {
  16. string[] currentCommand = command.Split(new Char[] { ' ', '-', '>' },
  17. StringSplitOptions.RemoveEmptyEntries);
  18.  
  19. string commandOne = currentCommand[0]; // playerName
  20. string commandTwo = currentCommand[1]; //playerPosition
  21. string commandThree = currentCommand[2]; //playerSkill
  22. bool isItDemoted = false;
  23.  
  24. if (commandTwo == "vs")
  25. {
  26. if (players.ContainsKey(commandOne) && players.ContainsKey(commandThree))
  27. {
  28. foreach (var player in players[commandOne])
  29. {
  30. foreach (var position in players[commandThree])
  31. {
  32. if (player.Key == position.Key)
  33. {
  34. if (players[commandOne].Values.Sum() > players[commandThree].Values.Sum())
  35. {
  36. players.Remove(commandThree);
  37. isItDemoted = true;
  38. }
  39. else if (players[commandOne].Values.Sum() < players[commandThree].Values.Sum())
  40. {
  41. players.Remove(commandOne);
  42. isItDemoted = true;
  43. }
  44. else if (players[commandOne].Values.Sum() == players[commandThree].Values.Sum())
  45. {
  46. isItDemoted = true;
  47. }
  48. }
  49. }
  50.  
  51. if (isItDemoted)
  52. {
  53. break;
  54. }
  55. }
  56. }
  57.  
  58. command = Console.ReadLine();
  59. continue;
  60. }
  61.  
  62. // Declaring the players AND ONLY IF their skill is increased we increase it!!
  63.  
  64. if (!players.ContainsKey(commandOne))
  65. {
  66. players[commandOne] = new Dictionary<string, int>();
  67.  
  68. players[commandOne].Add(commandTwo, int.Parse(commandThree));
  69.  
  70. command = Console.ReadLine();
  71. continue;
  72. }
  73.  
  74. else if (players.ContainsKey(commandOne))
  75. {
  76. if (!players[commandOne].ContainsKey(commandTwo))
  77. {
  78. players[commandOne].Add(commandTwo, int.Parse(commandThree));
  79. }
  80.  
  81. else if (players.ContainsKey(commandOne) && players[commandOne][commandTwo] < int.Parse(commandThree))
  82. {
  83. players[commandOne][commandTwo] = int.Parse(commandThree);
  84. }
  85. }
  86.  
  87.  
  88. command = Console.ReadLine();
  89. }
  90.  
  91.  
  92. // Converting the Print by descending
  93. // First is the main Dicitonary
  94. // Then in the foreach loop is the nested Dictionary
  95.  
  96. players = players
  97. .OrderByDescending(x => x.Value.Values.Sum())
  98. .ThenBy(x => x.Key)
  99. .ToDictionary(x => x.Key, x => x.Value);
  100.  
  101. foreach (var kvp in players)
  102. {
  103. string name = kvp.Key;
  104. Dictionary<string, int> positionAndSkill = kvp.Value;
  105.  
  106. positionAndSkill = positionAndSkill
  107. .OrderByDescending(x => x.Value)
  108. .ThenBy(x => x.Value)
  109. .ToDictionary(x => x.Key, x => x.Value);
  110.  
  111. Console.WriteLine($"{name}: {positionAndSkill.Values.Sum()} skill");
  112. foreach (var kvp2 in positionAndSkill)
  113. {
  114. string position = kvp2.Key;
  115. int skill = kvp2.Value;
  116.  
  117. Console.WriteLine($"- {position} <::> {skill}");
  118.  
  119. }
  120. }
  121. }
  122. }
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment