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. // Function prototypes
  10. void shuffle(int wDeck[][FACES]);
  11. void deal(const int wDeck[][FACES], int hand[][2], const char *wFace[], const char *wSuit[]);
  12. void evaluateHand(const int hand[][2]);
  13. int hasPair(const int hand[][2]);
  14. int hasTwoPairs(const int hand[][2]);
  15. int hasThreeOfAKind(const int hand[][2]);
  16. int hasFourOfAKind(const int hand[][2]);
  17. int hasFlush(const int hand[][2]);
  18. int hasStraight(const int hand[][2]);
  19.  
  20. int main(void) {
  21. const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};
  22. const char *face[FACES] = {
  23. "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
  24. "Eight", "Nine", "Ten", "Jack", "Queen", "King"
  25. };
  26.  
  27. int deck[SUITS][FACES] = {0};
  28. int hand[HAND_SIZE][2]; // hand[i][0] = face index, hand[i][1] = suit index
  29.  
  30. srand(time(NULL));
  31. shuffle(deck);
  32. deal(deck, hand, face, suit);
  33. evaluateHand(hand);
  34.  
  35. return 0;
  36. }
  37.  
  38. // Shuffle cards in deck
  39. void shuffle(int wDeck[][FACES]) {
  40. for (int card = 1; card <= 52; ++card) {
  41. int row = rand() % SUITS;
  42. int column = rand() % FACES;
  43. while (wDeck[row][column] != 0) {
  44. row = rand() % SUITS;
  45. column = rand() % FACES;
  46. }
  47. wDeck[row][column] = card;
  48. }
  49. }
  50.  
  51. // Deal 5 cards to the player
  52. void deal(const int wDeck[][FACES], int hand[][2], const char *wFace[], const char *wSuit[]) {
  53. int cardCount = 0;
  54. printf("Player's Hand:\n");
  55.  
  56. for (int c = 1; c <= HAND_SIZE; ++c) {
  57. for (int row = 0; row < SUITS; ++row) {
  58. for (int col = 0; col < FACES; ++col) {
  59. if (wDeck[row][col] == c) {
  60. hand[cardCount][0] = col;
  61. hand[cardCount][1] = row;
  62. printf(" %s of %s\n", wFace[col], wSuit[row]);
  63. cardCount++;
  64. }
  65. }
  66. }
  67. }
  68. printf("\n");
  69. }
  70.  
  71. // Evaluate the poker hand using the required functions
  72. void evaluateHand(const int hand[][2]) {
  73. printf("Hand Analysis:\n");
  74.  
  75. if (hasFourOfAKind(hand)) {
  76. printf("- Contains Four of a Kind\n");
  77. }
  78. if (hasThreeOfAKind(hand)) {
  79. printf("- Contains Three of a Kind\n");
  80. }
  81. if (hasTwoPairs(hand)) {
  82. printf("- Contains Two Pairs\n");
  83. } else if (hasPair(hand)) {
  84. printf("- Contains a Pair\n");
  85. }
  86. if (hasFlush(hand)) {
  87. printf("- Contains a Flush\n");
  88. }
  89. if (hasStraight(hand)) {
  90. printf("- Contains a Straight\n");
  91. }
  92.  
  93. if (!hasPair(hand) && !hasTwoPairs(hand) && !hasThreeOfAKind(hand) &&
  94. !hasFourOfAKind(hand) && !hasFlush(hand) && !hasStraight(hand)) {
  95. printf("- High Card hand (No special combinations)\n");
  96. }
  97. }
  98.  
  99. // a) Determine whether the hand contains a pair
  100. int hasPair(const int hand[][2]) {
  101. int faceCount[FACES] = {0};
  102. for (int i = 0; i < HAND_SIZE; ++i) {
  103. faceCount[hand[i][0]]++;
  104. }
  105. int pairs = 0;
  106. for (int i = 0; i < FACES; ++i) {
  107. if (faceCount[i] == 2) {
  108. pairs++;
  109. }
  110. }
  111. return (pairs == 1);
  112. }
  113.  
  114. // b) Determine whether the hand contains two pairs
  115. int hasTwoPairs(const int hand[][2]) {
  116. int faceCount[FACES] = {0};
  117. for (int i = 0; i < HAND_SIZE; ++i) {
  118. faceCount[hand[i][0]]++;
  119. }
  120. int pairs = 0;
  121. for (int i = 0; i < FACES; ++i) {
  122. if (faceCount[i] == 2) {
  123. pairs++;
  124. }
  125. }
  126. return (pairs == 2);
  127. }
  128.  
  129. // c) Determine whether the hand contains three of a kind
  130. int hasThreeOfAKind(const int hand[][2]) {
  131. int faceCount[FACES] = {0};
  132. for (int i = 0; i < HAND_SIZE; ++i) {
  133. faceCount[hand[i][0]]++;
  134. }
  135. for (int i = 0; i < FACES; ++i) {
  136. if (faceCount[i] == 3) {
  137. return 1;
  138. }
  139. }
  140. return 0;
  141. }
  142.  
  143. // d) Determine whether the hand contains four of a kind
  144. int hasFourOfAKind(const int hand[][2]) {
  145. int faceCount[FACES] = {0};
  146. for (int i = 0; i < HAND_SIZE; ++i) {
  147. faceCount[hand[i][0]]++;
  148. }
  149. for (int i = 0; i < FACES; ++i) {
  150. if (faceCount[i] == 4) {
  151. return 1;
  152. }
  153. }
  154. return 0;
  155. }
  156.  
  157. // e) Determine whether the hand contains a flush (all cards same suit)
  158. int hasFlush(const int hand[][2]) {
  159. int suitRef = hand[0][1];
  160. for (int i = 1; i < HAND_SIZE; ++i) {
  161. if (hand[i][1] != suitRef) {
  162. return 0;
  163. }
  164. }
  165. return 1;
  166. }
  167.  
  168. // f) Determine whether the hand contains a straight (consecutive face values)
  169. int hasStraight(const int hand[][2]) {
  170. int faces[HAND_SIZE];
  171. for (int i = 0; i < HAND_SIZE; ++i) {
  172. faces[i] = hand[i][0];
  173. }
  174.  
  175. // Sort faces array using bubble sort
  176. for (int i = 0; i < HAND_SIZE - 1; ++i) {
  177. for (int j = 0; j < HAND_SIZE - i - 1; ++j) {
  178. if (faces[j] > faces[j + 1]) {
  179. int temp = faces[j];
  180. faces[j] = faces[j + 1];
  181. faces[j + 1] = temp;
  182. }
  183. }
  184. }
  185.  
  186. // Check consecutive values
  187. for (int i = 0; i < HAND_SIZE - 1; ++i) {
  188. if (faces[i + 1] != faces[i] + 1) {
  189. return 0;
  190. }
  191. }
  192. return 1;
  193. }
  194.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Player's Hand:
  Nine of Clubs
  Ace of Spades
  Five of Hearts
  Seven of Diamonds
  Jack of Spades

Hand Analysis:
- High Card hand (No special combinations)