22.4 Example: Card Shuffling and Dealing Simulation

The card shuffling and dealing program in Figs. 22.222.4 is similar to the one described in Exercise 9.23. This program represents the deck of cards as an array of structures.

Fig. 22.2 Definition of class DeckOfCards that represents a deck of playing cards.

Alternate View

 1  // Fig. 22.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     std::string face;        
10     std::string suit;        
11  };                          
12
13  // DeckOfCards class definition
14  class DeckOfCards {
15  public:
16     static const int numberOfCards{52};
17     static const int faces{13};
18     static const int suits{4};
19
20     DeckOfCards(); // constructor initializes deck
21     void shuffle(); // shuffles cards in deck
22     void deal() const; // deals cards in deck
23
24  private:
25     std::array<Card, numberOfCards> deck; // represents deck of cards
26  };

The constructor (lines 12–27 of Fig. 22.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.

Fig. 22.3 Member-function definitions for class DeckOfCards.

Alternate View

 1  // Fig. 22.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     // initialize suit array
14     static string suit[suits]{"Hearts", "Diamonds", "Clubs", "Spades"};
15
16     // initialize face array
17     static string face[faces]{"Ace", "Deuce", "Three", "Four", "Five",
18        "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
19
20     // set values for deck of 52 Cards
21     for (size_t i{0}; i < deck.size(); ++i) {
22        deck[i].face = face[i % faces];
23        deck[i].suit = suit[i / faces];
24     }
25
26     srand(static_cast< size_t >(time(nullptr))); // seed
27  }
28
29  // shuffle cards in deck
30  void DeckOfCards::shuffle() {
31     // shuffle cards randomly
32     for (size_t i{0}; i < deck.size(); ++i) {
33        int j{rand() % numberOfCards};
34        Card temp = deck[i];
35        deck[i] = deck[j];  
36        deck[j] = temp;     
37     }
38  }
39
40  // deal cards in deck
41  void DeckOfCards::deal() const {
42     // display each card’s face and suit
43     for (size_t i = 0; i < deck.size(); ++i) {
44        cout << right << setw(5) << deck[i].face << " of "
45           << left << setw(8) << deck[i].suit
46           << ((i + 1) % 2 ? '	' : '
');
47     }
48  }

Fig. 22.4 Card shuffling and dealing program.

Alternate View

1  // Fig. 22.4: fig22_04.cpp
2  // Card shuffling and dealing program.
3  #include "DeckOfCards.h" // DeckOfCards class definition
4
5  int main() {
6     DeckOfCards deckOfCards; // create DeckOfCards object
7     deckOfCards.shuffle(); // shuffle the cards in the deck
8     deckOfCards.deal(); // deal the cards in the deck
9  }

 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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset