Advertisement
Niicksana

05. Hands of Cards

Jun 16th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _05._Hands_of_Cards
  6. {
  7. class Program
  8. { // 07. Dictionaries, Lambda and LINQ - Exercises
  9. static void Main(string[] args)
  10. {
  11. string input = Console.ReadLine(); // Input
  12.  
  13. Dictionary<string, int> handsOfCards = new Dictionary<string, int>(); // Final Dict with names and sum
  14. Dictionary<string, List<string>> cardsFromHand = new Dictionary<string, List<string>>(); //Current Dict with names and list of cards
  15.  
  16. List<string> names = new List<string>(); // List with all not repeating names
  17. List<int> result = new List<int>(); List with sum of all not repeating cards
  18.  
  19. while (input != "JOKER")
  20. {
  21. string[] personAndCards = input.Split(new string[] { ": ", ", " }, StringSplitOptions.RemoveEmptyEntries).ToArray(); // Info Array
  22. string name = personAndCards[0]; // Person name
  23.  
  24. List<string> cards = new List<string>(); // List with current cards
  25.  
  26. for (int i = 1; i < personAndCards.Length; i++)
  27. {
  28. cards.Add(personAndCards[i]); // Add current cards to List with cards
  29. }
  30.  
  31. if (cardsFromHand.ContainsKey(name)) // If the name exists
  32. {
  33. foreach (var item in cardsFromHand)
  34. {
  35. for (int i = 0; i < cards.Count; i++)
  36. {
  37. if (cardsFromHand[item.Key].Contains(cards[i])) // If the card exist in the list in the Dict - does not work correctly;(
  38. {
  39. continue; ;
  40. }
  41.  
  42. cardsFromHand[name].Add(cards[i]); // Else add that card to the list
  43.  
  44. }
  45. }
  46. }
  47. else
  48. {
  49. cardsFromHand.Add(name, cards); //Else add to the Dict name with cards
  50. names.Add(name); // Add to the names list
  51. }
  52.  
  53. input = Console.ReadLine();
  54. }
  55.  
  56. // For calculation the card
  57. int sum = 0;
  58. int j = 0;
  59. foreach (var cards in cardsFromHand)
  60. {
  61. for (int i = 0; i < cards.Value.Count; i++)
  62. {
  63. char firstLetter = ' ';
  64. char secondLetter = ' ';
  65.  
  66. foreach (var item in cardsFromHand)
  67. {
  68. string card = item.Value[i]; // Card
  69. firstLetter = card[0]; // First char of the card
  70. secondLetter = card[1]; // Second char of the card
  71. }
  72.  
  73.  
  74. int power = 0;
  75. int type = 0;
  76. if (int.TryParse(firstLetter.ToString(), out power)) // Try parse the first char if it is a number
  77. {
  78. switch (secondLetter)
  79. {
  80. case 'C':
  81. type = 1;
  82. break;
  83. case 'D':
  84. type = 2;
  85. break;
  86. case 'H':
  87. type = 3;
  88. break;
  89. case 'S':
  90. type = 4;
  91. break;
  92. }
  93. }
  94. else // Else if it is a letter
  95. {
  96. switch (firstLetter)
  97. {
  98. case 'J':
  99. power = 11;
  100. break;
  101. case 'Q':
  102. power = 12;
  103. break;
  104. case 'K':
  105. power = 13;
  106. break;
  107. case 'A':
  108. power = 14;
  109. break;
  110. default:
  111. power = 10;
  112. break;
  113. }
  114.  
  115. switch (secondLetter)
  116. {
  117. case 'C':
  118. type = 1;
  119. break;
  120. case 'D':
  121. type = 2;
  122. break;
  123. case 'H':
  124. type = 3;
  125. break;
  126. case 'S':
  127. type = 4;
  128. break;
  129. }
  130.  
  131. }
  132.  
  133. sum += power * type; // sum
  134.  
  135. j++;
  136. }
  137.  
  138. result.Add(sum); // Add to the result list
  139. }
  140.  
  141. for (int i = 0; i < result.Count; i++)
  142. {
  143. handsOfCards.Add(names[i], result[i]);
  144. }
  145.  
  146. foreach (var hand in handsOfCards)
  147. {
  148. Console.WriteLine($"{hand.Key}: {hand.Value}");
  149. }
  150. }
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement