Code:
#include <stdio.h>
enum suit_t
{
// Arranged in this order to get black/red/black/red.
SUIT_CLUB=0, // 0 & 1 == 0. Black.
SUIT_HEART=1, // 1 & 1 == 1. Red.
SUIT_SPADE=2, // 2 & 1 == 0. Black.
SUIT_DIAMOND=3 // 3 & 1 == 1. Red.
};
enum card_t
{
CARD_MIN=0, CARD_MAX=52,
// Cards are numbered as 0, 4, 8, 12, etc. The first 2 bits decide the suit.
CARD_ACE=(0<<2), CARD_2=(1<<2),
CARD_3=(2<<2), CARD_4=(3<<2),
CARD_5=(4<<2), CARD_6=(5<<2),
CARD_7=(6<<2), CARD_8=(7<<2),
CARD_9=(8<<2), CARD_10=(9<<2),
CARD_JACK=(10<<2), CARD_QUEEN=(11<<2),
CARD_KING=(12<<2)
};
const char const *cardnames[]={
"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King", NULL };
const char const *colornames[]={ "Black", "Red", NULL };
const char const *suitnames[]={"Clubs", "Hearts", "Spades", "Diamonds", NULL };
#define CARD_NAMESTR(X) (cardnames[(X)>>2])
#define CARD_SUITSTR(X) (suitnames[(X)&SUIT_DIAMOND])
#define CARD_COLORSTR(X) (colornames[((X)&1)])
int main(void)
{
int card;
for(card=CARD_MIN; card < CARD_MAX; card++)
{
printf("card %d is the %s of %s, and is %s\n", card,
CARD_NAMESTR(card), CARD_SUITSTR(card),
CARD_COLORSTR(card));
}
}