/*
* cards_2.c
* This program picks up five cards and prints
them.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* The value of a card: A, 2-10, J, Q and K: */
enum _Value
{
ACE = 1,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING
};
/* The possible suits. */
enum _Suit
{
HEARTS,
SPADES,
DIAMONDS,
CLUBS,
N_SUITS
};
typedef enum
_Value Value;
typedef enum
_Suit Suit;
/* A data
structure for a card. */
struct _Card
{
Value value;
Suit suit;
};
typedef struct
_Card Card;
#define N_CARDS 5
int main ()
{
Card
cards[N_CARDS];
const
char *suit_names[N_SUITS] = {"hearts", "spades",
"diamonds", "clubs"};
const
char *name;
int is_unique;
int i, j;
/* In order to get different cards each time: */
srand (time (NULL));
for
(i = 0; i < N_CARDS; i++)
{
/* Pick up the
current card. */
do
{
cards[i].value =
(Value)(1 + rand() % KING);
cards[i].suit =
(Suit)(rand() % N_SUITS);
/* Make sure
the card is unique. */
is_unique = 1;
for (j = 0; j <
i; j++)
{
if (cards[i].value == cards[j].value
&&
cards[i].suit
== cards[j].suit)
{
/* We already have this card: */
is_unique = 0;
break;
}
}
} while (!
is_unique);
/* Print the
card. */
name =
suit_names[cards[i].suit];
switch
(cards[i].value)
{
case
ACE:
printf ("A of %s", name);
break;
case
JACK:
printf ("J of %s", name);
break;
case
QUEEN:
printf ("Q of %s", name);
break;
case
KING:
printf ("K of %s", name);
break;
default:
printf ("%d of %s", cards[i].value, name);
}
printf ("\n");
}
return
(0);
}