1012 Card Shuffling and Dealing Use the functions from Exerc
10.12 (Card Shuffling and Dealing) Use the functions from Exercise 10.11 to write a program that deals two five-card poker hands, ecaluates each hand and determines which is the better hand.
Book: C++ How to Program 7ed ISBN: 978-0-13-611726-1
Solution
#include
#include
#include
#define SUITS 4
#define FACES 13
#define CARDS 52
//prototypes
void shuffle(unsigned int wDeck[][FACES]); //shufling modifies wDeck
void deal(unsigned int wDeck[][FACES], const char *wFace[], const char *wSuit[]); //dealing doesn\'t modify arrays
int main()
{
//initialize suit array
const char *suit[SUITS] = {\"Hearts\", \"Diamonds\", \"Clubs\", \"Spades\"};
//initialize face array
const char *face[FACES] = {\"Ace\", \"Deuce\", \"Three\", \"Four\", \"Five\", \"Six\", \"Seven\", \"Eight\", \"Nine\", \"Ten\", \"Jack\", \"Queen\", \"King\"};
//initialize deck array
unsigned int deck[SUITS][FACES] = {0};
srand(time(NULL)); //seed random-number generator
shuffle(deck);
deal(deck, face, suit);
}
void shuffle(unsigned int wDeck[][FACES])
{
size_t row; //row #
size_t column; //column #
size_t card; //counter
//for each of the cards, choose slot of deck randomly
for (card = 1; card <= CARDS; ++card)
{
//choose new random location until unoccupied slot found
do
{
row = rand() % SUITS;
column = rand() % FACES;
} while(wDeck[row][column] != 0);
//place card # 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[])
{
size_t card; //card counter
size_t row; //row counter
size_t column; //column counter
//deal each of the cards
for(card = 1; card <= CARDS; ++card)
{
for(row = 0; row < SUITS; ++row)
{
for(column = 0; column < FACES; ++column)
{
//if slot contains current card, display card
if (wDeck[row][column] == card)
{
printf(\"%5s of %-8s\ \", wFace[column], wSuit[row], card % 2 == 0 ? \'\ \' : \'\\t\');
}
}
}
}
}


