CardPacket.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //-----------------------------------------------------------------------------
  2. // CardsCollection.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. namespace CardsFramework
  11. {
  12. /// <summary>
  13. /// Card related <see cref="EventArgs"/> holding event information of a <see cref="TraditionalCard"/>
  14. /// </summary>
  15. public class CardEventArgs : EventArgs
  16. {
  17. public TraditionalCard Card { get; set; }
  18. }
  19. /// <summary>
  20. /// A packet of cards
  21. /// </summary>
  22. /// <remarks>
  23. /// A card packet may be initialized with a collection of cards.
  24. /// It may lose cards or deal them to <see cref="Hand"/>, but may
  25. /// not receive new cards unless derived and overridden.
  26. /// </remarks>
  27. public class CardPacket
  28. {
  29. protected List<TraditionalCard> cards { get; set; }
  30. /// <summary>
  31. /// An event which triggers when a card is removed from the collection.
  32. /// </summary>
  33. public event EventHandler<CardEventArgs> LostCard;
  34. public int Count { get { return cards.Count; } }
  35. /// <summary>
  36. /// Initializes a card collection by simply allocating a new card list.
  37. /// </summary>
  38. protected CardPacket()
  39. {
  40. cards = new List<TraditionalCard>();
  41. }
  42. /// <summary>
  43. /// Returns a card at a specified index in the collection.
  44. /// </summary>
  45. /// <param name="index">The card's index.</param>
  46. /// <returns>The card at the specified index.</returns>
  47. public TraditionalCard this[int index]
  48. {
  49. get
  50. {
  51. return cards[index];
  52. }
  53. }
  54. /// <summary>
  55. /// Initializes a new instance of the <see cref="CardPacket"/> class.
  56. /// </summary>
  57. /// <param name="numberOfDecks">The number of decks to add to
  58. /// the collection.</param>
  59. /// <param name="jokersInDeck">The amount of jokers in each deck.</param>
  60. /// <param name="suits">The suits to add to each decks. Suits are specified
  61. /// as flags and several can be added.</param>
  62. /// <param name="cardValues">The card values which will appear in each deck.
  63. /// values are specified as flags and several can be added.</param>
  64. public CardPacket(int numberOfDecks, int jokersInDeck,
  65. CardSuit suits, CardValue cardValues)
  66. {
  67. cards = new List<TraditionalCard>();
  68. for (int deckIndex = 0; deckIndex < numberOfDecks; deckIndex++)
  69. {
  70. AddSuit(suits, cardValues);
  71. for (int j = 0; j < jokersInDeck / 2; j++)
  72. {
  73. cards.Add(new TraditionalCard(CardSuit.Club,
  74. CardValue.FirstJoker, this));
  75. cards.Add(new TraditionalCard(CardSuit.Club,
  76. CardValue.SecondJoker, this));
  77. }
  78. if (jokersInDeck % 2 == 1)
  79. {
  80. cards.Add(new TraditionalCard(CardSuit.Club,
  81. CardValue.FirstJoker, this));
  82. }
  83. }
  84. }
  85. /// <summary>
  86. /// Adds suits of cards to the collection.
  87. /// </summary>
  88. /// <param name="suits">The suits to add to each decks. Suits are specified
  89. /// as flags and several can be added.</param>
  90. /// <param name="cardValues">The card values which will appear in each deck.
  91. /// values are specified as flags and several can be added.</param>
  92. private void AddSuit(CardSuit suits, CardValue cardValues)
  93. {
  94. if ((suits & CardSuit.Club) == CardSuit.Club)
  95. {
  96. AddCards(CardSuit.Club, cardValues);
  97. }
  98. if ((suits & CardSuit.Diamond) == CardSuit.Diamond)
  99. {
  100. AddCards(CardSuit.Diamond, cardValues);
  101. }
  102. if ((suits & CardSuit.Heart) == CardSuit.Heart)
  103. {
  104. AddCards(CardSuit.Heart, cardValues);
  105. }
  106. if ((suits & CardSuit.Spade) == CardSuit.Spade)
  107. {
  108. AddCards(CardSuit.Spade, cardValues);
  109. }
  110. }
  111. /// <summary>
  112. /// Adds cards to the collection.
  113. /// </summary>
  114. /// <param name="suit">The suit of the added cards.</param>
  115. /// <param name="cardValues">The card values which will appear in each deck.
  116. /// values are specified as flags and several can be added.</param>
  117. private void AddCards(CardSuit suit,
  118. CardValue cardValues)
  119. {
  120. if ((cardValues & CardValue.Ace) == CardValue.Ace)
  121. {
  122. cards.Add(new TraditionalCard(suit, CardValue.Ace, this));
  123. }
  124. if ((cardValues & CardValue.Two) == CardValue.Two)
  125. {
  126. cards.Add(new TraditionalCard(suit, CardValue.Two, this));
  127. }
  128. if ((cardValues & CardValue.Three) == CardValue.Three)
  129. {
  130. cards.Add(new TraditionalCard(suit, CardValue.Three, this));
  131. }
  132. if ((cardValues & CardValue.Four) == CardValue.Four)
  133. {
  134. cards.Add(new TraditionalCard(suit, CardValue.Four, this));
  135. }
  136. if ((cardValues & CardValue.Five) == CardValue.Five)
  137. {
  138. cards.Add(new TraditionalCard(suit, CardValue.Five, this));
  139. }
  140. if ((cardValues & CardValue.Six) == CardValue.Six)
  141. {
  142. cards.Add(new TraditionalCard(suit, CardValue.Six, this));
  143. }
  144. if ((cardValues & CardValue.Seven) == CardValue.Seven)
  145. {
  146. cards.Add(new TraditionalCard(suit, CardValue.Seven, this));
  147. }
  148. if ((cardValues & CardValue.Eight) == CardValue.Eight)
  149. {
  150. cards.Add(new TraditionalCard(suit, CardValue.Eight, this));
  151. }
  152. if ((cardValues & CardValue.Nine) == CardValue.Nine)
  153. {
  154. cards.Add(new TraditionalCard(suit, CardValue.Nine, this));
  155. }
  156. if ((cardValues & CardValue.Ten) == CardValue.Ten)
  157. {
  158. cards.Add(new TraditionalCard(suit, CardValue.Ten, this));
  159. }
  160. if ((cardValues & CardValue.Jack) == CardValue.Jack)
  161. {
  162. cards.Add(new TraditionalCard(suit, CardValue.Jack, this));
  163. }
  164. if ((cardValues & CardValue.Queen) == CardValue.Queen)
  165. {
  166. cards.Add(new TraditionalCard(suit, CardValue.Queen, this));
  167. }
  168. if ((cardValues & CardValue.King) == CardValue.King)
  169. {
  170. cards.Add(new TraditionalCard(suit, CardValue.King, this));
  171. }
  172. }
  173. /// <summary>
  174. /// Shuffles the cards in the packet by randomly changing card placement.
  175. /// </summary>
  176. public void Shuffle()
  177. {
  178. Random random = new Random();
  179. List<TraditionalCard> shuffledDeck = new List<TraditionalCard>();
  180. while (cards.Count > 0)
  181. {
  182. TraditionalCard card = cards[random.Next(0, cards.Count)];
  183. cards.Remove(card);
  184. shuffledDeck.Add(card);
  185. }
  186. cards = shuffledDeck;
  187. }
  188. /// <summary>
  189. /// Shuffles the cards in the packet using a specified seed for deterministic shuffling.
  190. /// </summary>
  191. /// <param name="seed">The seed for the random number generator.</param>
  192. public void Shuffle(int seed)
  193. {
  194. Random random = new Random(seed);
  195. List<TraditionalCard> shuffledDeck = new List<TraditionalCard>();
  196. while (cards.Count > 0)
  197. {
  198. TraditionalCard card = cards[random.Next(0, cards.Count)];
  199. cards.Remove(card);
  200. shuffledDeck.Add(card);
  201. }
  202. cards = shuffledDeck;
  203. }
  204. /// <summary>
  205. /// Removes the specified card from the packet. The first matching card
  206. /// will be removed.
  207. /// </summary>
  208. /// <param name="card">The card to remove.</param>
  209. /// <returns>The card that was removed from the collection.</returns>
  210. /// <remarks>
  211. /// Please note that removing a card from a packet may only be performed internally by
  212. /// other card-framework classes to maintain the principle that a card may only be held
  213. /// by one <see cref="CardPacket"/> only at any given time.
  214. /// </remarks>
  215. internal TraditionalCard Remove(TraditionalCard card)
  216. {
  217. if (cards.Contains(card))
  218. {
  219. cards.Remove(card);
  220. if (LostCard != null)
  221. {
  222. LostCard(this, new CardEventArgs() { Card = card });
  223. }
  224. return card;
  225. }
  226. return null;
  227. }
  228. /// <summary>
  229. /// Removes all the cards from the collection.
  230. /// </summary>
  231. /// <returns>A list of all the cards that were removed.</returns>
  232. internal List<TraditionalCard> Remove()
  233. {
  234. List<TraditionalCard> cards = this.cards;
  235. this.cards = new List<TraditionalCard>();
  236. return cards;
  237. }
  238. /// <summary>
  239. /// Deals the first card from the collection to a specified hand.
  240. /// </summary>
  241. /// <param name="destinationHand">The destination hand.</param>
  242. /// <returns>The card that was moved to the hand.</returns>
  243. public TraditionalCard DealCardToHand(Hand destinationHand)
  244. {
  245. TraditionalCard firstCard = cards[0];
  246. firstCard.MoveToHand(destinationHand);
  247. return firstCard;
  248. }
  249. /// <summary>
  250. /// Deals several cards to a specified hand.
  251. /// </summary>
  252. /// <param name="destinationHand">The destination hand.</param>
  253. /// <param name="count">The amount of cards to deal.</param>
  254. /// <returns>A list of the cards that were moved to the hand.</returns>
  255. public List<TraditionalCard> DealCardsToHand(Hand destinationHand, int count)
  256. {
  257. List<TraditionalCard> dealtCards = new List<TraditionalCard>();
  258. for (int cardIndex = 0; cardIndex < count; cardIndex++)
  259. {
  260. dealtCards.Add(DealCardToHand(destinationHand));
  261. }
  262. return dealtCards;
  263. }
  264. }
  265. }