fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define SUITS 4
  6. #define FACES 13
  7. #define CARDS 52
  8. #define HAND_SIZE 5
  9.  
  10. void shuffle(int deck[][FACES]);
  11.  
  12. void getCard(int deck[][FACES],
  13. int cardNumber,
  14. int card[]);
  15.  
  16. void printHand(int hand[][2],
  17. const char *face[],
  18. const char *suit[]);
  19.  
  20. int handValue(int hand[][2]);
  21.  
  22. int containsPair(int hand[][2]);
  23. int containsThreeOfKind(int hand[][2]);
  24. int containsFourOfKind(int hand[][2]);
  25. int containsFlush(int hand[][2]);
  26. int containsStraight(int hand[][2]);
  27.  
  28. void dealerDraw(int deck[][FACES],
  29. int hand[][2],
  30. int *nextCard);
  31.  
  32.  
  33. int main(void)
  34. {
  35. int deck[SUITS][FACES] = { 0 };
  36.  
  37. int playerHand[HAND_SIZE][2];
  38. int dealerHand[HAND_SIZE][2];
  39.  
  40. const char *suit[SUITS] =
  41. { "Hearts", "Diamonds", "Clubs", "Spades" };
  42.  
  43. const char *face[FACES] =
  44. {
  45. "Ace", "Deuce", "Three", "Four", "Five",
  46. "Six", "Seven", "Eight", "Nine", "Ten",
  47. "Jack", "Queen", "King"
  48. };
  49.  
  50. srand((unsigned int)time(NULL));
  51.  
  52. shuffle(deck);
  53.  
  54. for (int i = 0; i < HAND_SIZE; ++i)
  55. {
  56. getCard(deck, i + 1, playerHand[i]);
  57. }
  58.  
  59. for (int i = 0; i < HAND_SIZE; ++i)
  60. {
  61. getCard(deck,
  62. HAND_SIZE + i + 1,
  63. dealerHand[i]);
  64. }
  65.  
  66. printf("\nPlayer hand:\n");
  67. printHand(playerHand, face, suit);
  68.  
  69. printf("\nDealer hand before drawing:\n");
  70. printHand(dealerHand, face, suit);
  71.  
  72. int nextCard = 11;
  73.  
  74. dealerDraw(deck, dealerHand, &nextCard);
  75.  
  76. printf("\nDealer hand after drawing:\n");
  77. printHand(dealerHand, face, suit);
  78.  
  79. printf("\nPlayer hand value = %d\n",
  80. handValue(playerHand));
  81.  
  82. printf("Dealer hand value = %d\n",
  83. handValue(dealerHand));
  84.  
  85. if (handValue(playerHand) > handValue(dealerHand))
  86. {
  87. printf("\nPlayer wins.\n");
  88. }
  89. else if (handValue(dealerHand) > handValue(playerHand))
  90. {
  91. printf("\nDealer wins.\n");
  92. }
  93. else
  94. {
  95. printf("\nThe two hands are equal.\n");
  96. }
  97.  
  98. return 0;
  99. }
  100.  
  101.  
  102. void shuffle(int deck[][FACES])
  103. {
  104. for (int card = 1; card <= CARDS; ++card)
  105. {
  106. int row;
  107. int column;
  108.  
  109. do
  110. {
  111. row = rand() % SUITS;
  112. column = rand() % FACES;
  113. }
  114. while (deck[row][column] != 0);
  115.  
  116. deck[row][column] = card;
  117. }
  118. }
  119.  
  120.  
  121. void getCard(int deck[][FACES],
  122. int cardNumber,
  123. int card[])
  124. {
  125. for (int row = 0; row < SUITS; ++row)
  126. {
  127. for (int column = 0; column < FACES; ++column)
  128. {
  129. if (deck[row][column] == cardNumber)
  130. {
  131. card[0] = row;
  132. card[1] = column;
  133.  
  134. return;
  135. }
  136. }
  137. }
  138. }
  139.  
  140.  
  141. void printHand(int hand[][2],
  142. const char *face[],
  143. const char *suit[])
  144. {
  145. for (int i = 0; i < HAND_SIZE; ++i)
  146. {
  147. printf("%s of %s\n",
  148. face[hand[i][1]],
  149. suit[hand[i][0]]);
  150. }
  151. }
  152.  
  153.  
  154. void dealerDraw(int deck[][FACES],
  155. int hand[][2],
  156. int *nextCard)
  157. {
  158. int value = handValue(hand);
  159.  
  160. if (value >= 3)
  161. {
  162. return;
  163. }
  164.  
  165. int keep[HAND_SIZE] = { 0 };
  166.  
  167. if (containsFourOfKind(hand))
  168. {
  169. for (int i = 0; i < HAND_SIZE; ++i)
  170. {
  171. int count = 0;
  172.  
  173. for (int j = 0; j < HAND_SIZE; ++j)
  174. {
  175. if (hand[i][1] == hand[j][1])
  176. {
  177. ++count;
  178. }
  179. }
  180.  
  181. if (count == 4)
  182. {
  183. keep[i] = 1;
  184. }
  185. }
  186. }
  187. else if (containsThreeOfKind(hand))
  188. {
  189. for (int i = 0; i < HAND_SIZE; ++i)
  190. {
  191. int count = 0;
  192.  
  193. for (int j = 0; j < HAND_SIZE; ++j)
  194. {
  195. if (hand[i][1] == hand[j][1])
  196. {
  197. ++count;
  198. }
  199. }
  200.  
  201. if (count == 3)
  202. {
  203. keep[i] = 1;
  204. }
  205. }
  206. }
  207. else if (containsPair(hand))
  208. {
  209. for (int i = 0; i < HAND_SIZE; ++i)
  210. {
  211. int count = 0;
  212.  
  213. for (int j = 0; j < HAND_SIZE; ++j)
  214. {
  215. if (hand[i][1] == hand[j][1])
  216. {
  217. ++count;
  218. }
  219. }
  220.  
  221. if (count == 2)
  222. {
  223. keep[i] = 1;
  224. }
  225. }
  226. }
  227.  
  228. for (int i = 0; i < HAND_SIZE; ++i)
  229. {
  230. if (keep[i] == 0)
  231. {
  232. getCard(deck, *nextCard, hand[i]);
  233.  
  234. ++(*nextCard);
  235. }
  236. }
  237. }
  238.  
  239.  
  240. int containsPair(int hand[][2])
  241. {
  242. for (int i = 0; i < HAND_SIZE; ++i)
  243. {
  244. for (int j = i + 1; j < HAND_SIZE; ++j)
  245. {
  246. if (hand[i][1] == hand[j][1])
  247. {
  248. return 1;
  249. }
  250. }
  251. }
  252.  
  253. return 0;
  254. }
  255.  
  256.  
  257. int containsThreeOfKind(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 == 3)
  272. {
  273. return 1;
  274. }
  275. }
  276.  
  277. return 0;
  278. }
  279.  
  280.  
  281. int containsFourOfKind(int hand[][2])
  282. {
  283. for (int i = 0; i < HAND_SIZE; ++i)
  284. {
  285. int count = 0;
  286.  
  287. for (int j = 0; j < HAND_SIZE; ++j)
  288. {
  289. if (hand[i][1] == hand[j][1])
  290. {
  291. ++count;
  292. }
  293. }
  294.  
  295. if (count == 4)
  296. {
  297. return 1;
  298. }
  299. }
  300.  
  301. return 0;
  302. }
  303.  
  304.  
  305. int containsFlush(int hand[][2])
  306. {
  307. for (int i = 1; i < HAND_SIZE; ++i)
  308. {
  309. if (hand[i][0] != hand[0][0])
  310. {
  311. return 0;
  312. }
  313. }
  314.  
  315. return 1;
  316. }
  317.  
  318.  
  319. int containsStraight(int hand[][2])
  320. {
  321. int values[FACES] = { 0 };
  322.  
  323. for (int i = 0; i < HAND_SIZE; ++i)
  324. {
  325. values[hand[i][1]] = 1;
  326. }
  327.  
  328. for (int start = 0; start <= 8; ++start)
  329. {
  330. int straight = 1;
  331.  
  332. for (int i = 0; i < HAND_SIZE; ++i)
  333. {
  334. if (values[start + i] == 0)
  335. {
  336. straight = 0;
  337. }
  338. }
  339.  
  340. if (straight == 1)
  341. {
  342. return 1;
  343. }
  344. }
  345.  
  346. if (values[0] == 1 &&
  347. values[1] == 1 &&
  348. values[2] == 1 &&
  349. values[3] == 1 &&
  350. values[4] == 1)
  351. {
  352. return 1;
  353. }
  354.  
  355. return 0;
  356. }
  357.  
  358.  
  359. int handValue(int hand[][2])
  360. {
  361. if (containsFourOfKind(hand))
  362. {
  363. return 6;
  364. }
  365.  
  366. if (containsFlush(hand) &&
  367. containsStraight(hand))
  368. {
  369. return 5;
  370. }
  371.  
  372. if (containsThreeOfKind(hand) &&
  373. containsPair(hand))
  374. {
  375. return 4;
  376. }
  377.  
  378. if (containsFlush(hand) ||
  379. containsStraight(hand))
  380. {
  381. return 3;
  382. }
  383.  
  384. if (containsThreeOfKind(hand))
  385. {
  386. return 2;
  387. }
  388.  
  389. if (containsPair(hand))
  390. {
  391. return 1;
  392. }
  393.  
  394. return 0;
  395. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Player hand:
Ace of Clubs
Three of Spades
Three of Clubs
Queen of Hearts
Four of Clubs

Dealer hand before drawing:
Six of Clubs
Ace of Hearts
Jack of Diamonds
Nine of Hearts
Five of Diamonds

Dealer hand after drawing:
Eight of Diamonds
Queen of Spades
Nine of Spades
King of Spades
Deuce of Clubs

Player hand value = 1
Dealer hand value = 0

Player wins.