Thread: Euchre program only printing out Heart suit

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    11

    Question Euchre program only printing out Heart suit

    I'm supposed to make a euchre program in C. However, when I try running it the output is the following:

    Nine of Hearts
    Ten of Hearts
    Queen of Hearts
    Ace of Hearts

    Jack of Hearts



    It does change what the face is, but it's always X of Hearts. I think it might be #CARDS 5.

    If I increase #CARDS with another number here is the output:

    #CARDS 10 outputs both Hearts and Diamonds

    #CARDS 15 outputs Hearts, Diamonds, and Clubs

    #CARDS 20 outputs Hearts, Diamonds, Clubs, and Spades


    Is there a way I can fix this to allow the suits to be randomized with #CARDS 5?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define CARDS 5
    #define FACES 6
    
    // *******************begin replacement section*****************************
    // card structure definition
    struct card {
        const char *face;
        const char *suit;
    };
    
    typedef struct card Card;
    // *******************end replacement section*******************************
    // Can also use the following code instead of begin/end replacement section
    // typedef struct card {
    //     const char *face;
    //     const char *suit;
    // } Card;
    // *************************************************************************
    
    // prototypes
    void fillDeck(Card * const wDeck, const char *wFace[], const char *wSuit[]);
    void shuffle(Card * const wDeck);
    void deal(const Card * const wDeck);
    
    int main(void) {
        Card deck[CARDS]; // define array of Cards
    
        // initialize array of pointers
        const char *face[] = {"Ace", "Nine", "Ten", "Jack", "Queen", "King"};
    
        // initialize array of pointers
        const char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
    
        srand(time(NULL)); // seed RNG
    
        fillDeck(deck, face, suit); // load the deck with Cards
        shuffle(deck); // put Cards in random order
        deal(deck); // deal all 52 cards
    }
    
    // place strings into Card structures
    void fillDeck(Card * const wDeck, const char *wFace[], const char *wSuit[])
    {
        // loop through wDeck
        for (size_t i=0; i<CARDS; i++) {
            // wDeck indexing works like a normal array
            // Use . operator to access members of struct for
            // specific array elements
            wDeck[i].face = wFace[i % FACES];
            wDeck[i].suit = wSuit[i / FACES];
        }
    } // end fillDeck function
    
    // shuffle cards
    void shuffle(Card * const wDeck) {
        // loop through wDeck randomly swapping Cards
        for (size_t i=0; i<CARDS; i++) {
            size_t j = rand() % CARDS;
            Card temp = wDeck[i];
            wDeck[i] = wDeck[j];
            wDeck[j] = temp;
        }
    } // end shuffle function
    
    // deal cards
    void deal(const Card * const wDeck) {
        // loop through wDeck
        for (size_t i=0; i<CARDS; i++) {
            printf("%5s of %-8s%s\n", wDeck[i].face, wDeck[i].suit, 
                    (i+1) % 4 ? "  " : "\n");
        }
    } // end deal function

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,423
    > wDeck[i].face = wFace[i % FACES];
    > wDeck[i].suit = wSuit[i / FACES];
    wSuit has nothing to do with the number of FACES.

    Probably out of bound accesses.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    627
    Surely there's 24 cards in a euchre deck. Surely.

    BTW, if you feel you need to protect your own parameter values from accidental change, that's your business, but it has nothing to do with the interface (it's an implementation detail), so it shouldn't be in the prototype. I.e., the red const below has nothing to do with the prototype:

    void deal(const Card * const wDeck);
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
     
    #define CARDS 24
    #define FACES  6
     
    typedef struct card {
        const char *face;
        const char *suit;
    } Card;
     
    void fillDeck(Card *deck);
    void shuffle(Card *deck);
    void deal(const Card *deck);
     
    int main(void) {
        Card deck[CARDS];
        srand(time(NULL));
        fillDeck(deck);
        shuffle(deck);
        deal(deck);
    }
     
    void fillDeck(Card *deck) {
        // The string literals here are available globally (no need for 'static').
        const char *face[] = {"Ace", "Nine", "Ten", "Jack", "Queen", "King"};
        const char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
        for (int i = 0; i < CARDS; i++) {
            deck[i].face = face[i % FACES];
            deck[i].suit = suit[i / FACES];
        }
    }
     
    void shuffle(Card *d) { // more proper shuffle
        for (int i = CARDS; i > 1;) {
            int j = rand() % i--;
            Card t = d[i]; d[i] = d[j]; d[j] = t;
        }
    }
      
    void deal(const Card *deck) {
        for (int i = 0; i < CARDS; i++)
            printf("%5s of %-8s%s", deck[i].face, deck[i].suit, 
                    (i + 1) % 4 ? "  " : "\n");
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  4. #4
    Registered User
    Join Date
    Oct 2018
    Posts
    11
    Quote Originally Posted by john.c View Post
    Surely there's 24 cards in a euchre deck. Surely.

    BTW, if you feel you need to protect your own parameter values from accidental change, that's your business, but it has nothing to do with the interface (it's an implementation detail), so it shouldn't be in the prototype. I.e., the red const below has nothing to do with the prototype:

    void deal(const Card * const wDeck);
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
     
    #define CARDS 24
    #define FACES  6
     
    typedef struct card {
        const char *face;
        const char *suit;
    } Card;
     
    void fillDeck(Card *deck);
    void shuffle(Card *deck);
    void deal(const Card *deck);
     
    int main(void) {
        Card deck[CARDS];
        srand(time(NULL));
        fillDeck(deck);
        shuffle(deck);
        deal(deck);
    }
     
    void fillDeck(Card *deck) {
        // The string literals here are available globally (no need for 'static').
        const char *face[] = {"Ace", "Nine", "Ten", "Jack", "Queen", "King"};
        const char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
        for (int i = 0; i < CARDS; i++) {
            deck[i].face = face[i % FACES];
            deck[i].suit = suit[i / FACES];
        }
    }
     
    void shuffle(Card *d) { // more proper shuffle
        for (int i = CARDS; i > 1;) {
            int j = rand() % i--;
            Card t = d[i]; d[i] = d[j]; d[j] = t;
        }
    }
      
    void deal(const Card *deck) {
        for (int i = 0; i < CARDS; i++)
            printf("%5s of %-8s%s", deck[i].face, deck[i].suit, 
                    (i + 1) % 4 ? "  " : "\n");
    }

    But how would I make it out put 5 cards in each hand?

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    11
    Quote Originally Posted by john.c View Post
    Surely there's 24 cards in a euchre deck. Surely.

    BTW, if you feel you need to protect your own parameter values from accidental change, that's your business, but it has nothing to do with the interface (it's an implementation detail), so it shouldn't be in the prototype. I.e., the red const below has nothing to do with the prototype:

    void deal(const Card * const wDeck);
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
     
    #define CARDS 24
    #define FACES  6
     
    typedef struct card {
        const char *face;
        const char *suit;
    } Card;
     
    void fillDeck(Card *deck);
    void shuffle(Card *deck);
    void deal(const Card *deck);
     
    int main(void) {
        Card deck[CARDS];
        srand(time(NULL));
        fillDeck(deck);
        shuffle(deck);
        deal(deck);
    }
     
    void fillDeck(Card *deck) {
        // The string literals here are available globally (no need for 'static').
        const char *face[] = {"Ace", "Nine", "Ten", "Jack", "Queen", "King"};
        const char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
        for (int i = 0; i < CARDS; i++) {
            deck[i].face = face[i % FACES];
            deck[i].suit = suit[i / FACES];
        }
    }
     
    void shuffle(Card *d) { // more proper shuffle
        for (int i = CARDS; i > 1;) {
            int j = rand() % i--;
            Card t = d[i]; d[i] = d[j]; d[j] = t;
        }
    }
      
    void deal(const Card *deck) {
        for (int i = 0; i < CARDS; i++)
            printf("%5s of %-8s%s", deck[i].face, deck[i].suit, 
                    (i + 1) % 4 ? "  " : "\n");
    }

    Just curious, why wouldn't #define CARDS 5 work?

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    627
    Just curious, why did you duplicate my code twice for no reason whatsoever? Just enjoy wasting space?

    Anyway, I have no idea what "works" even means. I don't know the game and you haven't given any explanation.

    I have no idea what CARDS is supposed to mean. However, you used it as the limit of the loops that fill and shuffle the deck. So at least in those uses it represents the size of the deck, which is 24, right?

    You never said anything about 5 cards per hand. Your print function seems to want to print the whole deck 4 per line, so that's what I did.

    You want 5 per hand, but what is a "hand"? If it's just the cards printed on the screen, I don't see how you're going to do anything more with them once you've printed them (I guess the deck is still in the same order...).

    Storing the "Card"s as two pointers to strings is pretty useless. Most card games are dependent on the numerical value of the cards (and possibly suits). The strings making up the names of the cards don't really enter into it.

    I would just store the cards as ints and convert with % and / whenever I needed the value or to index an array of names.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing a heart help
    By dubbin240 in forum C Programming
    Replies: 20
    Last Post: 03-04-2011, 12:42 AM
  2. Heart Rate R-to R value
    By biomedC in forum C Programming
    Replies: 12
    Last Post: 11-16-2010, 03:23 PM
  3. M$ phone suit
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 12-26-2002, 05:58 PM
  4. would a class suit?
    By DMaxJ in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2002, 01:16 PM

Tags for this Thread