Card Shuffling and Dealing Simulation - C++ Class

C++ examples for Class:Member Field

Description

Card Shuffling and Dealing Simulation

Demo Code

                                                                                                                                                                                                                                          
#include <string> 
#include <vector> 
#include <iostream> 
#include <iomanip> 
#include <cstdlib> // prototypes for rand and srand 
#include <ctime> // prototype for time 
                                                                                                                                                                                                                                         
using namespace std; 
 struct Card /*from   w  w w .  j a v  a2s.  c  o  m*/
 { 
    string face; 
    string suit; 
 };
                                                                                                                                                                                                                                          
class DeckOfCards 
{ 
public: 
    static const int numberOfCards = 52; 
    static const int faces = 13; 
    static const int suits = 4; 
                                                                                                                                                                                                                                          
    DeckOfCards(); // constructor initializes deck 
    void shuffle(); // shuffles cards in deck 
    void deal() const; // deals cards in deck 
                                                                                                                                                                                                                                          
private: 
    vector< Card > deck; // represents deck of cards 
};
                                                                                                                                                                                                                                          
DeckOfCards::DeckOfCards() : deck( numberOfCards ) 
{ 
    static string suit[ suits ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; 
                                                                                                                                                                                                                                          
   static string face[ faces ] = { "Ace", "Deuce", "Three", "Four" , "Five", "Six", "Seven", 
       "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; 
                                                                                                                                                                                                                                          
   for ( int i = 0; i < numberOfCards; ++i ) { 
       deck[ i ].face = face[ i % faces ]; 
       deck[ i ].suit = suit[ i / faces ]; 
   }
                                                                                                                                                                                                                                          
   srand( time( 0 ) ); // seed random number generator 
}
                                                                                                                                                                                                                                          
void DeckOfCards::shuffle() 
{ 
   for ( int i = 0; i < numberOfCards; ++i ) { 
       int j = rand() % numberOfCards; 
       Card temp = deck[ i ]; 
       deck[ i ] = deck[ j ]; 
       deck[ j ] = temp; 
   }
}
                                                                                                                                                                                                                                         
void DeckOfCards::deal() const 
{ 
   for ( int i = 0; i < numberOfCards; ++i ) 
       cout << right << setw( 5 ) << deck[ i ].face << " of " 
           << left << setw( 8 ) << deck[ i ].suit 
           << ( ( i + 1 ) % 2 ? '\t' : '\n' ); 
}
                                                                                                                                                                                                                                          
int main() 
{ 
   DeckOfCards deckOfCards; // create DeckOfCards object 
   deckOfCards.shuffle(); // shuffle the cards in the deck 
   deckOfCards.deal(); // deal the cards in the deck 
}

Result


Related Tutorials