Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 21. Bits, Characters, C Strings ... > Example: Card Shuffling and Dealing ...

21.4. Example: Card Shuffling and Dealing Simulation

The card shuffling and dealing program in Figs. 21.221.4 is similar to the one described in Exercise 10.10. This program represents the deck of cards as a vector of structures and uses high-performance shuffling and dealing algorithms.

Fig. 21.2. Header for DeckOfCards class.

 1   // Fig. 21.2: DeckOfCards.h
 2   // Definition of class DeckOfCards that
 3   // represents a deck of playing cards.
 4   #include <string>
 5   #include <vector>
 6   using namespace std;
 7
 8   // Card structure definition
 9   struct Card                 
10   {                           
11      string face;             
12      string suit;             
13   }; // end structure Card    
14
15   // DeckOfCards class definition
16   class DeckOfCards
17   {
18   public:
19      static const int numberOfCards = 52;
20      static const int faces = 13;
21      static const int suits = 4;
22
23      DeckOfCards(); // constructor initializes deck
24      void shuffle(); // shuffles cards in deck
25      void deal() const; // deals cards in deck
26
27   private:
28      vector< Card > deck; // represents deck of cards
29   }; // end class DeckOfCards

					  


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial