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

Dealer's final hand category: Two Pairs
Dealer's final hand:
Eight of Clubs
Deuce of Spades
Seven of Diamonds
Seven of Spades
Deuce of Hearts