Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that reads five cards from the user, then analyzes the cards and prints out the category of hand that they represent. Poker

Write a program that reads five cards from the user, then analyzes the cards and prints out the category of hand that they represent.

Poker hands are categorized according to the following labels: Straight flush, four of a kind, full house, straight, flush, three of a kind, two pairs, pair, high card.

To simplify the program we will ignore card suits, and face cards. The values that the user inputs will be integer values from 2 to 9. When your program runs it should start by collecting five integer values from the user and placing the integers into an array that has 5 elements. It might look like this:

Enter five numeric cards, no face cards. Use 2 - 9.

Card 1: 8

Card 2: 7

Card 3: 8

Card 4: 2

Card 5: 3

(This is a pair, since there are two eights)

Since we are ignoring card suits there won't be any flushes. Your program should be able to recognize the following hand categories, listed from least valuable to most valuable:

High Card: There are no matching cards, and the hand is not a straight, example:2, 5, 3, 8, 7

PairTwo: of the cards are identical, example:2, 5, 3, 5, 7

Two Pair: Two different pairs, example:2, 5, 3, 5, 3

Three of a kind: Three matching cards, example:5, 5, 3, 5, 7

Straight: 5 consecutive cards3, 5, 6, 4, 7

Full House: A pair and three of a kind5, 7, 5, 7, 7

Four of a kind: Four matching cards, example:2, 5, 5, 5, 5

(A note on straights: a hand is a straight regardless of the order. So the values 3, 4, 5, 6, 7 represent a straight, but so do the values 7, 4, 5, 6, 3).

Your program should read in five values and then print out the appropriate hand type. If a hand matches more than one description, the program should print out the most valuable hand type.

Here is are three sample runs of the program:

Enter five numeric cards, no face cards. Use 2 - 9.

Card 1: 8

Card 2: 7

Card 3: 8

Card 4: 2

Card 5: 7

Two Pair!

Enter five numeric cards, no face cards. Use 2 - 9.

Card 1: 8

Card 2: 7

Card 3: 4

Card 4: 6

Card 5: 5

Straight!

Enter five numeric cards, no face cards. Use 2 - 9.

Card 1: 9

Card 2: 2

Card 3: 3

Card 4: 4

Card 5: 5

High Card!

You must write a function for each hand type. Each function must accept a const int array that contains five integers, each representing one of the 5 cards in the hand, and must return "true" if the hand contains the cards indicated by the name of the function, "false" if it does not. The functions should have the following signatures.

bool containsPair(const int hand[])

bool containsTwoPair(const int hand[])

bool containsThreeOfaKind(const int hand[])

bool containsStraight(const int hand[])

bool containsFullHouse(const int hand[])

bool containsFourOfaKind(const int hand[])

There are two subtle questions here that I want to clarify. If you get lost in this paragraph, you can just ignore it, unless you get to a point where you are actually asking yourself one of these questions. (1) Does a hand contain one of the hand categories if it also contains another, higher valued hand category? For example, does 2, 2, 2, 3, 4 contain a pair, since it also contains three of a kind? I will leave that up to you to decide. (2) Is it ok to return true even though the hand doesn't actually contain the indicated category of hand, as long as you handle this in main() by adjusting the order in which the functions are called? The answer is no, that's not ok.

You do not need to write a containsHighCard function. All hands contain a highest card. If you determine that a particular hand is not one of the better hand types, then you know that it is a High Card hand.

Do not sort the cards in the hand.

no function may have nested loops in it. If you need nested loops, the inner loop must be turned into a separate function, hopefully in a way that makes sense and so that the separate function is general enough to be re-used by the other functions. Also, no function other than main() may have more than 5 lines of code. (This is counting declarations, but not counting lines that have only a curly brace on them.) In my solution I was able to create just 3 helper functions, 2 of which are used repeatedly by the various functions.

Test these function independently. Once you are sure that they all work, the program logic for the complete program will be fairly straightforward.

Here is code that tests a containsPair function:

int main() {

int hand[] = {2, 5, 3, 2, 9};

if (containsAPair(hand)) {

cout << "contains a pair" << endl;

}

}

Step by Step Solution

3.42 Rating (149 Votes )

There are 3 Steps involved in it

Step: 1

Code The screenshots of outputs of the program are as follows Include the necessary library fil... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Probability And Statistics

Authors: William Mendenhall, Robert Beaver, Barbara Beaver

14th Edition

1133103758, 978-1133103752

More Books

Students also viewed these Programming questions

Question

Explain the use of a drawing account in a partnership.

Answered: 1 week ago