Advertisement
Guest User

Untitled

a guest
Oct 8th, 2016
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _05HandsOfCards
  8. {
  9. class Program
  10. {
  11. private static void AddCards(List<string> playersAndCards, Dictionary<string, List<string>> dict)
  12. {
  13. string player = playersAndCards[0];
  14. var cards = playersAndCards.Where((x, index) => index != 0).Distinct().ToList();
  15. if (dict.ContainsKey(player))
  16. {
  17. foreach (var card in cards)
  18. {
  19. if (!(dict[player].Contains(card)))
  20. {
  21. dict[player].Add(card);
  22. }
  23. }
  24. }
  25. else
  26. {
  27. dict.Add(player, new List<string>());
  28. dict[player].AddRange(cards);
  29. }
  30. }
  31.  
  32. private static int CalculateLeft(string card)
  33. {
  34. int sum = 0;
  35. string number = new string(card.Where(char.IsDigit).ToArray());
  36. if (number.Any())
  37. {
  38. sum += int.Parse(number);
  39. }
  40. else
  41. {
  42. switch (card[0])
  43. {
  44. case 'J':
  45. sum += 11;
  46. break;
  47. case 'Q':
  48. sum += 12;
  49. break;
  50. case 'K':
  51. sum += 13;
  52. break;
  53. case 'A':
  54. sum += 14;
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. return sum;
  61. }
  62.  
  63. private static int CalculateRight(string card)
  64. {
  65. char letter = card[card.Length - 1];
  66. switch (letter)
  67. {
  68. case 'S':
  69. return 4;
  70. case 'H':
  71. return 3;
  72. case 'D':
  73. return 2;
  74. default:
  75. return 1;
  76. }
  77.  
  78. }
  79. private static int CalculateCardPoints(List<string> hand)
  80. {
  81. int sum = 0;
  82. foreach (var card in hand)
  83. {
  84. sum += CalculateLeft(card) * CalculateRight(card);
  85. }
  86. return sum;
  87. }
  88. private static void EndGame(Dictionary<string, List<string>> dict)
  89. {
  90. var results = new Dictionary<string, int>();
  91. foreach (KeyValuePair<string, List<string>> player in dict)
  92. {
  93. results.Add(player.Key, CalculateCardPoints(player.Value));
  94. }
  95. foreach (var result in results)
  96. {
  97. Console.WriteLine("{0}: {1}", result.Key, result.Value);
  98. }
  99. }
  100. static void Main(string[] args)
  101. {
  102. var players = new Dictionary<string, List<string>>();
  103. bool done = false;
  104. while (!done)
  105. {
  106. var playersAndCards = Console.ReadLine().Split(new char[] { ':', ' ', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  107. switch (playersAndCards[0])
  108. {
  109. case "JOKER":
  110. EndGame(players);
  111. done = true;
  112. break;
  113. default:
  114. AddCards(playersAndCards, players);
  115. break;
  116. }
  117. }
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement