(Card Shuffling and Dealing) Modify the program you developed in Exercise 10.10 so that it deals a five-card poker hand. Then write functions to accomplish each of the following:

a) Determine whether the hand contains a pair.
b) Determine whether the hand contains two pairs.
c) Determine whether the hand contains three of a kind (e.g., three jacks).
d) Determine whether the hand contains four of a kind (e.g., four aces).
e) Determine whether the hand contains a flush (i.e., all five cards of the same suit).
f) Determine whether the hand contains a straight (i.e., five cards of consecutive face values).


```
// Stores and calculates attributes of a hand of cards.
#ifndef HAND_H
#define HAND_H

#include
#include
#include "Card.h"
#include "DeckOfCards.h"
using namespace std;

class Hand
{
public:
// constructor takes 5 cards from Deck
Hand( DeckOfCards &deck );
void print() const; // display hand

// determine if we have the given scoring hand
bool pair() const;
bool twoPair() const;
bool threeOfAKind() const;
bool fourOfAKind() const;
bool flush() const;
bool straight() const;
private:
vector< Card > hand; // our hand
vector< int > faceCount; // number of each face
}; // end class Hand

#endif
```
```
// Stores and calculates attributes of a hand of cards.
#include
#include "Hand.h"
using namespace std;

// constructor takes 5 cards from Deck; it assumes 5 cards are available
Hand::Hand( DeckOfCards &deck )
: faceCount( Card::totalFaces )
{
// get hand from deck
for ( unsigned i = 0; i < 5; ++i )
hand.push_back( deck.dealCard() );

// count number of times each face appears (for later use)
for ( unsigned i = 0; i < hand.size(); ++i )
++faceCount[ hand[ i ].getFace() ];
} // end constructor

// display contents of hand to standard output stream
void Hand::print() const
{
cout << "Hand is:\n";
for ( unsigned i = 0; i < hand.size(); ++i )
cout << hand[ i ].toString() << '\n';
cout << endl;
} // end function print

// determine if hand contains a pair
bool Hand::pair() const
{
for ( unsigned i = 0; i < faceCount.size(); ++i )
if ( faceCount[ i ] == 2 )
return true;

return false;
} // end function pair
// determine if hand contains two pairs
bool Hand::twoPair() const
{
bool foundOne = false; // true if the first pair has been found

for ( unsigned i = 0; i < faceCount.size(); ++i )
{
if ( faceCount[ i ] == 2 && foundOne )
return true; // found both pairs
else if ( faceCount[ i ] == 2 )
foundOne = true; // found the first pair
} // end for

return false; // didn't find both pairs
} // end function twoPair

// determine if hand contains three of a kind
bool Hand::threeOfAKind() const
{
for ( unsigned i = 0; i < faceCount.size(); ++i )
if ( faceCount[ i ] == 3 )
return true;

return false;
} // end function threeOfAKind

// determine if hand contains four of a kind
bool Hand::fourOfAKind() const
{
for ( unsigned i = 0; i < faceCount.size(); ++i )
if ( faceCount[ i ] == 4 )
return true;

return false;
} // end function fourOfAKind

// determine if hand contains a flush
bool Hand::flush() const
{
int suit = hand[ 0 ].getSuit();

// search for any card that has a suit different from the first
for ( unsigned i = 1; i < hand.size(); ++i )
if ( hand[ i ].getSuit() != suit )
return false;

return true;
} // end function flush

// determine if hand contains a straight
bool Hand::straight() const
{
vector< int > tmp = faceCount; // make a temporary copy of face count
tmp.push_back( tmp[ 0 ] ); // ace can also be used as the high card

// special case: ace-2-3-4-5 straight
if ( tmp[ 0 ] == 1 && tmp[ 1 ] == 1 && tmp[ 2 ] == 1 &&
tmp[ 3 ] == 1 && tmp[ 4 ] == 1 )
return true;

unsigned i = 1; // counter to hold current position

// search for first non-zero face count
while ( i < tmp.size() && tmp[ i ] == 0 )
++i;

unsigned start = i; // save position

// count consecutive frequencies of 1
while ( i < tmp.size() && tmp[ i ] == 1 )
++i;

return i == start + 5; // should have counted 5 faces with frequency 1
} // end function straight
```
```
// Card shuffling and dealing program.
#include
#include
#include "DeckOfCards.h" // DeckOfCards class definition
#include "Hand.h" // Hand class definition
using namespace std;

int main()
{
DeckOfCards myDeckOfCards;
myDeckOfCards.shuffle(); // place Cards in random order

Hand hand( myDeckOfCards ); // deal a hand from the deck
hand.print(); // display hand

// check for each type of hand by decreasing ranking
if ( hand.fourOfAKind() )
cout << "Hand contains four of a kind" << endl;
else if ( hand.flush() )
cout << "Hand contains a flush" << endl;
else if ( hand.straight() )
cout << "Hand contains a straight" << endl;
else if ( hand.threeOfAKind() )
cout << "Hand contains three of a kind" << endl;
else if ( hand.twoPair() )
cout << "Hand contains two pairs" << endl;
else if ( hand.pair() )
cout << "Hand contains a pair" << endl;
} // end main
```

Computer Science & Information Technology

You might also like to view...

How many times will this loop execute?

``` for ( int i = 1 ; i <= 1 0 ; i++) System.out.println ( i ) ; ```

Computer Science & Information Technology

Microsoft Office for Mac 2011 offers ____ as a feature that lets you easily create professional looking business diagrams, such as organizational charts, process diagrams, and timelines.

A. SmartArt B. Draw Table C. Clip Gallery D. Table Styles Gallery

Computer Science & Information Technology

OODBMS are slower than relational DBMS.

a. true b. false

Computer Science & Information Technology

Write the equation of the line pictured in the graph.

What will be an ideal response?

Computer Science & Information Technology