Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that reads five (or more) cards from the user, then analyzes the cards and prints out the category of hand that they

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Write a program that reads five (or more) 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) No input validation is required for this assignment. You can assume that the user will always enter valid data (numbers between 2 and 9). 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: (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). (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 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! Additional Requirements 1. 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 containsThree0fakind (const int hand []) bool containsStraight (const int hand [] ) bool containsFullHouse (const int hand[]) bool containsFourOfaKind(const int hand [] ) Note that there are some interesting questions regarding what some of these should return if the hand contains the target hand-type and also contains a higher hand-type. For example, should containsPair() return true for the hand [2, 2, 2, 3, 4]? Should it return true for [2, 2, 3, 3, 4]? [2, 2, 3, 3, 3]? I will leave these decisions up to you. 2. Of course, as a matter of good practice, you should use a constant to represent the number of cards in the hand, and everything in your code should still work if the number of cards in the hand is changed to 4 or 6 or 11 (for example). Writing your code so that it does not easily generalize to more than 5 cards allows you to avoid the objectives of this assignment (such as traversing an array using a loop). If you do this, you will receive a 0 on the assignment. 3. 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. 4. Do not sort the cards in the hand. Also, do not make a copy of the hand and then sort that. Suggestions Test these functions 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 (containsPair(hand)) \{ cout "contains a pair" endl \} An important objective of either of the above 1D array related assignments is to have you practice creating excellent decomposition. Don't worry about efficiency on this assignment. Focus on excellent decomposition, which results in readable code and do not forget documentation. In either of these programs you can rush and get it done but end up with code that is really difficult to read, debug, modify, and re-use. If you think about it hard, you can think of really helpful ways in which to combine the tasks that the various functions are performing and make use of appropriate calls from within functions or through the interface An important objective of either of the above 1D array related assignments is to have you practice creating excellent decomposition. Don't worry about efficiency on this assignment. Focus on excellent decomposition, which results in readable code and do not forget documentation. In either of these programs you can rush and get it done but end up with code that is really difficult to read, debug, modify, and re-use. If you think about it hard, you can think of really helpful ways in which to combine the tasks that the various functions are performing and make use of appropriate calls from within functions or through the interface provided. Functions other than main() should have no more than 5 - 20 lines of code. (This is counting declarations, but not counting the function header, function related documentation, blank lines, or lines that have only a curly brace on them. Less is best!) These additional criteria are intended as an extra challenge and may be difficult for many of you. If you can't figure it out, give it your best shot, but don't be too discouraged. And be sure to attempt the Algorithm Workbench 2 - 1D Array processing practice or extra credit activity (see link at the top) as the algorithms you write for practice maybe helpful and make sure to study the posted solution carefully after the assignment has been graded. (Portions of this assignment were adapted from Professor Harden and the Tony Gaddis text problem sets)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

More Books

Students also viewed these Databases questions

Question

=+and non-compete agreements in three to five different countries.

Answered: 1 week ago