(Card Shufflling and Dealing) Modify Fig. 22.14 to shuffle the cards using a high-perfor- mance shuffle, as shown in Fig. 22.3. Print the resulting deck in two-column format. Precede each card with its color.

What will be an ideal response?

```
// Definition of class DeckOfCards that
// represents a deck of playing cards.

// BitCard structure definition with bit fields
struct BitCard
{
unsigned face : 4; // 4 bits; 0-15
unsigned suit : 2; // 2 bits; 0-3
unsigned color : 1; // 1 bit; 0-1
}; // end struct BitCard

// DeckOfCards class definition
class DeckOfCards
{
public:
DeckOfCards(); // constructor initializes deck
void shuffle(); // shuffle cards in deck
void deal(); // deals cards in deck
private:
BitCard deck[ 52 ]; // represents deck of cards
}; // end class DeckOfCards
```
```
// Member-function definitions for class DeckOfCards that simulates
// the shuffling and dealing of a deck of playing cards.
#include
#include
#include
using namespace std;

#include "DeckOfCards.h" // DeckOfCards class definition

// no-argument DeckOfCards constructor intializes deck
DeckOfCards::DeckOfCards()
{
for ( int i = 0; i <= 51; i++ )
{
deck[ i ].face = i % 13; // faces in order
deck[ i ].suit = i / 13; // suits in order
deck[ i ].color = i / 26; // colors in order
} // end for
} // end no-argument DeckOfCards constructor

// shuffle cards in deck
void DeckOfCards::shuffle()
{
// shuffle cards randomly
for ( int i = 0; i < 52; i++ )
{
int j = rand() % 52;
BitCard temp = deck[ i ];
deck[ i ] = deck[ j ];
deck[ j ] = temp;
} // end for
} // end function shuffle

// deal cards in deck
void DeckOfCards::deal()
{
char *face[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
char *color[] = { "Red", "Black" };

for ( int i = 0; i < 52; i++ )
{
cout << right << setw( 5 ) << color[ deck[ i ].color ] << ": "
<< setw( 8 ) << face[ deck[ i ].face ] << " of "
<< left << setw( 8 ) << suit[ deck[ i ].suit ]
<< ( ( i + 1 ) % 2 ? '\t' : '\n' );
} // end for
} // end function deal
```
```
// Card shuffling and dealing program.
#include "DeckOfCards.h" // DeckOfCards class definition

int main()
{
DeckOfCards deckOfCards; // create DeckOfCards object
deckOfCards.shuffle(); // shuffle the cards in the deck
deckOfCards.deal(); // deal the cards in the deck
return 0; // indicates successful termination
} // end main
```
Black: Three of Spades Red: Nine of Hearts
Red: Ten of Diamonds Red: Ten of Hearts
Red: Five of Diamonds Red: Eight of Diamonds
Red: King of Diamonds Black: Five of Clubs
Black: Ace of Clubs Red: Five of Hearts
Red: Seven of Diamonds Red: Ace of Diamonds
Red: Jack of Hearts Black: Six of Clubs
Red: King of Hearts Black: Queen of Spades
Red: Queen of Hearts Black: Ten of Clubs
Red: Deuce of Diamonds Black: Three of Clubs
Black: Nine of Spades Black: Six of Spades
Black: Four of Spades Black: Jack of Spades
Red: Four of Hearts Black: King of Clubs
Red: Eight of Hearts Black: Ten of Spades
Black: Queen of Clubs Red: Four of Diamonds
Black: Ace of Spades Red: Seven of Hearts
Black: Deuce of Spades Black: Eight of Spades
Red: Three of Diamonds Black: Deuce of Clubs
Red: Nine of Diamonds Red: Deuce of Hearts
Black: Jack of Clubs Black: Four of Clubs
Black: King of Spades Red: Six of Hearts
Black: Five of Spades Black: Nine of Clubs
Black: Seven of Spades Red: Three of Hearts
Red: Queen of Diamonds Red: Jack of Diamonds
Red: Six of Diamonds Black: Eight of Clubs
Red: Ace of Hearts Black: Seven of Clubs

Computer Science & Information Technology

You might also like to view...

Screen capture software requires a webcam to be able to capture a video of your screen

Indicate whether the statement is true or false

Computer Science & Information Technology

A font is a set of characters with a similar appearance, size, and color.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology