The card shuffling and dealing program in Figs. 20.2–20.4 represents the deck of cards as an array
of structures.
1 // Fig. 20.2: DeckOfCards.h
2 // Definition of class DeckOfCards that
3 // represents a deck of playing cards.
4 #include <string>
5 #include <array>
6
7 // Card structure definition
8 struct Card
9 {
10 std::string face;
11 std::string suit;
12 }; // end structure Card
13
14 // DeckOfCards class definition
15 class DeckOfCards
16 {
17 public:
18 static const int numberOfCards = 52;
19 static const int faces = 13;
20 static const int suits = 4;
21
22 DeckOfCards(); // constructor initializes deck
23 void shuffle(); // shuffles cards in deck
24 void deal() const; // deals cards in deck
25
26 private:
27 std::array< Card, numberOfCards > deck; // represents deck of cards
28 }; // end class DeckOfCards
1 // Fig. 20.3: DeckOfCards.cpp
2 // Member-function definitions for class DeckOfCards that simulates
3 // the shuffling and dealing of a deck of playing cards.
4 #include <iostream>
5 #include <iomanip>
6 #include <cstdlib> // prototypes for rand and srand
7 #include <ctime> // prototype for time
8 #include "DeckOfCards.h" // DeckOfCards class definition
9 using namespace std;
10
11 // no-argument DeckOfCards constructor intializes deck
12 DeckOfCards::DeckOfCards()
13 {
14 // initialize suit array
15 static string suit[ suits ] =
16 { "Hearts", "Diamonds", "Clubs", "Spades" };
17
18 // initialize face array
19 static string face[ faces ] =
20 { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven",
21 "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
22
23 // set values for deck of 52 Cards
24 for ( size_t i = 0; i < deck.size(); ++i )
25 {
26 deck[ i ].face = face[ i % faces ];
27 deck[ i ].suit = suit[ i / faces ];
28 } // end for
29
30 srand( static_cast< size_t >( time( nullptr ) ) ); // seed
31 } // end no-argument DeckOfCards constructor
32
33 // shuffle cards in deck
34 void DeckOfCards::shuffle()
35 {
36 // shuffle cards randomly
37 for ( size_t i = 0; i < deck.size(); ++i )
38 {
39 int j = rand() % numberOfCards;
40 Card temp = deck[ i ];
41 deck[ i ] = deck[ j ];
42 deck[ j ] = temp;
43 } // end for
44 } // end function shuffle
45
46 // deal cards in deck
47 void DeckOfCards::deal() const
48 {
49 // display each card's face and suit
50 for ( size_t i = 0; i < deck.size(); ++i )
51 cout << right << setw( 5 ) << deck[ i ].face << " of "
52 << left << setw( 8 ) << deck[ i ].suit
53 << ( ( i + 1 ) % 2 ? ' ' : '
' );
54 } // end function deal
1 // Fig. 20.4: fig20_04.cpp
2 // Card shuffling and dealing program.
3 #include "DeckOfCards.h" // DeckOfCards class definition
4
5 int main()
6 {
7 DeckOfCards deckOfCards; // create DeckOfCards object
8 deckOfCards.shuffle(); // shuffle the cards in the deck
9 deckOfCards.deal(); // deal the cards in the deck
10 } // end main
King of Clubs Ten of Diamonds
Five of Diamonds Jack of Clubs
Seven of Spades Five of Clubs
Three of Spades King of Hearts
Ten of Clubs Eight of Spades
Eight of Hearts Six of Hearts
Nine of Diamonds Nine of Clubs
Three of Diamonds Queen of Hearts
Six of Clubs Seven of Hearts
Seven of Diamonds Jack of Diamonds
Jack of Spades King of Diamonds
Deuce of Diamonds Four of Clubs
Three of Clubs Five of Hearts
Eight of Clubs Ace of Hearts
Deuce of Spades Ace of Clubs
Ten of Spades Eight of Diamonds
Ten of Hearts Six of Spades
Queen of Diamonds Nine of Hearts
Seven of Clubs Queen of Clubs
Deuce of Clubs Queen of Spades
Three of Hearts Five of Spades
Deuce of Hearts Jack of Hearts
Four of Hearts Ace of Diamonds
Nine of Spades Four of Diamonds
Ace of Spades Six of Diamonds
Four of Spades King of Spades
The constructor (lines 12–31 of Fig. 20.3) initializes the array
in order with character strings representing Ace through King of each suit. Function shuffle
implements the shuffling algorithm. The function loops through all 52 cards (subscripts 0 to 51). For each card, a number between 0 and 51 is picked randomly. Next, the current Card
and the randomly selected Card
are swapped in the array
. A total of 52 swaps are made in a single pass of the entire array
, and the array
is shuffled. Because the Card
structures were swapped in place in the array
, the dealing algorithm implemented in function deal
requires only one pass of the array
to deal the shuffled cards.