Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h> // Time function.
  3. #include <stdlib.h> // Random number generator functions.
  4. #include <string.h>
  5.  
  6. /**
  7.  * Simulates shuffling a deck of playing 52 cards each time the user
  8.  * hits Enter. Creates a deck of playing cards by defining a "card" structure
  9.  * and storing "card" structures in an array. Shuffles and displays the deck.
  10.  * NO jokers.
  11.  *
  12.  * Uses strings as a char array and a char* array.
  13.  *
  14.  * To compile this file alone:
  15.  *   % gcc cards.c
  16.  * To run:
  17.  *   % ./a.out
  18.  *
  19.  * @modified_author Graham Francisco
  20.  * @author Ed Meyer
  21.  * @original_author William Albritton
  22.  */
  23.  
  24. #define MAX 9 // The max length of a suit: Diamonds + '\0'
  25. #define MAX_CARDS 52
  26. #define MAX_COLORS 2
  27. #define MAX_RANKS 13
  28. #define MAX_SUITS 4
  29. #define COLS 1 // Number of columns to display in output.
  30.  
  31. /**
  32.  * A playing "card" structure.
  33.  * Stores the following attributes of a single playing card:
  34.  *   1) rank: The number or face (Ace, King, Queen, Jack)
  35.  *   2) suit: Clubs, Diamonds, Hearts, Spades.
  36.  */
  37. struct card {
  38.   char *rank;
  39.   char suit[MAX];
  40.   char *color;
  41. };
  42. // Declares Card as an alias/synonym for struct card.
  43. typedef struct card Card;
  44.  
  45. // All possible ranks a playing card can have.
  46. char *ranks[MAX_RANKS] = {"Ace", "Two", "Three", "Four", "Five",
  47.                           "Six", "Seven", "Eight", "Nine", "Ten",
  48.                           "Jack", "Queen", "King"};
  49.  
  50. // All possible colors of a card
  51. char *colors[MAX_COLORS] = {"Red", "Black"};
  52.  
  53. // Two-dimensional array of strings for suits.
  54. // All possible suits a playing card can have.
  55. char suits[MAX_SUITS][MAX] = {"Clubs", "Diamonds", "Hearts", "Spades"};
  56.  
  57. /** Function prototypes */
  58. void initialize(Card []);
  59. void shuffle(Card []);
  60. void display(const Card[]);
  61.  
  62. int main() {
  63.  
  64.   char newline = '\n'; //to repeat while loop
  65.  
  66.   // Declare an array of 52 cards.
  67.   Card deck[MAX_CARDS] = {"", ""};
  68.  
  69.   // Populate the deck.
  70.   initialize(deck);
  71.   printf("Display an ordered deck of cards:\n");
  72.   display(deck);
  73.  
  74.   // Keep shuffling and displaying the deck each time the user hits Enter.
  75.   // End the program when the user enters anything else.
  76.   while ('\n' == newline) {
  77.     printf("\nshuffling deck ... \n");
  78.     shuffle(deck);
  79.     display(deck);
  80.     printf("\nWould you like to shuffle again?\nIf so, press \"Enter\" key. ");
  81.     printf("If not, press any other key. ");
  82.     newline = getchar();
  83.   }
  84.  
  85.   return 0;
  86. }
  87.  
  88. /**
  89.  * Initializes a playing card deck of 52 cards.
  90.  * Sets the rank and suit of each card in deck[].
  91.  * Creates all the Clubs, then Diamonds, then Hearts and finally Spades cards.
  92.  * (Essentially in the order of suits[].)
  93.  *
  94.  * @param deck[] An array of cards to initialize.
  95.  */
  96. void initialize(Card deck[]) {
  97.   int i = 0;
  98.  
  99.   // Loop through the deck.
  100.   for (i = 0; i < MAX_CARDS; i++) {
  101.     // i % MAX_RANKS to cycle through each possible rank.
  102.     deck[i].rank = ranks[i % MAX_RANKS];
  103.     // i / MAX_RANKS to keep the same suit for each card in a suit.
  104.     strncpy(deck[i].suit, suits[i / MAX_RANKS], MAX);
  105.    
  106.     // Compares if card is one of the black suites
  107.     int cmpSpd = strcmp(deck[i].suit, "Spades");
  108.     int cmpClb = strcmp(deck[i].suit, "Clubs");
  109.    
  110.     // If it is a black suit, then set its color to black, otherwise red
  111.     if (cmpSpd == 0 || cmpClb == 0) {
  112.         deck[i].color = "Black";
  113.     } else {
  114.         deck[i].color = "Red";
  115.     }
  116.    
  117.   }
  118. }
  119.  
  120. /**
  121.  * Shuffles the deck (an array of cards).
  122.  * Uses the pseudo-random number generator to shuffle the cards.
  123.  * Loops through the entire deck and makes random swaps.
  124.  *
  125.  * @param deck[] An array of cards to shuffle.
  126.  */
  127. void shuffle(Card deck[]) {
  128.  
  129.   // Index of the card to be swapped.
  130.   int swapper = 0;
  131.   // Loop increment variable.
  132.   int i = 0;
  133.   // Temp holding place for the swap.
  134.   Card temp = {"", ""};
  135.  
  136.   srand(time(NULL)); // Seed the random generator.
  137.  
  138.   // Loop through the deck and make random swaps.
  139.   for (i = 0; i < MAX_CARDS; i++) {
  140.  
  141.     // Generate a pseudo-random number from 0 to 51 for the index.
  142.     swapper = rand() % MAX_CARDS;
  143.    
  144.     // Make the swap.
  145.     temp = deck[i];
  146.     deck[i] = deck[swapper];
  147.     deck[swapper] = temp;
  148.   }
  149. }
  150.  
  151. /**
  152.  * Prints the contents of the deck (an array of cards) in columns.
  153.  * Default: 3 columns (Change COL to change the number of columns)
  154.  *
  155.  * @param deck[] An array of cards to be printed.
  156.  */
  157. void display(const Card deck[]) {
  158.   int i = 0;
  159.   for (i = 0; i < MAX_CARDS; i++) {
  160.     // Added  deck[i].color to print the color of a card
  161.     printf("%5s of %-s (%-s)", deck[i].rank, deck[i].suit, deck[i].color);
  162.     // Every COL (3) cards print a newline to separate.
  163.     if (0 == ((i + 1) % COLS)) {
  164.       printf("\n");
  165.     }
  166.   }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement