📄 pc5_11.cpp
字号:
// Chapter 5 of C++ How to Program
// Programming Challenge 11
#include <iostream>
using std::cout;
using std::left;
#include <iomanip>
using std::setw;
using std::setprecision;
#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[] = { "Hearts", "Diamonds",
"Clubs", "Spades" };
const char *face[] = { "Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen",
"King" };
int deck[ 4 ][ 13 ] = { 0 };
int 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;
} // end main
// shuffle deck
void shuffle( int wDeck[][ 13 ] )
{
int row;
int column;
for ( int card = 1; card <= 52; ++card ) {
do {
row = rand() % 4;
column = rand() % 13;
} while ( wDeck[ row ][ column ] != 0 );
wDeck[ row ][ column ] = card;
} // end for
} // end function shuffle
// deal a five-card poker hand
void deal( const int wDeck[][ 13 ], const char *wFace[],
const char *wSuit[], int wHand[][ 2 ] )
{
int r = 0;
cout << "The hand is:\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 << left << setw( 5 ) << wFace[ column ]
<< " of " << setw( 8 )
<< wSuit[ row ]
<< ( card % 2 == 0 ? '\n' : '\t' );
++r;
} // end if
cout << '\n';
} // end function deal
// pair determines if the hand contains one or two pair
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 << "The hand contains a pair of " << wFace[ p ]
<< "'s.\n";
} // end function pair
// determines if the hand contains three of a kind
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 << "The hand contains three " << wFace[ t ]
<< "'s.\n";
} // end function threeOfKind
// determines if the hand contains four of a kind
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 << "The hand contains four " << wFace[ k ]
<< "'s.\n";
} // end function fourOfKind
// determines if the hand contains a flush
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 << "The hand contains a flush of " << wSuit[ f ]
<< "'s.\n";
} // end function flush
// determines if the hand contains a straight
void straightHand( const int wDeck[][ 13 ],
const int wHand[][ 2 ],
const char *wSuit[],
const char *wFace[] )
{
int s[ 5 ] = { 0 };
int temp;
// copy column locations to sort
for ( int r = 0; r < 5; ++r )
s[ r ] = wHand[ r ][ 1 ];
// bubble sort column locations
for ( int pass = 1; pass < 5; ++pass )
for ( int comp = 0; comp < 4; ++comp )
if ( s[ comp ] > s[ comp + 1 ] ) {
temp = s[ comp ];
s[ comp ] = s[ comp + 1 ];
s[ comp + 1 ] = temp;
} // end if
// check if sorted columns are a straight
if ( s[ 4 ] - 1 == s[ 3 ] && s[ 3 ] - 1 == s[ 2 ]
&& s[ 2 ] - 1 == s[ 1 ] && s[ 1 ] - 1 == s[ 0 ] ) {
cout << "The hand contains a straight consisting of\n";
for ( int j = 0; j < 5; ++j )
cout << wFace[ wHand[ j ][ 1 ] ] << " of " << wSuit[ wHand[ j ][ 0 ] ]
<< '\n';
} // end if
} // end function straightHand
/**************************************************************************
* (C) Copyright 1992-2003 by Deitel & Associates, Inc. and Prentice *
* Hall. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
*************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -