Code:
// Fig. 5.12
// Card shuffling dealing program
#include <iostream>
#include <conio.h>
using std::cout;
using std::ios;
using std::endl;
#include <iomanip>
using std::setw;
using std::setiosflags;
#include <cstdlib>
#include <ctime>
void shuffle( int [][ 13 ] );
void deal( const int [][ 13 ], const char *[], const char *[], int [][ 2 ] );
void pair( const int [][ 13 ], const int [][ 2 ], const char *[] );
void threeOfKind( const int [][ 13 ], const int [][ 2 ], const char *[] );
void fourOfKind( const int [][ 13 ], const int [][ 2 ], const char *[] );
void flushHand( const int [][ 13 ], const int [][ 2 ], const char *[] );
void straightHand( const int [][ 13 ], const int [][ 2 ], const char *[], const char *[] );
int main()
{
const char *suit[ 4 ] =
{ "Hearts", "Diamonds", "Clubs", "Spades" };
const char *face[ 13 ] =
{ "Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King" };
int deck[ 4 ][ 13 ] = { 0 }, hand[ 5 ][ 2 ] = { 0 };
srand( time( 0 ) );
shuffle( deck );
deal( deck, face, suit, hand );
pair( deck, hand, face );
threeOfKind( deck, hand, face );
fourOfKind( deck, hand, face );
flushHand( deck, hand, suit );
straightHand( deck, hand, suit, face );
return 0;
}
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;
}
}
void deal( const int wDeck[][ 13 ], const char *wFace[],
const char *wSuit[], int wHand[][ 2 ] )
{
int r = 0;
cout << "Your five card hand is:\n \n";
for ( int card = 1; card < 6; ++card )
for ( int row = 0; row <= 3; ++row )
for ( int column = 0; column <= 12; ++column )
if ( wDeck[ row ][ column ] == card ) {
wHand[ r ][ 0 ] = row;
wHand[ r ][ 1 ] = column;
cout << setw( 5 ) << setiosflags( ios::left )
<< wFace[ column ] << " of "
<< setw( 8 ) << setiosflags( ios::left )
<< wSuit[ row ]
<< ( card % 1 == 0 ? '\n' : '\t' );
++r;
}
}
void pair( const int wDeck[][ 13 ], const int wHand[][ 2 ],
const char *wFace[] )
{
int counter[ 13 ] = { 0 };
for ( int r = 0; r < 5; ++r )
++counter[ wHand[ r ][ 1 ] ];
cout << '\n';
for ( int p = 0; p < 13; ++p )
if ( counter[ p ] == 2 )
cout << "Nice job, you have a pair of " << wFace[ p ] << "s.\n";
}
void threeOfKind( const int wDeck[][ 13 ], const int wHand[][ 2 ],
const char *wFace[] )
{
int counter[ 13 ] = { 0 };
for ( int r = 0; r < 5; ++r )
++counter[ wHand[ r ][ 1 ] ];
for ( int t = 0; t < 13; t++ )
if ( counter[ t ] == 3 )
cout << "Sweet! You have three " << wFace[ t ] << "s.\n";
}
void fourOfKind( const int wDeck[][ 13 ], const int wHand[][ 2 ],
const char *wFace[] )
{
int counter[ 13 ] = { 0 };
for ( int r = 0; r < 5; ++r )
++counter[ wHand[ r ][ 1 ] ];
for ( int k = 0; k < 13; ++k )
if ( counter[ k ] == 4 )
cout << "You Rule!!! there is four " << wFace[ k ] << "s.\n";
}
void flushHand( const int wDeck[][ 13 ], const int wHand[][ 2 ],
const char *wSuit[] )
{
int count[ 4 ] = { 0 };
for ( int r = 0; r < 5; ++r )
++count[ wHand[ r ][ 0 ] ];
for ( int f = 0; f < 4; ++f )
if ( count[ f ] == 5 ) cout << "AWESOME!!! You have a flush of " << wSuit[ f ] << "s.\n";
}
void straightHand( const int wDeck[][ 13 ], const int wHand[][ 2 ],
const char *wSuit[], const char *wFace[] )
{
int x[ 5 ] = { 0 }, y;
for ( int r = 0; r < 5; ++r )
x[ r ] = wHand[ r ][ 1 ];
for ( int b = 1; b < 5; ++b )
for ( int c = 0; c < 4; ++c )
if ( x[ c ] > x[ c + 1 ] ) {
y = x[ c ];
x[ c ] = x[ c + 1 ];
x[ c + 1 ] = y;
}
if ( x[ 4 ] - 1 == x[ 3 ] && x[ 3 ] - 1 == x[ 2 ]
&& x[ 2 ] - 1 == x[ 1 ] && x[ 1 ] - 1 == x[ 0 ] ) {
cout << "I bow down to you! You have a straight of\n";
for ( int d = 0; d < 5; ++d )
cout << wFace[ wHand[ d ][ 1 ] ] << " of " << wSuit[ wHand[ d ][ 0 ] ]
<< '\n';
}
}
THANK YOU SO MUCH