Deitel & Deitel, C How to Program 6th Edition, Exercise 10.7 (Card Shuffling and Dealing Modification)
// 10.7 (Card Shuffling and Dealing Modification)
#include <stdio.h>
#include <time.h>
typedef struct bitCard { // a structure for deck using smallest memory possible
unsigned face : 4;
unsigned suit : 2;
unsigned color : 1;
} Card;
// func definitions
void shuffle(Card wDeck[]);
void fillDeck(Card * const wDeck);
void deal(const Card * const wDeck);
int main() {
Card deck[52];
srand(time(NULL));
fillDeck(deck);
shuffle(deck);
deal(deck);
printf("\n\n\n");
return 0;
}
void fillDeck( Card * const wDeck) // filling the deck in order
{
int i;
for (i = 0; i <= 51; i++) {
wDeck[i].face = i % 13; // giving a face number between 0-12
wDeck[i].suit = i / 13; // giving a suit number between 0-3
wDeck[i].color = i / 26; // giving a color number between 0-1
}
}
void shuffle( Card wDeck[]) { // Change places of every single card with a random card
Card temp; // variable temp type Card holds the card while switching place with another card
int i, j;
for (i = 0; i < 52; i++) {
j = rand() % 52; // picking a random card
temp = wDeck[i]; // hold
wDeck[i] = wDeck[j]; // switch places
wDeck[j] = temp;
}
}
void deal(const Card * const wDeck) // dealing cards in 2 columns with their names and colors
{
int i;
const char *faceNames[] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
const char *suitNames[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
const char *color[] = { " Red ", "Black" };
for (i = 0; i < 26; i++) { // printing in 2 columns
printf("( %s )%7s of %-9s ( %5s ) %7s of %-9s\n", color[wDeck[i].suit / 2],
faceNames[wDeck[i].face], suitNames[wDeck[i].suit], color[wDeck[i + 26].suit / 2],
faceNames[wDeck[i + 26].face], suitNames[wDeck[i + 26].suit]);
}
}
OUTPUT
( Red ) Two of Hearts ( Black ) Five of Clubs
( Red ) Ten of Hearts ( Black ) Three of Clubs
( Red ) Seven of Hearts ( Red ) Nine of Hearts
( Red ) Six of Diamonds ( Black ) Ten of Clubs
( Red ) Ten of Diamonds ( Black ) Eight of Clubs
( Black ) Jack of Spades ( Black ) Six of Clubs
( Black ) Queen of Clubs ( Red ) Eight of Hearts
( Red ) King of Diamonds ( Black ) Nine of Clubs
( Red ) Nine of Diamonds ( Black ) Jack of Clubs
( Black ) Eight of Spades ( Black ) Ace of Spades
( Red ) Four of Diamonds ( Black ) Six of Spades
( Red ) Queen of Diamonds ( Red ) Ace of Hearts
( Black ) Queen of Spades ( Black ) Five of Spades
( Black ) Ace of Clubs ( Black ) Ten of Spades
( Red ) Six of Hearts ( Red ) Three of Diamonds
( Black ) Four of Spades ( Red ) Jack of Diamonds
( Black ) King of Clubs ( Red ) Seven of Diamonds
( Black ) Seven of Clubs ( Red ) Three of Hearts
( Red ) Five of Hearts ( Black ) Seven of Spades
( Black ) Two of Spades ( Black ) Two of Clubs
( Red ) Five of Diamonds ( Red ) Queen of Hearts
( Red ) Four of Hearts ( Red ) King of Hearts
( Black ) King of Spades ( Black ) Three of Spades
( Red ) Ace of Diamonds ( Red ) Eight of Diamonds
( Black ) Four of Clubs ( Black ) Nine of Spades
( Red ) Jack of Hearts ( Red ) Two of Diamonds
Press any key to continue . . .
#include <stdio.h>
#include <time.h>
typedef struct bitCard { // a structure for deck using smallest memory possible
unsigned face : 4;
unsigned suit : 2;
unsigned color : 1;
} Card;
// func definitions
void shuffle(Card wDeck[]);
void fillDeck(Card * const wDeck);
void deal(const Card * const wDeck);
int main() {
Card deck[52];
srand(time(NULL));
fillDeck(deck);
shuffle(deck);
deal(deck);
printf("\n\n\n");
return 0;
}
void fillDeck( Card * const wDeck) // filling the deck in order
{
int i;
for (i = 0; i <= 51; i++) {
wDeck[i].face = i % 13; // giving a face number between 0-12
wDeck[i].suit = i / 13; // giving a suit number between 0-3
wDeck[i].color = i / 26; // giving a color number between 0-1
}
}
void shuffle( Card wDeck[]) { // Change places of every single card with a random card
Card temp; // variable temp type Card holds the card while switching place with another card
int i, j;
for (i = 0; i < 52; i++) {
j = rand() % 52; // picking a random card
temp = wDeck[i]; // hold
wDeck[i] = wDeck[j]; // switch places
wDeck[j] = temp;
}
}
void deal(const Card * const wDeck) // dealing cards in 2 columns with their names and colors
{
int i;
const char *faceNames[] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
const char *suitNames[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
const char *color[] = { " Red ", "Black" };
for (i = 0; i < 26; i++) { // printing in 2 columns
printf("( %s )%7s of %-9s ( %5s ) %7s of %-9s\n", color[wDeck[i].suit / 2],
faceNames[wDeck[i].face], suitNames[wDeck[i].suit], color[wDeck[i + 26].suit / 2],
faceNames[wDeck[i + 26].face], suitNames[wDeck[i + 26].suit]);
}
}
OUTPUT
( Red ) Two of Hearts ( Black ) Five of Clubs
( Red ) Ten of Hearts ( Black ) Three of Clubs
( Red ) Seven of Hearts ( Red ) Nine of Hearts
( Red ) Six of Diamonds ( Black ) Ten of Clubs
( Red ) Ten of Diamonds ( Black ) Eight of Clubs
( Black ) Jack of Spades ( Black ) Six of Clubs
( Black ) Queen of Clubs ( Red ) Eight of Hearts
( Red ) King of Diamonds ( Black ) Nine of Clubs
( Red ) Nine of Diamonds ( Black ) Jack of Clubs
( Black ) Eight of Spades ( Black ) Ace of Spades
( Red ) Four of Diamonds ( Black ) Six of Spades
( Red ) Queen of Diamonds ( Red ) Ace of Hearts
( Black ) Queen of Spades ( Black ) Five of Spades
( Black ) Ace of Clubs ( Black ) Ten of Spades
( Red ) Six of Hearts ( Red ) Three of Diamonds
( Black ) Four of Spades ( Red ) Jack of Diamonds
( Black ) King of Clubs ( Red ) Seven of Diamonds
( Black ) Seven of Clubs ( Red ) Three of Hearts
( Red ) Five of Hearts ( Black ) Seven of Spades
( Black ) Two of Spades ( Black ) Two of Clubs
( Red ) Five of Diamonds ( Red ) Queen of Hearts
( Red ) Four of Hearts ( Red ) King of Hearts
( Black ) King of Spades ( Black ) Three of Spades
( Red ) Ace of Diamonds ( Red ) Eight of Diamonds
( Black ) Four of Clubs ( Black ) Nine of Spades
( Red ) Jack of Hearts ( Red ) Two of Diamonds
Press any key to continue . . .
Let me know in the comments if this page helped you somehow.
It is nice to know if my work helped someone, thanks :)
Comments
Post a Comment