// Card shuffling dealing program #include #include #include // this is needed for srand using namespace std; void shuffle( int [][ 13 ] ); // prototypes void deal( const int [][ 13 ], const char *[], const char *[] ); int main() { const char *suit[ 4 ] = { "Spades", "Hearts", "Diamonds", "Clubs", }; const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; int deck[ 4 ][ 13 ] = { 0 }; // initialize all elements to zero srand( time( 0 ) ); // seed the rand function shuffle( deck ); deal( deck, face, suit ); // the three arrays are the arguments char ch; // this is just to slow down the output cin >>ch; return 0; } /* Determines the order of the cards in the deck. If the contents * of the 2-D array is zero, the card has not yet been put in the deck. */ void shuffle( int wDeck[][ 13 ] ) { int row, column; for ( int card = 1; card <= 52; card++ ) { do { row = rand() % 4; column = rand() % 13; } while( wDeck[ row ][ column ] != 0 ); wDeck[ row ][ column ] = card; } } /* This function outputs all the cards in the deck. The order of * of the cards in the deck is the contents of the array, the card * itself is determined by its position in the array */ void deal( const int wDeck[][ 13 ], const char *wFace[], const char *wSuit[] ) { for ( int card = 1; card <= 52; card++ ) // deal entire deck for ( int row = 0; row <= 3; row++ ) for ( int column = 0; column <= 12; column++ ) if ( wDeck[ row ][ column ] == card ) cout << setw( 5 ) << setiosflags( ios::right ) << wFace[ column ] << " of " << setw( 8 ) << setiosflags( ios::left ) << wSuit[ row ] << ( card % 2 == 0 ? '\n' : '\t' ); }