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 deal(int deck[][FACES], const char* face[], const char* suit[],int row1[], int col1[],int row2[], int col2[]);
  14.  
  15. int containsPair(int handCol[]);
  16. int containsTwoPairs(int handCol[]);
  17. int containsThreeOfKind(int handCol[]);
  18. int containsFourOfKind(int handCol[]);
  19. int containsFlush(int handRow[]);
  20. int containsStraight(int handCol[]);
  21. int containsFullHouse(int handCol[]);
  22.  
  23. int evaluateHand(int handRow[], int handCol[]);
  24. const char* getHandName(int rank);
  25.  
  26.  
  27.  
  28. int main(void)
  29. {
  30. int deck[SUITS][FACES] = { 0 };
  31.  
  32. int row1[HAND_SIZE], col1[HAND_SIZE];
  33. int row2[HAND_SIZE], col2[HAND_SIZE];
  34.  
  35. const char* suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};
  36.  
  37. const char* face[FACES] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
  38.  
  39. srand(time(NULL));
  40.  
  41. shuffel(deck);
  42.  
  43. deal(deck, face, suit,row1, col1, row2, col2);
  44.  
  45. int rank1 = evaluateHand(row1, col1);
  46. int rank2 = evaluateHand(row2, col2);
  47.  
  48. printf("\nPlayer 1 hand: %s\n", getHandName(rank1));
  49. printf("Player 2 hand: %s\n", getHandName(rank2));
  50.  
  51. if (rank1 > rank2)
  52. {
  53. printf("\nPlayer 1 wins\n");
  54. }
  55. else if (rank2 > rank1)
  56. {
  57. printf("\nPlayer 2 wins\n");
  58. }
  59. else
  60. {
  61. printf("\nBoth players have the same hand category.\n");
  62. }
  63.  
  64. return 0;
  65. }
  66.  
  67.  
  68. void shuffel(int deck[][FACES])
  69. {
  70. for (int card = 1; card <= 52; card++)
  71. {
  72. int row;
  73. int column;
  74.  
  75. do
  76. {
  77. row = rand() % SUITS;
  78. column = rand() % FACES;
  79. } while (deck[row][column] != 0);
  80.  
  81. deck[row][column] = card;
  82. }
  83. }
  84.  
  85.  
  86.  
  87. void deal(int deck[][FACES],const char* face[],const char* suit[],int row1[], int col1[],int row2[], int col2[])
  88. {
  89. printf("Player 1 cards:\n");
  90.  
  91. for (int card = 1; card <= 10; card++)
  92. {
  93. for (int row = 0; row < SUITS; row++)
  94. {
  95. for (int column = 0; column < FACES; column++)
  96. {
  97. if (deck[row][column] == card)
  98. {
  99. int index = (card - 1) % HAND_SIZE;
  100.  
  101. if (card <= 5)
  102. {
  103. printf("%s of %s\n",face[column], suit[row]);
  104.  
  105. row1[index] = row;
  106. col1[index] = column;
  107. }
  108. else
  109. {
  110. if (card == 6)
  111. printf("\nPlayer 2 cards:\n");
  112.  
  113. printf("%s of %s\n",face[column], suit[row]);
  114.  
  115. row2[index] = row;
  116. col2[index] = column;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123.  
  124.  
  125.  
  126. int containsPair(int handCol[])
  127. {
  128. int count[FACES] = { 0 };
  129. int pairs = 0;
  130.  
  131. for (int i = 0; i < HAND_SIZE; i++)
  132. count[handCol[i]]++;
  133.  
  134. for (int i = 0; i < FACES; i++)
  135. {
  136. if (count[i] == 2)
  137. pairs++;
  138. }
  139.  
  140. return pairs == 1;
  141. }
  142.  
  143.  
  144.  
  145. int containsTwoPairs(int handCol[])
  146. {
  147. int count[FACES] = { 0 };
  148. int pairs = 0;
  149.  
  150. for (int i = 0; i < HAND_SIZE; i++)
  151. count[handCol[i]]++;
  152.  
  153. for (int i = 0; i < FACES; i++)
  154. {
  155. if (count[i] == 2)
  156. pairs++;
  157. }
  158.  
  159. return pairs == 2;
  160. }
  161.  
  162.  
  163.  
  164. int containsThreeOfKind(int handCol[])
  165. {
  166. int count[FACES] = { 0 };
  167.  
  168. for (int i = 0; i < HAND_SIZE; i++)
  169. count[handCol[i]]++;
  170.  
  171. for (int i = 0; i < FACES; i++)
  172. {
  173. if (count[i] == 3)
  174. return 1;
  175. }
  176.  
  177. return 0;
  178. }
  179.  
  180.  
  181.  
  182. int containsFourOfKind(int handCol[])
  183. {
  184. int count[FACES] = { 0 };
  185.  
  186. for (int i = 0; i < HAND_SIZE; i++)
  187. count[handCol[i]]++;
  188.  
  189. for (int i = 0; i < FACES; i++)
  190. {
  191. if (count[i] == 4)
  192. return 1;
  193. }
  194.  
  195. return 0;
  196. }
  197.  
  198.  
  199.  
  200. int containsFlush(int handRow[])
  201. {
  202. for (int i = 1; i < HAND_SIZE; i++)
  203. {
  204. if (handRow[i] != handRow[0])
  205. return 0;
  206. }
  207.  
  208. return 1;
  209. }
  210.  
  211.  
  212.  
  213. int containsStraight(int handCol[])
  214. {
  215. int temp[HAND_SIZE];
  216.  
  217. for (int i = 0; i < HAND_SIZE; i++)
  218. temp[i] = handCol[i];
  219.  
  220.  
  221. for (int i = 0; i < HAND_SIZE ; i++)
  222. {
  223. for (int j = 0; j < HAND_SIZE - i - 1 ; j++)
  224. {
  225. if (temp[j] > temp[j + 1])
  226. {
  227. int swap = temp[j];
  228. temp[j] = temp[j + 1];
  229. temp[j + 1] = swap;
  230. }
  231. }
  232. }
  233.  
  234.  
  235.  
  236.  
  237. for (int i = 0; i < HAND_SIZE - 1; i++)
  238. {
  239. if (temp[i + 1] != temp[i] + 1)
  240. {
  241. return 0;
  242.  
  243. }
  244. }
  245.  
  246.  
  247. return 1;
  248.  
  249.  
  250. }
  251.  
  252.  
  253. int containsFullHouse(int handCol[])
  254. {
  255.  
  256. return containsPair(handCol) && containsThreeOfKind(handCol);
  257. }
  258.  
  259.  
  260.  
  261. int evaluateHand(int handRow[], int handCol[])
  262. {
  263. int flush = containsFlush(handRow);
  264. int straight = containsStraight(handCol);
  265.  
  266. if (flush && straight)
  267. return 8;
  268.  
  269. if (containsFourOfKind(handCol))
  270. return 7;
  271.  
  272. if (containsFullHouse(handCol))
  273. return 6;
  274.  
  275. if (flush)
  276. return 5;
  277.  
  278. if (straight)
  279. return 4;
  280.  
  281. if (containsThreeOfKind(handCol))
  282. return 3;
  283.  
  284. if (containsTwoPairs(handCol))
  285. return 2;
  286.  
  287. if (containsPair(handCol))
  288. return 1;
  289.  
  290. return 0;
  291. }
  292.  
  293.  
  294.  
  295. const char* getHandName(int rank)
  296. {
  297. switch (rank)
  298. {
  299. case 8: return "Straight Flush";
  300. case 7: return "Four of a Kind";
  301. case 6: return "Full House";
  302. case 5: return "Flush";
  303. case 4: return "Straight";
  304. case 3: return "Three of a Kind";
  305. case 2: return "Two Pairs";
  306. case 1: return "One Pair";
  307. default: return "High Card";
  308. }
  309. }
  310.  
Success #stdin #stdout 0s 5252KB
stdin
Standard input is empty
stdout
Player 1 cards:
Deuce of Hearts
Five of Diamonds
Five of Clubs
Ace of Hearts
Ten of Clubs

Player 2 cards:
Ace of Spades
Five of Hearts
Deuce of Spades
Three of Hearts
Jack of Clubs

Player 1 hand: One Pair
Player 2 hand: High Card

Player 1 wins