Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.72 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_RANKS 13
  27. #define MAX_SUITS 4
  28. #define COLS 1 // Number of columns to display in output.
  29.  
  30. /**
  31.  * A playing "card" structure.
  32.  * Stores the following attributes of a single playing card:
  33.  *   1) rank: The number or face (Ace, King, Queen, Jack)
  34.  *   2) suit: Clubs, Diamonds, Hearts, Spades.
  35.  *   3) color: Black or Red
  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. // Two-dimensional array of strings for suits.
  51. // All possible suits a playing card can have.
  52. char suits[MAX_SUITS][MAX] = {"Clubs", "Diamonds", "Hearts", "Spades"};
  53.  
  54. /** Function prototypes */
  55. void initialize(Card []);
  56. void shuffle(Card []);
  57. void display(const Card[]);
  58.  
  59. int main() {
  60.  
  61.   char newline = '\n'; //to repeat while loop
  62.  
  63.   // Declare an array of 52 cards.
  64.   Card deck[MAX_CARDS] = {"", ""};
  65.  
  66.   // Populate the deck.
  67.   initialize(deck);
  68.   printf("Display an ordered deck of cards:\n");
  69.   display(deck);
  70.  
  71.   // Keep shuffling and displaying the deck each time the user hits Enter.
  72.   // End the program when the user enters anything else.
  73.   while ('\n' == newline) {
  74.     printf("\nshuffling deck ... \n");
  75.     shuffle(deck);
  76.     display(deck);
  77.     printf("\nWould you like to shuffle again?\nIf so, press \"Enter\" key. ");
  78.     printf("If not, press any other key. ");
  79.     newline = getchar();
  80.   }
  81.  
  82.   return 0;
  83. }
  84.  
  85. /**
  86.  * Initializes a playing card deck of 52 cards.
  87.  * Sets the rank and suit of each card in deck[].
  88.  * Creates all the Clubs, then Diamonds, then Hearts and finally Spades cards.
  89.  * (Essentially in the order of suits[].)
  90.  * Also sets the correct corresponding color to a card's suit
  91.  *
  92.  * @param deck[] An array of cards to initialize.
  93.  */
  94. void initialize(Card deck[]) {
  95.   int i = 0;
  96.  
  97.   // Loop through the deck.
  98.   for (i = 0; i < MAX_CARDS; i++) {
  99.     // i % MAX_RANKS to cycle through each possible rank.
  100.     deck[i].rank = ranks[i % MAX_RANKS];
  101.     // i / MAX_RANKS to keep the same suit for each card in a suit.
  102.     strncpy(deck[i].suit, suits[i / MAX_RANKS], MAX);
  103.    
  104.     // Compares if card is one of the black suites
  105.     int cmpSpd = strcmp(deck[i].suit, "Spades");
  106.     int cmpClb = strcmp(deck[i].suit, "Clubs");
  107.    
  108.     // If it is a black suit, then set its color to black, otherwise red
  109.     if (cmpSpd == 0 || cmpClb == 0) {
  110.         deck[i].color = "Black";
  111.     } else {
  112.         deck[i].color = "Red";
  113.     }
  114.    
  115.   }
  116. }
  117.  
  118. /**
  119.  * Shuffles the deck (an array of cards).
  120.  * Uses the pseudo-random number generator to shuffle the cards.
  121.  * Loops through the entire deck and makes random swaps.
  122.  *
  123.  * @param deck[] An array of cards to shuffle.
  124.  */
  125. void shuffle(Card deck[]) {
  126.  
  127.   // Index of the card to be swapped.
  128.   int swapper = 0;
  129.   // Loop increment variable.
  130.   int i = 0;
  131.   // Temp holding place for the swap.
  132.   Card temp = {"", ""};
  133.  
  134.   srand(time(NULL)); // Seed the random generator.
  135.  
  136.   // Loop through the deck and make random swaps.
  137.   for (i = 0; i < MAX_CARDS; i++) {
  138.  
  139.     // Generate a pseudo-random number from 0 to 51 for the index.
  140.     swapper = rand() % MAX_CARDS;
  141.    
  142.     // Make the swap.
  143.     temp = deck[i];
  144.     deck[i] = deck[swapper];
  145.     deck[swapper] = temp;
  146.   }
  147. }
  148.  
  149. /**
  150.  * Prints the contents of the deck (an array of cards) in columns.
  151.  * Default: 1 columns (Change COL to change the number of columns)
  152.  *
  153.  * @param deck[] An array of cards to be printed.
  154.  */
  155. void display(const Card deck[]) {
  156.   int i = 0;
  157.   for (i = 0; i < MAX_CARDS; i++) {
  158.     // Added  deck[i].color to print the color of a card
  159.     printf("%5s of %-s (%-s)", deck[i].rank, deck[i].suit, deck[i].color);
  160.     // Every COL (3) cards print a newline to separate.
  161.     if (0 == ((i + 1) % COLS)) {
  162.       printf("\n");
  163.     }
  164.   }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement