Title: Poker Hand
1Poker Hand
- Project 6
- Mon, Dec 1, 2003
- Due Mon, Dec 8, 2003
2The PokerHand Program
- This program will
- Generate a specified number of poker hands at
random. - Categorize each hand.
- Report
- The number of hands in each category.
- The relative frequency of each category.
3The Card Class
- Each card has a suit and a face value.
- Suit is an enumerated type.
- enum Suit clubs, diamonds, hearts, spades
- Face is an enumerated type.
- enum Face two, three, four, five, six, seven,
eight, nine, ten, jack, queen, king, ace
4The Order Relation on Cards
- The function operatorlt() is defined to compare
Cards - First by Face value,
- Then by Suit.
- For example,
- two of spades lt ace of clubs
- The two of clubs is the lowest Card.
- The ace of spades is the highest Card.
5The Deck Class
- A Deck object consists of
- An array of 52 Cards.
- A RandomInt object, used to shuffle the deck.
6The Deck Class
- Internally, in the array, the cards are indexed
as 0 through 51. - Externally, through the user interface, they are
indexed as 1 through 52.
Card DeckgetCard(int i) const assert(i gt 1
i lt 52) return cardsi - 1
7The PokerHand Class
- A PokerHand object consists of an array of 5
Cards. - The quality of a hand is the type of poker hand
that it is. - enum Quality nothing, onePair, twoPairs,
threeOfAKind, straight, aFlush, fullHouse,
fourOfAKind, straightFlush, royalFlush
8The Deck Class (again)
- The Deal() function returns a PokerHand object,
consisting of 5 Cards dealt from the top of the
deck. - This function should get cards 1 5 from the
deck and set them in the poker hand.
9Determining the Type of Hand
- To facilitate the analysis of the hand, many of
the functions should first arrange the cards in
order. - That makes it much easier to check for one pair,
two pairs, three of a kind, four of a kind, etc. - Use a Sort() function to do this.
10The sort() Function
- Use any of the sorting algorithms that we
discussed. - When swapping cards, either
- Swap the array elements, or
- Use GetCard() and SetCard() to move them.
- The order imposed on the cards is the order
defined by operatorlt() in the Card class.
11The hasOnePair() Function
bool PokerHandhasOnePair() const
PokerHand hand copyHand() hand.sort()
CardFace f1 hand.getCard(1).getFace()
CardFace f2 hand.getCard(2).getFace()
CardFace f3 hand.getCard(3).getFace()
CardFace f4 hand.getCard(4).getFace()
CardFace f5 hand.getCard(5).getFace()
return (f1 f2) (f2 f3) (f3
f4) (f4 f5)
12Determining the Quality of the Hand
- Once the hand is sorted, two pairs can occur in
only 3 ways. - The possible patterns are
- AABBC
- AABCC
- ABBCC
- How many patterns were possible before the hand
was sorted?
13Determining the Quality of the Hand
- In a sorted hand, what are the possible patterns
for - Three of a kind?
- Four of a kind?
- A full house?
14Determining the Quality of the Hand
- Test for a flush by seeing if all the cards have
the same suit. - Test for a straight by seeing if each card has a
value one greater than the card before it. - A straight flush is both a straight and a flush.
- Call hasStraight() and hasFlush().
15Determining the Quality of the Hand
- A royal flush is a straight flush in which the
high card is an ace. - Call hasStraightFlush() and getHighCard().
16Determining the Quality of the Hand
- The quality() function must test for the simpler
types first. Otherwise, the function may
miscategorize the hand. - For example, if a hand has four of a kind, then
the functions hasThreeOfAKind(), hasOnePair(),
and hasTwoPairs() will also return true.
17The main() Function
- The main() function should generate at random the
specified number of PokerHands. - Shuffle the deck every time before dealing a
hand. - Each hand should be categorized according to its
quality. - A counter for that type of hand should be
incremented. - Print the counts and the relative frequencies.
18The Output
- The output will be different every time the
program is run, even for the same input. - However, the following chart shows what to expect.