Example: High-Performance Card Shuffling and Dealing Simulation

Example High Performance Card Shuffling and Dealing Simulation

The program in Figs. 22.222.4 is based on the card shuffling and dealing simulation discussed in Chapter 8. The program represents the deck of cards as an array of structures and uses high-performance shuffling and dealing algorithms.

Figure 22.2. Header file for DeckOfCards class.

1 // Fig. 22.2: DeckOfCards.h 2 // Definition of class DeckOfCards that 3 // represents a deck of playing cards. 4 5 // Card structure definition 6 struct Card 7 { 8 char *face; 9 char *suit; 10 }; // end structure Card 11 12 // DeckOfCards class definition 13 class DeckOfCards 14 { 15 public: 16 DeckOfCards(); // constructor initializes deck 17 void shuffle(); // shuffles cards in deck 18 void deal(); // deals cards in deck 19 20 private: 21 Card deck[ 52 ]; // represents deck of cards 22 }; // end class DeckOfCards

Figure 22.3. Class file for DeckOfCards.

(This item is displayed on pages 1062 - 1064 in the print version)

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 5 using std::cout; 6 using std::left; 7 using std::right; 8 9 #include 10 using std::setw; 11 12 #include // prototypes for rand and srand 13 using std::rand; 14 using std::srand; 15 16 #include // prototype for time 17 using std::time; 18 19 #include "DeckOfCards.h" // DeckOfCards class definition 20 21 // no-argument DeckOfCards constructor intializes deck 22 DeckOfCards::DeckOfCards() 23 { 24 // initialize suit array 25 static char *suit[ 4 ] = 26 { "Hearts", "Diamonds", "Clubs", "Spades" }; 27 28 // initialize face array 29 static char *face[ 13 ] = 30 { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", 31 "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; 32 33 // set values for deck of 52 Cards 34 for ( int i = 0; i < 52; i++ ) 35 { 36 deck[ i ].face = face[ i % 13 ]; 37 deck[ i ].suit = suit[ i / 13 ]; 38 } // end for 39 40 srand( time( 0 ) ); // seed random number generator 41 } // end no-argument DeckOfCards constructor 42 43 // shuffle cards in deck 44 void DeckOfCards::shuffle() 45 { 46 // shuffle cards randomly 47 for ( int i = 0; i < 52; i++ ) 48 { 49 int j = rand() % 52; 50 Card temp = deck[ i ]; 51 deck[ i ] = deck[ j ]; 52 deck[ j ] = temp; 53 } // end for 54 } // end function shuffle 55 56 // deal cards in deck 57 void DeckOfCards::deal() 58 { 59 // display each card's face and suit 60 for ( int i = 0; i < 52; i++ ) 61 cout << right << setw( 5 ) << deck[ i ].face << " of " 62 << left << setw( 8 ) << deck[ i ].suit 63 << ( ( i + 1 ) % 2 ? ' ' : ' ' ); 64 } // end function deal


Figure 22.4. High-performance card shuffling and dealing simulation.

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 { 7 DeckOfCards deckOfCards; // create DeckOfCards object 8 9 deckOfCards.shuffle(); // shuffle the cards in the deck 10 deckOfCards.deal(); // deal the cards in the deck 11 return 0; // indicates successful termination 12 } // 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  

In the program, the constructor initializes the Card array in order with character strings representing the Ace through the King of each suit. Function shuffle is where the high-performance shuffling algorithm is implemented. The function loops through all 52 cards (array subscripts 0 to 51). For each card, a number between 0 and 51 is picked randomly. Next, the current Card structure and the randomly selected Card structure are swapped in the array. A total of 52 swaps are made in a single pass of the entire array, and the array of Card structures is shuffled! Unlike the shuffling algorithm presented in Chapter 8, this algorithm does not suffer from indefinite postponement. Because the Card structures were swapped in place in the array, the high-performance dealing algorithm implemented in function deal requires only one pass of the array to deal the shuffled cards.


Категории