Guest User

Untitled

a guest
Jun 18th, 2019
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class Deck
  2. {
  3. private Random rnd;
  4. private List<Card> DeckCards { get; set; }
  5. private int PlayerCount { get; set; }
  6. public Deck(int playerCount)
  7. {
  8. rnd = new Random();
  9. List<Card> result = new List<Card>();
  10. for (int i = 2; i <= 14; i++)
  11. {
  12. result.Add(new Card(i, Suit.Club));
  13. result.Add(new Card(i, Suit.Diamond));
  14. result.Add(new Card(i, Suit.Heart));
  15. result.Add(new Card(i, Suit.Spade));
  16. }
  17. DeckCards = result;
  18. SetCards();
  19. CreatePlayerHands(playerCount);
  20. PlayerCount = playerCount;
  21. }
  22.  
  23. public void CreatePlayerHands(int count)
  24. {
  25. if (count < 1 || count > 10)
  26. throw new IndexOutOfRangeException();
  27. for (int i = 1; i <= count; i++)
  28. {
  29. SetCards(i);
  30. }
  31. }
  32.  
  33. public void ShowCards()
  34. {
  35. Console.WriteLine("Flop:");
  36. foreach (Card c in DeckCards.Where(x => x.UsedByPlayerID == 0))
  37. {
  38. c.ConsoleOutput();
  39. }
  40. Console.WriteLine();
  41. for (int i = 1; i <= PlayerCount; i++)
  42. {
  43. HandResult result = GetHandResult(i);
  44. Console.Write("Player #" + i + "t(" + result.result + " ");
  45. foreach (Card cResult in result.resultCards)
  46. {
  47. cResult.ConsoleOutput();
  48. }
  49. Console.Write(")");
  50. Console.WriteLine();
  51. foreach (Card c in DeckCards.Where(x => x.UsedByPlayerID == i))
  52. {
  53. c.ConsoleOutput();
  54. }
  55. Console.WriteLine();
  56. }
  57. }
  58.  
  59. private IOrderedEnumerable<Card> GetPlayerSet(int playerID)
  60. {
  61. return DeckCards.Where(x => x.UsedByPlayerID == 0 || x.UsedByPlayerID == playerID).OrderByDescending(x => x.Face);
  62. }
  63.  
  64. private void SetCards(int? playerID = null)
  65. {
  66. IEnumerable<Card> playerCards = DeckCards.Where(x => !x.UsedByPlayerID.HasValue).OrderBy(x => rnd.Next()).Take(playerID.HasValue ? 2 : 5);
  67. foreach (Card c in playerCards)
  68. {
  69. c.UsedByPlayerID = playerID.HasValue ? playerID : 0;
  70. }
  71. }
  72.  
  73. public class HandResult
  74. {
  75. public HandResult(IEnumerable<Card> rc, Hand h)
  76. {
  77. result = h;
  78. resultCards = rc;
  79. }
  80. public Hand result { get; set; }
  81. public IEnumerable<Card> resultCards { get; set; }
  82. }
  83.  
  84. public HandResult GetHandResult(int playerID)
  85. {
  86. IEnumerable<Card> set = GetPlayerSet(playerID).OrderBy(x => x.Face);
  87. return new[] { GetRoyalFlush(set), GetStraightFlush(set), GetFourOfAKind(set), GetFullHouse(set), GetFlush(set), GetStraight(set), GetThreeOfAKind(set), GetTwoPair(set), GetOnePair(set), GetHighCard(set) }.First(x => x != null);
  88. }
  89.  
  90. private HandResult GetRoyalFlush(IEnumerable<Card> set)
  91. {
  92. HandResult straightflush = GetStraightFlush(set);
  93. if (straightflush != null)
  94. {
  95. return straightflush.resultCards.Any(x => x.Face == 14 && x.Face == 13) ? new HandResult(straightflush.resultCards, Hand.RoyalFlush) : null;
  96. }
  97. return null;
  98. }
  99.  
  100. private HandResult GetStraightFlush(IEnumerable<Card> set)
  101. {
  102. IEnumerable<Card> flush = set.GroupBy(x => x.Suit).Where(x => x.Count() >= 5).SingleOrDefault() ?? Enumerable.Empty<Card>();
  103. HandResult strightflush = GetStraight(flush);
  104. return strightflush != null ? new HandResult(strightflush.resultCards, Hand.StraightFlush) : null;
  105. }
  106.  
  107. private HandResult GetFourOfAKind(IEnumerable<Card> set)
  108. {
  109. IEnumerable<Card> fourOfAKind = set.GroupBy(x => x.Face).SingleOrDefault(x => x.Count() >= 4) ?? Enumerable.Empty<Card>();
  110. return fourOfAKind.Any() ? new HandResult(fourOfAKind, Hand.FourOfAKind) : null;
  111. }
  112.  
  113. private HandResult GetFullHouse(IEnumerable<Card> set)
  114. {
  115. HandResult threeOfAKind = GetThreeOfAKind(set);
  116. if (threeOfAKind != null)
  117. {
  118. IEnumerable<Card> remainingCards = set.Where(x => !threeOfAKind.resultCards.Any(y => y.Face == x.Face && y.Suit == x.Suit));
  119. IEnumerable<Card> highestTwoPair = remainingCards.GroupBy(x => x.Face).Where(x => x.Count() >= 2).OrderByDescending(x => x.Key).FirstOrDefault() ?? Enumerable.Empty<Card>(); ;
  120. if (highestTwoPair.Any())
  121. {
  122. return new HandResult(threeOfAKind.resultCards.Concat(highestTwoPair.Take(2)), Hand.ThreeOfAKind);
  123. }
  124. }
  125. return null;
  126. }
  127.  
  128. private HandResult GetFlush(IEnumerable<Card> set)
  129. {
  130. IEnumerable<Card> flush = set.GroupBy(x => x.Suit).Where(x => x.Count() >= 5).SelectMany(x => x.OrderByDescending(y => y.Face).Take(5));
  131. return flush.Any() ? new HandResult(flush, Hand.Flush) : null;
  132. }
  133.  
  134. private HandResult GetThreeOfAKind(IEnumerable<Card> set)
  135. {
  136. IEnumerable<Card> threeOfAKind = set.GroupBy(x => x.Face).OrderByDescending(x => x.Key).FirstOrDefault(x => x.Count() == 3);
  137. return threeOfAKind != null ? new HandResult(threeOfAKind, Hand.ThreeOfAKind) : null;
  138. }
  139.  
  140. private HandResult GetTwoPair(IEnumerable<Card> set)
  141. {
  142. var pairs = set.GroupBy(x => x.Face).Where(x => x.Count() == 2);
  143. return pairs.Count() >= 2 ? new HandResult(pairs.OrderByDescending(x => x.Key).Take(2).SelectMany(x => x), Hand.TwoPair):null;
  144. }
  145.  
  146. private HandResult GetOnePair(IEnumerable<Card> set)
  147. {
  148. IEnumerable<Card> onePair = set.GroupBy(x => x.Face).FirstOrDefault(x => x.Count() >= 2) ;
  149. return onePair != null ? new HandResult(onePair, Hand.OnePair) : null;
  150. }
  151.  
  152. private HandResult GetStraight(IEnumerable<Card> set)
  153. {
  154. if (set.Any())
  155. {
  156. bool isAceStraight = !new List<int>() { 14, 2, 3, 4, 5 }.Except(set.Select(x => x.Face)).Any();
  157. if (isAceStraight)
  158. {
  159. return new HandResult( set.Where(x => new List<int>() { 14, 2, 3, 4, 5 }.Contains(x.Face)).GroupBy(x => x.Face).Select(x => x.First()), Hand.Straight);
  160. }
  161. int? temp = null;
  162. int conseductiveIndex = 0;
  163. for (int i = 0; i < set.Count(); i++)
  164. {
  165. if (temp.HasValue)
  166. {
  167. if (temp != set.ElementAt(i).Face - 1)
  168. {
  169. conseductiveIndex = i;
  170. }
  171. if (i - conseductiveIndex == 4)
  172. {
  173. return new HandResult( set.Skip(conseductiveIndex).Take(5), Hand.Straight);
  174. }
  175. }
  176. temp = set.ElementAt(i).Face;
  177. }
  178. }
  179. return null;
  180. }
  181.  
  182. private HandResult GetHighCard(IEnumerable<Card> set)
  183. {
  184. return new HandResult(set.OrderByDescending(x => x.Face).Take(5), Hand.HighCard);
  185. }
  186.  
  187. public enum Hand { RoyalFlush, StraightFlush, FourOfAKind, FullHouse, Flush, Straight, ThreeOfAKind, TwoPair, OnePair, HighCard }
  188. public enum Suit { Heart, Diamond, Spade, Club }
  189.  
  190. public class Card
  191. {
  192. public Card(int face, Suit suit)
  193. {
  194. Suit = suit;
  195. Face = face;
  196. }
  197. public Suit Suit { get; set; }
  198. public int Face { get; set; }
  199. public int? UsedByPlayerID { get; set; }
  200.  
  201. public void ConsoleOutput()
  202. {
  203. Console.OutputEncoding = System.Text.Encoding.UTF8;
  204. char club = 'u2663', spade = 'u2660', diamond = 'u2666', heart = 'u2665';
  205. Console.BackgroundColor = ConsoleColor.White;
  206. string face = GetFace(Face).PadLeft(2);
  207. switch (Suit)
  208. {
  209. case Suit.Diamond:
  210. Console.ForegroundColor = ConsoleColor.Red;
  211. Console.Write(face + diamond);
  212. break;
  213. case Suit.Heart:
  214. Console.ForegroundColor = ConsoleColor.Red;
  215. Console.Write(face + heart);
  216. break;
  217. case Suit.Club:
  218. /Console.ForegroundColor = ConsoleColor.Black;
  219. Console.Write(face + club);
  220. break;
  221. case Suit.Spade:
  222. Console.ForegroundColor = ConsoleColor.Black;
  223. Console.Write(face + spade);
  224. break;
  225. }
  226. Console.ResetColor();
  227. Console.Write(" ");
  228. }
  229.  
  230. private string GetFace(int face)
  231. {
  232. return face.ToString().Replace("11", "J").Replace("12", "Q").Replace("13", "K").Replace("14", "A");
  233. }
  234. }
  235. }
RAW Paste Data