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 containsPair(int hand[][2]);
  21. int containsTwoPairs(int hand[][2]);
  22. int containsThreeOfKind(int hand[][2]);
  23. int containsFourOfKind(int hand[][2]);
  24. int containsFlush(int hand[][2]);
  25. int containsStraight(int hand[][2]);
  26.  
  27. int handValue(int hand[][2]);
  28.  
  29. void playerDraw(int deck[][FACES],
  30. int hand[][2],
  31. int *nextCard);
  32.  
  33. void dealerDraw(int deck[][FACES],
  34. int hand[][2],
  35. int *nextCard);
  36.  
  37.  
  38. int main(void)
  39. {
  40. int deck[SUITS][FACES] = { 0 };
  41.  
  42. int playerHand[HAND_SIZE][2];
  43. int dealerHand[HAND_SIZE][2];
  44.  
  45. const char *suit[SUITS] =
  46. { "Hearts", "Diamonds", "Clubs", "Spades" };
  47.  
  48. const char *face[FACES] =
  49. {
  50. "Ace", "Deuce", "Three", "Four", "Five",
  51. "Six", "Seven", "Eight", "Nine", "Ten",
  52. "Jack", "Queen", "King"
  53. };
  54.  
  55. srand((unsigned int)time(NULL));
  56.  
  57. shuffle(deck);
  58.  
  59. for (int i = 0; i < HAND_SIZE; ++i)
  60. {
  61. getCard(deck, i + 1, playerHand[i]);
  62. }
  63.  
  64. for (int i = 0; i < HAND_SIZE; ++i)
  65. {
  66. getCard(deck,
  67. HAND_SIZE + i + 1,
  68. dealerHand[i]);
  69. }
  70.  
  71. int nextCard = 11;
  72.  
  73. printf("Your hand before drawing:\n");
  74. printHand(playerHand, face, suit);
  75.  
  76. playerDraw(deck, playerHand, &nextCard);
  77.  
  78. printf("\nYour hand after drawing:\n");
  79. printHand(playerHand, face, suit);
  80.  
  81. dealerDraw(deck, dealerHand, &nextCard);
  82.  
  83. printf("\nDealer hand:\n");
  84. printHand(dealerHand, face, suit);
  85.  
  86. printf("\nYour hand value = %d\n",
  87. handValue(playerHand));
  88.  
  89. printf("Dealer hand value = %d\n",
  90. handValue(dealerHand));
  91.  
  92. if (handValue(playerHand) > handValue(dealerHand))
  93. {
  94. printf("\nYou win.\n");
  95. }
  96. else if (handValue(dealerHand) > handValue(playerHand))
  97. {
  98. printf("\nDealer wins.\n");
  99. }
  100. else
  101. {
  102. printf("\nThe game is tied.\n");
  103. }
  104.  
  105. return 0;
  106. }
  107.  
  108.  
  109. void shuffle(int deck[][FACES])
  110. {
  111. for (int card = 1; card <= CARDS; ++card)
  112. {
  113. int row;
  114. int column;
  115.  
  116. do
  117. {
  118. row = rand() % SUITS;
  119. column = rand() % FACES;
  120. }
  121. while (deck[row][column] != 0);
  122.  
  123. deck[row][column] = card;
  124. }
  125. }
  126.  
  127.  
  128. void getCard(int deck[][FACES],
  129. int cardNumber,
  130. int card[])
  131. {
  132. for (int row = 0; row < SUITS; ++row)
  133. {
  134. for (int column = 0; column < FACES; ++column)
  135. {
  136. if (deck[row][column] == cardNumber)
  137. {
  138. card[0] = row;
  139. card[1] = column;
  140.  
  141. return;
  142. }
  143. }
  144. }
  145. }
  146.  
  147.  
  148. void printHand(int hand[][2],
  149. const char *face[],
  150. const char *suit[])
  151. {
  152. for (int i = 0; i < HAND_SIZE; ++i)
  153. {
  154. printf("%d. %s of %s\n",
  155. i + 1,
  156. face[hand[i][1]],
  157. suit[hand[i][0]]);
  158. }
  159. }
  160.  
  161.  
  162. void playerDraw(int deck[][FACES],
  163. int hand[][2],
  164. int *nextCard)
  165. {
  166. int choice;
  167.  
  168. printf("\nHow many cards do you want to replace? ");
  169. scanf("%d", &choice);
  170.  
  171. if (choice < 0)
  172. {
  173. choice = 0;
  174. }
  175.  
  176. if (choice > HAND_SIZE)
  177. {
  178. choice = HAND_SIZE;
  179. }
  180.  
  181. for (int i = 0; i < choice; ++i)
  182. {
  183. int cardNumber;
  184.  
  185. printf("Enter the number of the card to replace: ");
  186. scanf("%d", &cardNumber);
  187.  
  188. if (cardNumber >= 1 && cardNumber <= HAND_SIZE)
  189. {
  190. getCard(deck, *nextCard, hand[cardNumber - 1]);
  191.  
  192. ++(*nextCard);
  193. }
  194. else
  195. {
  196. printf("Invalid card number.\n");
  197. --i;
  198. }
  199. }
  200. }
  201.  
  202.  
  203. void dealerDraw(int deck[][FACES],
  204. int hand[][2],
  205. int *nextCard)
  206. {
  207. int keep[HAND_SIZE] = { 0 };
  208.  
  209. if (containsFourOfKind(hand))
  210. {
  211. for (int i = 0; i < HAND_SIZE; ++i)
  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 == 4)
  224. {
  225. keep[i] = 1;
  226. }
  227. }
  228. }
  229. else if (containsThreeOfKind(hand))
  230. {
  231. for (int i = 0; i < HAND_SIZE; ++i)
  232. {
  233. int count = 0;
  234.  
  235. for (int j = 0; j < HAND_SIZE; ++j)
  236. {
  237. if (hand[i][1] == hand[j][1])
  238. {
  239. ++count;
  240. }
  241. }
  242.  
  243. if (count == 3)
  244. {
  245. keep[i] = 1;
  246. }
  247. }
  248. }
  249. else if (containsTwoPairs(hand))
  250. {
  251. for (int i = 0; i < HAND_SIZE; ++i)
  252. {
  253. int count = 0;
  254.  
  255. for (int j = 0; j < HAND_SIZE; ++j)
  256. {
  257. if (hand[i][1] == hand[j][1])
  258. {
  259. ++count;
  260. }
  261. }
  262.  
  263. if (count == 2)
  264. {
  265. keep[i] = 1;
  266. }
  267. }
  268. }
  269. else if (containsPair(hand))
  270. {
  271. for (int i = 0; i < HAND_SIZE; ++i)
  272. {
  273. int count = 0;
  274.  
  275. for (int j = 0; j < HAND_SIZE; ++j)
  276. {
  277. if (hand[i][1] == hand[j][1])
  278. {
  279. ++count;
  280. }
  281. }
  282.  
  283. if (count == 2)
  284. {
  285. keep[i] = 1;
  286. }
  287. }
  288. }
  289.  
  290. for (int i = 0; i < HAND_SIZE; ++i)
  291. {
  292. if (keep[i] == 0)
  293. {
  294. getCard(deck, *nextCard, hand[i]);
  295.  
  296. ++(*nextCard);
  297. }
  298. }
  299. }
  300.  
  301.  
  302. int containsPair(int hand[][2])
  303. {
  304. for (int i = 0; i < HAND_SIZE; ++i)
  305. {
  306. for (int j = i + 1; j < HAND_SIZE; ++j)
  307. {
  308. if (hand[i][1] == hand[j][1])
  309. {
  310. return 1;
  311. }
  312. }
  313. }
  314.  
  315. return 0;
  316. }
  317.  
  318.  
  319. int containsTwoPairs(int hand[][2])
  320. {
  321. int pairs = 0;
  322.  
  323. for (int i = 0; i < HAND_SIZE; ++i)
  324. {
  325. int count = 0;
  326.  
  327. for (int j = 0; j < HAND_SIZE; ++j)
  328. {
  329. if (hand[i][1] == hand[j][1])
  330. {
  331. ++count;
  332. }
  333. }
  334.  
  335. if (count == 2)
  336. {
  337. int alreadyCounted = 0;
  338.  
  339. for (int k = 0; k < i; ++k)
  340. {
  341. if (hand[k][1] == hand[i][1])
  342. {
  343. alreadyCounted = 1;
  344. }
  345. }
  346.  
  347. if (alreadyCounted == 0)
  348. {
  349. ++pairs;
  350. }
  351. }
  352. }
  353.  
  354. return pairs >= 2;
  355. }
  356.  
  357.  
  358. int containsThreeOfKind(int hand[][2])
  359. {
  360. for (int i = 0; i < HAND_SIZE; ++i)
  361. {
  362. int count = 0;
  363.  
  364. for (int j = 0; j < HAND_SIZE; ++j)
  365. {
  366. if (hand[i][1] == hand[j][1])
  367. {
  368. ++count;
  369. }
  370. }
  371.  
  372. if (count == 3)
  373. {
  374. return 1;
  375. }
  376. }
  377.  
  378. return 0;
  379. }
  380.  
  381.  
  382. int containsFourOfKind(int hand[][2])
  383. {
  384. for (int i = 0; i < HAND_SIZE; ++i)
  385. {
  386. int count = 0;
  387.  
  388. for (int j = 0; j < HAND_SIZE; ++j)
  389. {
  390. if (hand[i][1] == hand[j][1])
  391. {
  392. ++count;
  393. }
  394. }
  395.  
  396. if (count == 4)
  397. {
  398. return 1;
  399. }
  400. }
  401.  
  402. return 0;
  403. }
  404.  
  405.  
  406. int containsFlush(int hand[][2])
  407. {
  408. for (int i = 1; i < HAND_SIZE; ++i)
  409. {
  410. if (hand[i][0] != hand[0][0])
  411. {
  412. return 0;
  413. }
  414. }
  415.  
  416. return 1;
  417. }
  418.  
  419.  
  420. int containsStraight(int hand[][2])
  421. {
  422. int values[FACES] = { 0 };
  423.  
  424. for (int i = 0; i < HAND_SIZE; ++i)
  425. {
  426. values[hand[i][1]] = 1;
  427. }
  428.  
  429. for (int start = 0; start <= 8; ++start)
  430. {
  431. int straight = 1;
  432.  
  433. for (int i = 0; i < HAND_SIZE; ++i)
  434. {
  435. if (values[start + i] == 0)
  436. {
  437. straight = 0;
  438. }
  439. }
  440.  
  441. if (straight == 1)
  442. {
  443. return 1;
  444. }
  445. }
  446.  
  447. if (values[0] == 1 &&
  448. values[1] == 1 &&
  449. values[2] == 1 &&
  450. values[3] == 1 &&
  451. values[4] == 1)
  452. {
  453. return 1;
  454. }
  455.  
  456. return 0;
  457. }
  458.  
  459.  
  460. int handValue(int hand[][2])
  461. {
  462. if (containsFourOfKind(hand))
  463. {
  464. return 6;
  465. }
  466.  
  467. if (containsFlush(hand) &&
  468. containsStraight(hand))
  469. {
  470. return 5;
  471. }
  472.  
  473. if (containsThreeOfKind(hand) &&
  474. containsPair(hand))
  475. {
  476. return 4;
  477. }
  478.  
  479. if (containsFlush(hand) ||
  480. containsStraight(hand))
  481. {
  482. return 3;
  483. }
  484.  
  485. if (containsThreeOfKind(hand))
  486. {
  487. return 2;
  488. }
  489.  
  490. if (containsTwoPairs(hand))
  491. {
  492. return 1;
  493. }
  494.  
  495. if (containsPair(hand))
  496. {
  497. return 0;
  498. }
  499.  
  500. return -1;
  501. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Your hand before drawing:
1. Four of Spades
2. Five of Clubs
3. Three of Spades
4. Nine of Spades
5. Nine of Diamonds

How many cards do you want to replace? 
Your hand after drawing:
1. Four of Spades
2. Five of Clubs
3. Three of Spades
4. Nine of Spades
5. Nine of Diamonds

Dealer hand:
1. Ten of Clubs
2. Four of Diamonds
3. Eight of Diamonds
4. Ten of Hearts
5. Deuce of Clubs

Your hand value = 0
Dealer hand value = 0

The game is tied.