Thread: Trying to Model a Deck of Cards.

  1. #1
    Registered User
    Join Date
    Nov 2011

    Post Trying to Model a Deck of Cards.


    I'm working on an assignment that asks us to model a deck of cards by creating a map in a labeling scheme with the numbers 0-53 (including the jokers). I'm a little lost on how to do this.. I've looked through our text books and looked for hints but, I have not found anything. I have also looked through lecture notes as well and I can't unfortunately figure out how to accomplish this.
    The hints that we are offered are:

    2c -> 0, 2d -> 1, 2h -> 2, 2s -> 3, and etc, etc until Ac -> 48, Ad->49, Ah-> 50, As->51. .

    What I am thinking is adding a structure for the clubs, diamonds, hearts, and spades but, again I don't know if this is the right direction i need to be headed in. If anyone can show me the right direction or links to resources that can help me accomplish this. I would be most appreciated. Thanks.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    You can represent a deck of cards with just an array of ints. There are no structs needed. What your instructor is saying is the following integers can represent the following cards:

    0 - 2c
    1 - 2d
    2 - 2h
    3 - 2s
    4 - 3c
    5 - 3d
    6 - 3h
    7 - 3s

    So let's say you have int card that has a value somewhere in the range of 0 to 51. To find the face value of the card you just evaluate card / 4 and to find the suit you evaluate card % 4. If card is greater than 51 then you have a joker.

    Once you have that, you just need arrays of ranks and suits:
    char *ranks[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
    char suits[] = { 'c', 'd', 'h', 's' };
    To print a card you could just printf("%s%c", ranks[card / 4], suits[card % 4])
    Last edited by itsme86; 11-15-2011 at 04:10 PM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2011

    I'm not sure I understand exactly what you mean regarding "mapping" but I think an enum type might be useful for you. If you're trying to produce a unique numeric value for each card, you could simply create an enum for card (starting with one for ace) and one for suit and simply multiply the values together. i.e.

    enum card_value {ace = 1, two, three, four...};
    enum suit {clubs = 1, heart, diamonds...};
    int card[2];
    card[0] = ace;
    card[1] = heart;
    int card_value = ace*heart;
    Article regarding enum:

    C Enumeration Declarations

  4. #4
    Join Date
    Aug 2010
    Ontario Canada
    Hi Dan...
    I've always found the easiest way to model a deck of cards is as a single numerical array.

    Essentially you assign the array values from 0 to 51 then shuffle it.
    The suit of any given card (array value) is the number / 13, which in a deck numbered 0 to 51, returns 0 to 3.
    The face value of the card is the number % 13, which returns 0 to 12 to represent the card's face value.

    To print these out, you use constant arrays, one for the name of the face value, one for the names of the suits.

    Here is a little demonstration piece to show you the concepts... DO NOT simply copy this code. Study it, learn how it works, then write your own version of it... You don't learn anything by taking the easy way out, plus teachers do check here so you'd probably just get nailed for cheating!

    // deck of cards demonstration
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define CARDS 52
    const char *face[] = {"Ace","Two","Three","Four","Five","Six","Seven",
                        "Eight","Nine","Ten","Jack","Queen","King" };
    const char *suit[] = {"Spades","Hearts","Clubs","Diamonds"};
    // function to shuffle the deck
    int Shuffle(int *deck)
        int i, temp, card; 
        for (i = CARDS - 1; i > 0; i--)
            card = rand() % i;
            temp = deck[i];
            deck[i] = deck[card];
            deck[card] = temp;
        return 0;
    // program entry point
    int main (void)
        int deck[CARDS];  // deck
        int card = 53;    // card
        int fac, sut;     // face and suit
        int i;            // counter
        char ch = 0;      // keyboard
        printf("The card dealer....\n\n); 
        //initialize the deck
        for (i = 0; i < CARDS; i++)
          deck[i] = i;
        // deal cards  
            if ( card >= CARDS )  
              card = Shuffle(deck);
            // face and suit
            fac = deck[card] % 13;
            sut = deck[card] / 13;
            // display the card
            printf("%2d > %s of %s\t\t",card,face[fac],suit[sut]);
            printf("(Enter to deal, Q to quit)\n");
            ch = getchar();
        while( toupper(ch) != 'Q');
        return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to make a deck of cards.
    By Jesse20ghet in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2011, 03:44 PM
  2. Shuffling a deck?
    By Neo1 in forum C++ Programming
    Replies: 23
    Last Post: 07-16-2007, 01:21 AM
  3. Deck Shuffle
    By pjharris in forum C++ Programming
    Replies: 51
    Last Post: 01-06-2006, 04:59 PM
  4. FF8 Cardgame Deck class
    By IanelarAzure in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2004, 04:00 PM
  5. filling a deck of cards
    By lakai02 in forum C Programming
    Replies: 6
    Last Post: 12-07-2002, 10:13 AM