So I have a poker type program where it should shuffle and deal a deck of cards using arrays of pointers. I have to write a function that uses a pointer(s) to determine if the hand contains an Ace. If I can understand this one, I'd be able to do the rest with no problems. I was told to do what I did with int decks, int dealt, int the_hand, but I keep getting errors. I've been trying everything I could think of, but no luck. Am I even in the right direction?

Also, I read the book, reviewed my teachers notes, and even tried Googling this, but none of them helped. My teacher gave me a hint, but won't say anything else " Use the same parameter list as the deal function".

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4
#define FACES 6
#define CARDS 24
#define THE_HAND 5

// prototypes
void shuffle(unsigned int wDeck[][FACES]); // prototype to shuffle deck
void deal(unsigned int wDeck[][FACES], const char *wFace[],
        const char *wSuit[]); // dealing doesn't modify the arrays
int has_ace(int *the_hand);
int main(void) {
    // initialize deck array
    unsigned int deck[SUITS][FACES] = {0};
    int *decks = malloc(CARDS * sizeof(int));
    int dealt = 0; //number of cards taken from deck
    int *the_hand = NULL; //current hand
    srand(time(NULL)); // seed the RNG
    shuffle(deck); // shuffle the deck
        if (has_ace(the_hand)){
        printf("The hand has Jack\n");
    }
    else{
        printf("The hand has no Jack\n");
    }
    // initialize suit array
    const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};

    // initialize face array
    const char *face[FACES] = {"Ace", "Nine", "Ten","Jack", "Queen", "King"};

    deal(deck, face, suit); // deal the deck
};

int has_ace(int *the_hand){
    for (int i = 0; i < THE_HAND; i++){
        if (the_hand[i] == "ACE"){
            return 1; //true
        }
    }
    return 0; //false
}
// shuffle cards in deck
void shuffle(unsigned int wDeck[][FACES]) {
    // for each of the cards, choose slot of deck randomly
    for (size_t card = 20; card <= CARDS; card++) {
        size_t row;
        size_t column;

        // choose new random location until unoccupied slot found
        do {
            row = rand() % SUITS;
            column = rand() % FACES;
        } while (wDeck[row][column] != 0);

        // place card number in chosen slot of deck
    wDeck[row][column] = card;
    }
}

// deal cards in deck
void deal(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[]) {
    // deal each of the cards
for (size_t card = 20; card <= CARDS; card++) {
    // loop through rows of wDeck
    for (size_t row = 0; row < SUITS; row++) {
        // loop through columns of wDeck for current row
        for (size_t column = 0; column < FACES; column++) {
            // if slot contains current card, display card
            if (wDeck[row][column] == card) {
                printf("%5s of %-8s%c", 
                        wFace[column], wSuit[row],
                        card % 2 == 0 ? '\n' : '\t');
                } // end if
            } // end column for loop
        } // end row for lop
    } // end card for loop
} // end deal function

Things I've tried:

int has_ace(int *the_hand){
for (int i = 0; i < THE_HAND; i++){
    if (the_hand[i] == "Ace"){
        return 1; //true
        }
    }
    return 0; //false
}
   int has_ace(int *the_hand){
    for (int i = 0; i < THE_HAND; i++){
    if (the_hand[i] == wDeck[Ace][FACES]){
        return 1; //true
            }
        }
        return 0; //false
}
    int has_ace(int *the_hand){
    for (int i = 0; i < THE_HAND; i++){
    if (the_hand[i] == wFace[Ace]){
        return 1; //true
            }
        }
        return 0; //false
}

Errors I get:

/home/zombieknight93/Desktop/School/Unix/Programs/hw5/card_shuffle_deal.c: In function ‘has_ace’:
/home/zombieknight93/Desktop/School/Unix/Programs/hw5/card_shuffle_deal.c:48:25: warning: comparison between pointer and integer if (the_hand[i] == "Ace"){

Recommended Answers

All 12 Replies

I can't edit again for some reason, but I changed my code to this, but still no luck.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SUITS 4
#define FACES 6
#define CARDS 24

// prototypes
void shuffle(unsigned int wDeck[][FACES]); // shuffling modifies wDeck
void deal(unsigned int wDeck[][FACES], const char *wFace[],
        const char *wSuit[]); // dealing doesn't modify the arrays
void ace(unsigned int wDeck[][FACES], const char *wFace[],
        const char *wSuit[]);
int main(void) {
    // initialize deck array
    unsigned int deck[SUITS][FACES] = {0};

    srand(time(NULL)); // seed the RNG
    shuffle(deck); // shuffle the deck

    // initialize suit array
    const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};

    // initialize face array
    const char *face[FACES] = {"Ace", "Nine", "Ten","Jack", "Queen", "King"};

    deal(deck, face, suit); // deal the deck
}

// shuffle cards in deck
void shuffle(unsigned int wDeck[][FACES]) {
    // for each of the cards, choose slot of deck randomly
    for (size_t card = 20; card <= CARDS; card++) {
        size_t row;
        size_t column;

        // choose new random location until unoccupied slot found
        do {
            row = rand() % SUITS;
            column = rand() % FACES;
        } while (wDeck[row][column] != 0);

        // place card number in chosen slot of deck
        wDeck[row][column] = card;
    }
}

// deal cards in deck
void deal(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[]) {
    // deal each of the cards
    for (size_t card = 20; card <= CARDS; card++) {
        // loop through rows of wDeck
        for (size_t row = 0; row < SUITS; row++) {
            // loop through columns of wDeck for current row
            for (size_t column = 0; column < FACES; column++) {
                // if slot contains current card, display card
                if (wDeck[row][column] == card) {
                    printf("%5s of %-8s%c", 
                            wFace[column], wSuit[row],
                            card % 2 == 0 ? '\n' : '\t');
                } // end if
            } // end column for loop
        } // end row for lop
    } // end card for loop
} // end deal function

// deal cards in deck
  void ace(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[]){
for (size_t card = 20; card <= CARDS; card++) {
    // loop through rows of wDeck
    for (char face = 0; face < FACES; face++) {
        // loop through columns of wDeck for current row
        for (char suit = 0; suit < SUITS; suit++) {
            // if slot contains current card, display card
            if (wDeck[face][suit] == "ace") {
                printf("A %5s of %-8s%c is in your hand", 
                        wFace[face], wSuit[suit],
                        card % 2 == 0 ? '\n' : '\t');
                } // end if
             } // end column for loop
        } // end row for lop
    } // end card for loop
} // end deal function

What error does the second code give you?
In the first code, the error is telling you exactly what is wrong. You are comparing a pointer to a string. Obviously this won't work. A pointer is a reference to a memory location you can't compare it to a string. Even if you dereference it, it refers to an int you can't compare that to a string.

On a side not, your function should return bool instead of int. This is more intuitive to any one using the code.

It gives this error

/home/name/Desktop/School/Unix/Programs/hw5/draper_card_shuffle_deal_modified.c: In function ‘ace’:
/home/name/Desktop/School/Unix/Programs/hw5/draper_card_shuffle_deal_modified.c:81:27: warning: comparison between pointer and integer
if (wDeck[face][suit] == "Ace") {

And I understand the comparison error, but it's just fusterating because I can't figure out another way to do it. I see you said return bool. Do you mean assign boolean true for Ace?

Since you have an array of integers, you have to figure out what those integers represent. Are they the index to a specific card in the deck? Do they represent the index of different array? When you get the answers to these questions you should be on your way to making the proper comparision.

Another way to look at it. You need to compare strings. So what array(s) will give you strings?

If the comparison is true return true otherwise return false;

On the array
Row section: Hearts = 1, Diamonds = 2, Clubs = 3, Spades = 4
Column section: A = 1, Nine = 2, Ten = 3, Jack = 4, Queen = 5, King = 6

So I need to somehow find an array index with [4][ ] meaning Jack and suit. Is that what you mean?

Yes. You have to map the index of the card to the appropriate indeces in those arrays.

I would suggest having the deck a one dimension array of int. This can hold the card values from 0-23. When you shuffle this, each index will hold a random card value. Use a counter to keep track of how many cards have been dealt.

The hand array can hold the card values handed to it from the deck.

The suit will be the card value / 6.

The face will be the card value % 6.

These values will then correspond to the appropriate indeces in your arrays.

e.g. - If the card value 13, the suit is 2("Clubs") and the face is 2("Ten")(Remember arrays are 0 indexed)

This is what I did before I saw your reply, I think it's the same concept.

So, since arrays typically go from 0-??????? I set the column = 0 since that would be Ace

/***************************************************************************
 * Filename: card_shuffle_deal.c
 * Author: Kate Bowers
 * Description: Shuffles and deals deck of cards using arrays of pointers
 * ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define SUITS 4
#define FACES 6
#define CARDS 24

// prototypes
void shuffle(unsigned int wDeck[][FACES]); // shuffling modifies wDeck
void deal(unsigned int wDeck[][FACES], const char *wFace[],
        const char *wSuit[]); // dealing doesn't modify the arrays
void one_ace(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[]) ;
int main(void) {
    // initialize deck array
    unsigned int deck[SUITS][FACES] = {0};

    srand(time(NULL)); // seed the RNG
    shuffle(deck); // shuffle the deck

    // initialize suit array
    const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};

    // initialize face array
    const char *face[FACES] = {"Ace", "Nine", "Ten","Jack", "Queen", "King"};

    deal(deck, face, suit); // deal the deck
    one_ace(deck, face, suit);
}

// shuffle cards in deck
void shuffle(unsigned int wDeck[][FACES]) {
    // for each of the cards, choose slot of deck randomly
    for (size_t card = 20; card <= CARDS; card++) {
        size_t row;
        size_t column;

        // choose new random location until unoccupied slot found
        do {
            row = rand() % SUITS;
            column = rand() % FACES;
        } while (wDeck[row][column] != 0);

        // place card number in chosen slot of deck
        wDeck[row][column] = card;
    }
}

// deal cards in deck
void deal(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[]) {
    // deal each of the cards
    for (size_t card = 1; card <= CARDS; card++) {
        // loop through rows of wDeck
        for (size_t row = 0; row < SUITS; row++) {
            // loop through columns of wDeck for current row
            for (size_t column = 0; column < FACES; column++) {
                // if slot contains current card, display card

         // end if
                if (wDeck[row][column] == card) {
                    printf("%5s of %-8s%c", 
                            wFace[column], wSuit[row],
                            card % 2 == 0 ? '\n' : '\t');

        } // end if
            } // end column for loop
        } // end row for lop
    } // end card for loop
} // end deal function

void one_ace(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[]) {
    // find the jack in the hand
    for (size_t card = 1; card <= CARDS; card++) {
        // loop through rows of wDeck
        for (size_t row = 0; row < SUITS; row++) {
            // loop through columns of wDeck for current row
            for (size_t column = 0; column ==0 && column < 1; column++) {
                // if slot contains current card, display card
                if(wDeck[row][column] == card){
                        printf("An %4s has been found!\n", 
                                wFace[column]);
                } // end if     
            } // end column for loop
        } // end row for loop
    } // end card for lop
} // end jack function

However unless I'm doing something wrong it gives multiple outputs. When I just want it to tell me one time an Ace has been found, not say "An Ace has been found!" the number of times one appears.

Nine of Spades
Ace of Hearts
Jack of Diamonds
Nine of Diamonds
Ace of Clubs
A Ace has been found!
A Ace has been found!

Actually you're still using a 2 dimension array for the deck. Your function is looping through the deck, not a hand.

So should I make a hand function then?

You should probably modify the function to use a hand instead of a deck.

Something like this?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#define SUITS 4
#define FACES 6
#define CARDS 24
#define HAND 5
// prototypes
void shuffle(unsigned int wHand[][FACES]); // shuffling modifies wDeck
void deal(unsigned int wHand[][FACES], const char *wFace[],
        const char *wSuit[]); // dealing doesn't modify the arrays
void one_jack(unsigned int wHand[][FACES], const char *wFace[], const char *wSuit[]) ;
int main(void) {
    // initialize deck array
    //unsigned int deck[SUITS][FACES] = {0};
    unsigned int hand[SUITS][FACES] = {0};

    srand(time(NULL)); // seed the RNG
    shuffle(hand); // shuffle the deck

    // initialize suit array
    const char *suit[SUITS] = {"Hearts", "Diamonds", "Clubs", "Spades"};

    // initialize face array
    const char *face[FACES] = {"Ace", "Nine", "Ten","Jack", "Queen", "King"};

    deal(hand, face, suit); // deal the deck
    one_jack(hand, face, suit); // looks for Jack in hand

}

// shuffle cards in deck
void shuffle(unsigned int wHand[][FACES]) {
    // for each of the cards, choose slot of deck randomly
    for (size_t card = 20; card <= CARDS; card++) {
        size_t row;
        size_t column;

        // choose new random location until unoccupied slot found
        do {
            row = rand() % SUITS;
            column = rand() % FACES;
        } while (wHand[row][column] != 0);

        // place card number in chosen slot of deck
        wHand[row][column] = card;
    }
}

// deal cards in deck
void deal(unsigned int wHand[][FACES], const char *wFace[], const char *wSuit[]) {
    // deal each of the cards
    for (size_t card = 20; card <= CARDS; card++) {
        // loop through rows of wDeck
        for (size_t row = 0; row < SUITS; row++) {
            // loop through columns of wDeck for current row
            for (size_t column = 0; column < FACES; column++) {
                // if slot contains current card, display card

         // end if
                if (wHand[row][column] == card) {
                    printf("%5s of %-8s%c", 
                            wFace[column], wSuit[row],
                            card % 2 == 0 ? '\n' : '\t');

        } // end if
            } // end column for loop
        } // end row for lop
    } // end card for loop
} // end deal function

void one_jack(unsigned int wHand[][FACES], const char *wFace[], const char *wSuit[]) {
    // find the jack in the hand
    //for (size_t card = 2; card <= CARDS; card++) {
        // loop through rows of wDeck
        for (size_t hand = 0; hand < HAND; hand++) {
        for (size_t row = 0; row < SUITS; row++) {
            // loop through columns of wDeck for current row
            for (size_t column = 3; column > 2 && column < 4; column++) {
                // if slot contains current card, display card
                if(wHand[row][column] == hand){
                        printf("A %4s has been found!\n", 
                                wFace[column]);
                } // end if     
            } // end column for loop
        } // end row for loop
    } // end card for lop
} // end jack function

if it works it doesn't look too bad. But you should probably return after it has found an ace.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.