Question: Modify the card shuffling and dealing program below so the shuffling and dealing operations are performed by the same function shuffleAndDeal. The function should contain
Modify the card shuffling and dealing program below so the shuffling and dealing operations are performed by the same function shuffleAndDeal. The function should contain one nested lopping structure thats similar to function shuffle.
Code in C language.
#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');
}
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
