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

How many cards do you want to replace? (0-3): Enter card position to replace (1-5): Enter card position to replace (1-5): 
Your final hand:
1. Nine of Hearts
2. Deuce of Diamonds
3. Jack of Spades
4. Four of Clubs
5. Ace of Clubs
Your hand: High Card

Computer's final hand:
1. Three of Hearts
2. Ace of Hearts
3. Ten of Hearts
4. Three of Clubs
5. Queen of Hearts
Computer's hand: One Pair

Computer wins!