Thread: Trying to Model a Deck of Cards.

1. Trying to Model a Deck of Cards.

Hi,

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. 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:
Code:
```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])

3. Hi,

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.

Code:
```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. 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!

Code:
```// 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" };

// function to shuffle the deck
int Shuffle(int *deck)
{
int i, temp, card;

printf("\nShuffling...\n\n");
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);

srand(time(NULL));

//initialize the deck
for (i = 0; i < CARDS; i++)
deck[i] = i;

// deal cards
do
{
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]);
card++;

printf("(Enter to deal, Q to quit)\n");
ch = getchar();
}
while( toupper(ch) != 'Q');
return 0;
}```