fork download
  1. /* Exercise 7.12
  2.   Card Shuffling and Dealing: Dealing Poker Hands
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8.  
  9. #define SUITS 4
  10. #define FACES 13
  11. #define CARDS 52
  12. #define HAND_SIZE 5
  13.  
  14. void shuffle(int deck[][FACES]);
  15. void dealHand(int deck[][FACES],
  16. int hand[][2]);
  17.  
  18. int containsPair(int hand[][2]);
  19. int containsTwoPairs(int hand[][2]);
  20. int containsThreeOfKind(int hand[][2]);
  21. int containsFourOfKind(int hand[][2]);
  22. int containsFlush(int hand[][2]);
  23. int containsStraight(int hand[][2]);
  24.  
  25. void printHand(int hand[][2],
  26. const char *face[],
  27. const char *suit[]);
  28.  
  29.  
  30. int main(void)
  31. {
  32. int deck[SUITS][FACES] = { 0 };
  33.  
  34. int hand[HAND_SIZE][2];
  35.  
  36. const char *suit[SUITS] =
  37. { "Hearts", "Diamonds", "Clubs", "Spades" };
  38.  
  39. const char *face[FACES] =
  40. {
  41. "Ace", "Deuce", "Three", "Four", "Five",
  42. "Six", "Seven", "Eight", "Nine", "Ten",
  43. "Jack", "Queen", "King"
  44. };
  45.  
  46. srand((unsigned int)time(NULL));
  47.  
  48. shuffle(deck);
  49. dealHand(deck, hand);
  50.  
  51. printf("\nYour poker hand is:\n");
  52.  
  53. printHand(hand, face, suit);
  54.  
  55. printf("\nHand results:\n");
  56.  
  57. if (containsPair(hand))
  58. {
  59. printf("The hand contains a pair.\n");
  60. }
  61. else
  62. {
  63. printf("The hand does not contain a pair.\n");
  64. }
  65.  
  66. if (containsTwoPairs(hand))
  67. {
  68. printf("The hand contains two pairs.\n");
  69. }
  70. else
  71. {
  72. printf("The hand does not contain two pairs.\n");
  73. }
  74.  
  75. if (containsThreeOfKind(hand))
  76. {
  77. printf("The hand contains three of a kind.\n");
  78. }
  79. else
  80. {
  81. printf("The hand does not contain three of a kind.\n");
  82. }
  83.  
  84. if (containsFourOfKind(hand))
  85. {
  86. printf("The hand contains four of a kind.\n");
  87. }
  88. else
  89. {
  90. printf("The hand does not contain four of a kind.\n");
  91. }
  92.  
  93. if (containsFlush(hand))
  94. {
  95. printf("The hand contains a flush.\n");
  96. }
  97. else
  98. {
  99. printf("The hand does not contain a flush.\n");
  100. }
  101.  
  102. if (containsStraight(hand))
  103. {
  104. printf("The hand contains a straight.\n");
  105. }
  106. else
  107. {
  108. printf("The hand does not contain a straight.\n");
  109. }
  110.  
  111. return 0;
  112. }
  113.  
  114.  
  115. void shuffle(int deck[][FACES])
  116. {
  117. for (int card = 1; card <= CARDS; ++card)
  118. {
  119. int row;
  120. int column;
  121.  
  122. do
  123. {
  124. row = rand() % SUITS;
  125. column = rand() % FACES;
  126. }
  127. while (deck[row][column] != 0);
  128.  
  129. deck[row][column] = card;
  130. }
  131. }
  132.  
  133.  
  134. void dealHand(int deck[][FACES],
  135. int hand[][2])
  136. {
  137. int cardNumber = 1;
  138. int cardCount = 0;
  139.  
  140. for (int row = 0; row < SUITS; ++row)
  141. {
  142. for (int column = 0; column < FACES; ++column)
  143. {
  144. if (deck[row][column] == cardNumber)
  145. {
  146. hand[cardCount][0] = row;
  147. hand[cardCount][1] = column;
  148.  
  149. ++cardCount;
  150. ++cardNumber;
  151.  
  152. if (cardCount == HAND_SIZE)
  153. {
  154. return;
  155. }
  156. }
  157. }
  158. }
  159. }
  160.  
  161.  
  162. void printHand(int hand[][2],
  163. const char *face[],
  164. const char *suit[])
  165. {
  166. for (int i = 0; i < HAND_SIZE; ++i)
  167. {
  168. printf("%s of %s\n",
  169. face[hand[i][1]],
  170. suit[hand[i][0]]);
  171. }
  172. }
  173.  
  174.  
  175. int containsPair(int hand[][2])
  176. {
  177. for (int i = 0; i < HAND_SIZE; ++i)
  178. {
  179. for (int j = i + 1; j < HAND_SIZE; ++j)
  180. {
  181. if (hand[i][1] == hand[j][1])
  182. {
  183. return 1;
  184. }
  185. }
  186. }
  187.  
  188. return 0;
  189. }
  190.  
  191.  
  192. int containsTwoPairs(int hand[][2])
  193. {
  194. int pairs = 0;
  195.  
  196. for (int i = 0; i < HAND_SIZE; ++i)
  197. {
  198. int alreadyCounted = 0;
  199.  
  200. for (int k = 0; k < i; ++k)
  201. {
  202. if (hand[k][1] == hand[i][1])
  203. {
  204. alreadyCounted = 1;
  205. }
  206. }
  207.  
  208. if (alreadyCounted == 1)
  209. {
  210. continue;
  211. }
  212.  
  213. int count = 0;
  214.  
  215. for (int j = 0; j < HAND_SIZE; ++j)
  216. {
  217. if (hand[i][1] == hand[j][1])
  218. {
  219. ++count;
  220. }
  221. }
  222.  
  223. if (count == 2)
  224. {
  225. ++pairs;
  226. }
  227. }
  228.  
  229. return pairs >= 2;
  230. }
  231.  
  232.  
  233. int containsThreeOfKind(int hand[][2])
  234. {
  235. for (int i = 0; i < HAND_SIZE; ++i)
  236. {
  237. int count = 0;
  238.  
  239. for (int j = 0; j < HAND_SIZE; ++j)
  240. {
  241. if (hand[i][1] == hand[j][1])
  242. {
  243. ++count;
  244. }
  245. }
  246.  
  247. if (count == 3)
  248. {
  249. return 1;
  250. }
  251. }
  252.  
  253. return 0;
  254. }
  255.  
  256.  
  257. int containsFourOfKind(int hand[][2])
  258. {
  259. for (int i = 0; i < HAND_SIZE; ++i)
  260. {
  261. int count = 0;
  262.  
  263. for (int j = 0; j < HAND_SIZE; ++j)
  264. {
  265. if (hand[i][1] == hand[j][1])
  266. {
  267. ++count;
  268. }
  269. }
  270.  
  271. if (count == 4)
  272. {
  273. return 1;
  274. }
  275. }
  276.  
  277. return 0;
  278. }
  279.  
  280.  
  281. int containsFlush(int hand[][2])
  282. {
  283. for (int i = 1; i < HAND_SIZE; ++i)
  284. {
  285. if (hand[i][0] != hand[0][0])
  286. {
  287. return 0;
  288. }
  289. }
  290.  
  291. return 1;
  292. }
  293.  
  294.  
  295. int containsStraight(int hand[][2])
  296. {
  297. int values[FACES] = { 0 };
  298.  
  299. for (int i = 0; i < HAND_SIZE; ++i)
  300. {
  301. values[hand[i][1]] = 1;
  302. }
  303.  
  304. for (int start = 0; start <= FACES - HAND_SIZE; ++start)
  305. {
  306. int straight = 1;
  307.  
  308. for (int i = 0; i < HAND_SIZE; ++i)
  309. {
  310. if (values[start + i] == 0)
  311. {
  312. straight = 0;
  313. }
  314. }
  315.  
  316. if (straight == 1)
  317. {
  318. return 1;
  319. }
  320. }
  321.  
  322. /*
  323.   Special case:
  324.   Ace, Two, Three, Four, Five
  325.   */
  326.  
  327. if (values[0] == 1 &&
  328. values[1] == 1 &&
  329. values[2] == 1 &&
  330. values[3] == 1 &&
  331. values[4] == 1)
  332. {
  333. return 1;
  334. }
  335.  
  336. return 0;
  337. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Your poker hand is:
Nine of Spades
Ten of Spades
Ace of Diamonds
Ace of Hearts
Ace of Diamonds

Hand results:
The hand contains a pair.
The hand does not contain two pairs.
The hand contains three of a kind.
The hand does not contain four of a kind.
The hand does not contain a flush.
The hand does not contain a straight.