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.  
  9. void initializeDeck(int deck[][FACES]);
  10. void shuffle(int deck[][FACES]);
  11. void printDeck(int deck[][FACES]);
  12.  
  13. void deal(int deck[][FACES],
  14. const char *face[],
  15. const char *suit[]);
  16.  
  17.  
  18. int main(void)
  19. {
  20. int deck[SUITS][FACES];
  21.  
  22. const char *suit[SUITS] =
  23. { "Hearts", "Diamonds", "Clubs", "Spades" };
  24.  
  25. const char *face[FACES] =
  26. {
  27. "Ace", "Deuce", "Three", "Four", "Five",
  28. "Six", "Seven", "Eight", "Nine", "Ten",
  29. "Jack", "Queen", "King"
  30. };
  31.  
  32. srand((unsigned int)time(NULL));
  33.  
  34. initializeDeck(deck);
  35.  
  36. printf("The deck before shuffle:\n");
  37. printDeck(deck);
  38.  
  39. shuffle(deck);
  40.  
  41. printf("\nThe deck after shuffle:\n");
  42. printDeck(deck);
  43.  
  44. printf("\nThe cards after dealing:\n");
  45. deal(deck, face, suit);
  46.  
  47. return 0;
  48. }
  49.  
  50.  
  51. void initializeDeck(int deck[][FACES])
  52. {
  53. int card = 1;
  54.  
  55. for (int row = 0; row < SUITS; ++row)
  56. {
  57. for (int column = 0; column < FACES; ++column)
  58. {
  59. deck[row][column] = card;
  60. ++card;
  61. }
  62. }
  63. }
  64.  
  65.  
  66. void shuffle(int deck[][FACES])
  67. {
  68. for (int row = 0; row < SUITS; ++row)
  69. {
  70. for (int column = 0; column < FACES; ++column)
  71. {
  72. int randomRow = rand() % SUITS;
  73. int randomColumn = rand() % FACES;
  74.  
  75. int hold = deck[row][column];
  76.  
  77. deck[row][column] =
  78. deck[randomRow][randomColumn];
  79.  
  80. deck[randomRow][randomColumn] = hold;
  81. }
  82. }
  83. }
  84.  
  85.  
  86. void printDeck(int deck[][FACES])
  87. {
  88. for (int row = 0; row < SUITS; ++row)
  89. {
  90. for (int column = 0; column < FACES; ++column)
  91. {
  92. printf("%4d", deck[row][column]);
  93. }
  94.  
  95. puts("");
  96. }
  97. }
  98.  
  99.  
  100. void deal(int deck[][FACES],
  101. const char *face[],
  102. const char *suit[])
  103. {
  104. for (int card = 1; card <= CARDS; ++card)
  105. {
  106. int found = 0;
  107.  
  108. for (int row = 0; row < SUITS && found == 0; ++row)
  109. {
  110. for (int column = 0;
  111. column < FACES;
  112. ++column)
  113. {
  114. if (deck[row][column] == card)
  115. {
  116. printf("%5s of %-8s",
  117. face[column],
  118. suit[row]);
  119.  
  120. if (card % 4 == 0)
  121. {
  122. puts("");
  123. }
  124. else
  125. {
  126. printf("\t");
  127. }
  128.  
  129. found = 1;
  130. break;
  131. }
  132. }
  133. }
  134. }
  135. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
The deck before shuffle:
   1   2   3   4   5   6   7   8   9  10  11  12  13
  14  15  16  17  18  19  20  21  22  23  24  25  26
  27  28  29  30  31  32  33  34  35  36  37  38  39
  40  41  42  43  44  45  46  47  48  49  50  51  52

The deck after shuffle:
  33  36   7   2  52  46  19  21   4  35  45  43  50
  30  47  40   8   5  25  44  22  39  37  20  51  14
  16  32  23   9  42   1  12  18  15  28  11   6  13
   3  26  38  10  27  17  41  29  49  34  24  31  48

The cards after dealing:
  Six of Clubs   	 Four of Hearts  	  Ace of Spades  	 Nine of Hearts  
 Five of Diamonds	Queen of Clubs   	Three of Hearts  	 Four of Diamonds
 Four of Clubs   	 Four of Spades  	 Jack of Clubs   	Seven of Clubs   
 King of Clubs   	 King of Diamonds	 Nine of Clubs   	  Ace of Clubs   
  Six of Spades  	Eight of Clubs   	Seven of Hearts  	 Jack of Diamonds
Eight of Hearts  	Eight of Diamonds	Three of Clubs   	 Jack of Spades  
  Six of Diamonds	Deuce of Spades  	 Five of Spades  	  Ten of Clubs   
Eight of Spades  	  Ace of Diamonds	Queen of Spades  	Deuce of Clubs   
  Ace of Hearts  	  Ten of Spades  	  Ten of Hearts  	Deuce of Hearts  
  Ten of Diamonds	Three of Spades  	 Nine of Diamonds	Three of Diamonds
Seven of Spades  	 Five of Clubs   	Queen of Hearts  	Seven of Diamonds
 Jack of Hearts  	  Six of Hearts  	Deuce of Diamonds	 King of Spades  
 Nine of Spades  	 King of Hearts  	Queen of Diamonds	 Five of Hearts