Posted By

kyrathaba on 01/24/11


Tagged

game card cards Deck Playing 52


Versions (?)

Playing cards class


 / Published in: C#
 

Developed this class for a complete sample BlackJack game I wrote for the 2010 N.A.N.Y. competition on www.donationcoder.com

  1. namespace Kyrathasoft.Games.PlayingCard {
  2.  
  3. using System;
  4. using System.Drawing;
  5. using System.Collections.Generic;
  6.  
  7. class clsCard {
  8.  
  9. #region CardClassConstructors
  10. //default constructor and its overloads follow...
  11.  
  12. public clsCard() { //here's the default, parameter-less, constructor, that randomly generates a card
  13. //experience has taught me to seed the Random function in this manner
  14. Random rand = new Random((int)DateTime.Now.Ticks/TicksPerMillisec);
  15. int index = rand.Next(4);//randomly return integer 0, 1, 2, or 3 -- since they're 4 possible suits
  16. this.suit = (CardSuit)index; //assign suit using randomly-generated integer in range 0-3
  17. rand = new Random((int)DateTime.Now.Ticks / TicksPerMillisec);
  18. index = rand.Next(2,15);//randomly return int in range 2-14
  19. this.face = (CardFace)index;
  20. this.name = nameFromFace(this.face);
  21. this.numericValue = numericValueFromFace(this.face);
  22. shuffled = false; //since we've not invoked the ShuffleDeck() method...
  23. }
  24.  
  25. //an overload of the default constructor, that takes an Image parameter referencing an image of
  26. //all 52 cards (we'll use a method to crop out individual cards
  27.  
  28. public clsCard(Image imgAllCards) {
  29. //experience has taught me to seed the Random function in this manner
  30. Random rand = new Random((int)DateTime.Now.Ticks / TicksPerMillisec);
  31. int index = rand.Next(4);//randomly return integer 0, 1, 2, or 3 -- since they're 4 possible suits
  32. this.suit = (CardSuit)index; //assign suit using randomly-generated integer in range 0-3
  33. rand = new Random((int)DateTime.Now.Ticks / TicksPerMillisec);
  34. index = rand.Next(2, 15);//randomly return int in range 2-14
  35. this.face = (CardFace)index;
  36. this.name = nameFromFace(this.face);
  37. this.numericValue = numericValueFromFace(this.face);
  38. shuffled = false; //since we've not invoked the ShuffleDeck() method...
  39. this.imgAllCards = imgAllCards;
  40. this.cardHeight = imgAllCards.Height / 4;
  41. this.cardWidth = imgAllCards.Width / 13;
  42. }
  43.  
  44. //an overloaded constructor that let's us specify the card's face and suit
  45.  
  46. public clsCard(CardSuit theSuit, CardFace theFace) {
  47. this.suit = theSuit;
  48. this.face = theFace;
  49. this.name = nameFromFace(this.face);
  50. this.numericValue = numericValueFromFace(this.face);
  51. }
  52.  
  53. #endregion
  54.  
  55. #region CardClassConstantsAndEnums
  56. //constants & enumerations follow...
  57.  
  58. public const int TicksPerMillisec = 10000;//a constant for use with random-number generator
  59. public enum CardSuit { Clubs, Diamonds, Hearts, Spades };
  60. public enum CardFace { Two=2, Three, Four, Five, Six, Seven, Eight, Nine, Ten,
  61. Jack, Queen, King, Ace};
  62.  
  63. #endregion
  64.  
  65. #region CardClassMembersAndProperties
  66.  
  67. //private members & their properties follow...
  68.  
  69. private Stack<clsCard> Deck;
  70.  
  71. private Image imgAllCards;
  72. public Image ImgAllCards {
  73. get { return imgAllCards; }
  74. set { ;} // we'll only set this in a Card class constructor
  75. }
  76.  
  77. private int cardWidth;
  78. public int CardWidth {
  79. get { return cardWidth; }
  80. set { ;} //done in Card constructor
  81. }
  82.  
  83. private int cardHeight;
  84. public int CardHeight {
  85. get { return cardHeight; }
  86. set { ;}
  87. }
  88.  
  89. public Image imgOfIndividualCard;
  90. public Image ImgOfIndividualCard {
  91. get { return imgOfIndividualCard; }
  92. set { imgOfIndividualCard = value; }
  93. }
  94.  
  95. private bool shuffled;
  96. public bool Shuffled {
  97. get { return shuffled; }
  98. set { ;} // we'll only set this in a Card class constructor
  99. }
  100.  
  101. private CardSuit suit;
  102. public CardSuit Suit {
  103. get { return suit; }
  104. set { ;} //we'll only set this in a Card class constructor
  105. }
  106.  
  107. private CardFace face;
  108. public CardFace Face {
  109. get { return face; }
  110. set { ;} //we'll only set this in a Card class constructor
  111. }
  112.  
  113. private string name;
  114. public string Name {
  115. get { return name; }
  116. set { ;} //we'll only set this in a Card class constructor
  117. }
  118.  
  119. //I chose to implement a numericValue property, because multiple cards can have the same numeric
  120. //value, even though their faces are different (Ten, Jack, King, Queen)
  121. private int numericValue;
  122. public int NumericValue {
  123. get { return numericValue; }
  124. set { ;} //we'll only set this in a Card class constructor
  125. }
  126.  
  127. #endregion
  128.  
  129. #region CardClassMethods
  130.  
  131. public void buildTheDeck() {
  132. Deck = new Stack<clsCard>(52);
  133. clsCard card;
  134. for (int i = 0; i < 4; i++) {
  135. for (int j = 2; j < 15; j++) {
  136. card = new clsCard((CardSuit)i, (CardFace)j);
  137. Deck.Push(card);
  138. }
  139. }
  140. }
  141.  
  142. public clsCard dealCardFromDeck() {
  143. clsCard card = Deck.Pop();
  144. return card;
  145. }
  146.  
  147. public Point coordOfCardImg(CardSuit suit, CardFace face) {
  148. Point point;
  149. int width = this.imgAllCards.Width / 13;
  150. int height = this.imgAllCards.Height / 4;
  151. int x = 0;
  152. int y = 0;
  153. switch (suit) {
  154. case (clsCard.CardSuit.Clubs):
  155. y = 0;
  156. break;
  157. case (clsCard.CardSuit.Spades):
  158. y = height;
  159. break;
  160. case (clsCard.CardSuit.Hearts):
  161. y = 2 * height;
  162. break;
  163. case (clsCard.CardSuit.Diamonds):
  164. y = 3 * height;
  165. break;
  166. }
  167. switch (face) {
  168. case (clsCard.CardFace.Ace):
  169. x = 0;
  170. break;
  171. case (clsCard.CardFace.Two):
  172. x = width;
  173. break;
  174. case (clsCard.CardFace.Three):
  175. x = 2 * width;
  176. break;
  177. case (clsCard.CardFace.Four):
  178. x = 3 * width;
  179. break;
  180. case (clsCard.CardFace.Five):
  181. x = 4 * width;
  182. break;
  183. case (clsCard.CardFace.Six):
  184. x = 5 * width;
  185. break;
  186. case (clsCard.CardFace.Seven):
  187. x = 6 * width;
  188. break;
  189. case (clsCard.CardFace.Eight):
  190. x = 7 * width;
  191. break;
  192. case (clsCard.CardFace.Nine):
  193. x = 8 * width;
  194. break;
  195. case (clsCard.CardFace.Ten):
  196. x = 9 * width;
  197. break;
  198. case (clsCard.CardFace.Jack):
  199. x = 10 * width;
  200. break;
  201. case (clsCard.CardFace.Queen):
  202. x = 11 * width;
  203. break;
  204. case (clsCard.CardFace.King):
  205. x = 12 * width;
  206. break;
  207. }
  208. point = new Point(x, y);
  209. return point;
  210. }
  211.  
  212. public string announceCard(clsCard card) {
  213. string announce = string.Empty;
  214. announce += card.nameFromFace(card.face) + " of " + card.suitNameFromCardSuit(card.suit);
  215. return announce;
  216. }
  217.  
  218. public void shuffleTheDeck() {
  219.  
  220. List<int> theList = returnListOfRandomizedNums0To51(); //now holds ints 0-51 in RANDOM order
  221. #region proveEfficacyOfMethod_returnListOfRandomizedNums0To51()
  222.  
  223. //note to self: uncomment the following section of code if you need proof that
  224. //method returnListOfRandomizedNums0To51() does what I want it to do
  225.  
  226. //string s = string.Empty;
  227. //s += "The list of integers in the randomized order in which method returnListOfRandomizedNums0To51() ";
  228. //s += "returns them:" + Environment.NewLine + Environment.NewLine;
  229.  
  230. //foreach (int i in theList) {
  231. // s += i.ToString();
  232. // if (i < 51) {
  233. // s += ", ";
  234. // }
  235. //}
  236.  
  237. //s += Environment.NewLine + Environment.NewLine;
  238.  
  239. //s += "The list ordered smallest to largest:" + Environment.NewLine + Environment.NewLine;
  240.  
  241. //for(int i=0; i < 52; i++){
  242. // if (theList.Contains(i)) {
  243. // s += i.ToString();
  244. // if (i < 51) {
  245. // s += ", ";
  246. // }
  247. // }
  248. //}
  249.  
  250. //MessageBox.Show(s);
  251.  
  252. #endregion
  253. clsCard[] tmpArray = Deck.ToArray();//temp storage for the Deck's cards, so we can Clear the Deck
  254. Deck.Clear(); //so that we can repopulate the Deck with cards in a random order
  255. foreach (int i in theList) {
  256. Deck.Push(tmpArray[i]);
  257. }
  258. shuffled = true; //finally, we indicate that the Deck is now shuffled, by setting private bool field
  259. }
  260.  
  261. private List<int> returnListOfRandomizedNums0To51() {
  262. List<int> myList = new List<int>();
  263. int totalNums = 0;
  264. while (totalNums < 52) {
  265. Random rand = new Random((int)DateTime.Now.Ticks / TicksPerMillisec);
  266. int num = rand.Next(0, 52);
  267. if (myList.Contains(num)) {
  268. //don't add it again to the list of integers
  269. }
  270. else {
  271. myList.Add(num);
  272. totalNums++;
  273. }
  274. }
  275. return myList;
  276. }
  277.  
  278. private string nameFromFace(CardFace face) {
  279. string cardName = string.Empty;
  280. switch (face) {
  281. case (CardFace.Two):
  282. cardName = "Two";
  283. break;
  284. case (CardFace.Three):
  285. cardName = "Three";
  286. break;
  287. case (CardFace.Four):
  288. cardName = "Four";
  289. break;
  290. case (CardFace.Five):
  291. cardName = "Five";
  292. break;
  293. case (CardFace.Six):
  294. cardName = "Six";
  295. break;
  296. case (CardFace.Seven):
  297. cardName = "Seven";
  298. break;
  299. case (CardFace.Eight):
  300. cardName = "Eight";
  301. break;
  302. case (CardFace.Nine):
  303. cardName = "Nine";
  304. break;
  305. case (CardFace.Ten):
  306. cardName = "Ten";
  307. break;
  308. case (CardFace.Jack):
  309. cardName = "Jack";
  310. break;
  311. case (CardFace.Queen):
  312. cardName = "Queen";
  313. break;
  314. case (CardFace.King):
  315. cardName = "King";
  316. break;
  317. case (CardFace.Ace):
  318. cardName = "Ace";
  319. break;
  320. }
  321. return cardName;
  322. }
  323.  
  324. public int numericValueFromFace(CardFace face) {
  325. //make this method public because we'll need to invoke it from a class instance
  326. int numValue = 0;
  327. switch (face) {
  328. case (CardFace.Ace):
  329. numValue = 1;
  330. break;
  331. case (CardFace.Two):
  332. numValue = 2;
  333. break;
  334. case (CardFace.Three):
  335. numValue = 3;
  336. break;
  337. case (CardFace.Four):
  338. numValue = 4;
  339. break;
  340. case (CardFace.Five):
  341. numValue = 5;
  342. break;
  343. case (CardFace.Six):
  344. numValue = 6;
  345. break;
  346. case (CardFace.Seven):
  347. numValue = 7;
  348. break;
  349. case (CardFace.Eight):
  350. numValue = 8;
  351. break;
  352. case (CardFace.Nine):
  353. numValue = 9;
  354. break;
  355. case (CardFace.Ten):
  356. numValue = 10;
  357. break;
  358. case (CardFace.Jack):
  359. numValue = 10;
  360. break;
  361. case (CardFace.Queen):
  362. numValue = 10;
  363. break;
  364. case (CardFace.King):
  365. numValue = 10;
  366. break;
  367. }
  368. return numValue;
  369. }
  370.  
  371. private string suitNameFromCardSuit(CardSuit suit) {
  372. string suitName = string.Empty;
  373. switch (suit) {
  374. case CardSuit.Clubs:
  375. suitName = "Clubs";
  376. break;
  377. case CardSuit.Diamonds:
  378. suitName = "Diamonds";
  379. break;
  380. case CardSuit.Hearts:
  381. suitName = "Hearts";
  382. break;
  383. case CardSuit.Spades:
  384. suitName = "Spades";
  385. break;
  386. }
  387. return suitName;
  388. }
  389.  
  390. #endregion
  391.  
  392. //next closing-brace ends class Card
  393. }
  394.  
  395. }

Report this snippet  

You need to login to post a comment.