$24
Optional hard copy may be turned in at the beginning of lab.
Overall Assignment
For this assignment, you are to write a simple computer program that deals a hand of three cards, and evaluates it according to the rules of Three-Card Poker.
Background: Three Card Poker1
Three Card Poker is a variation of the game of Poker, in which each player receives 3 cards, and the hands are evaluated according to the table shown below1. ( This assignment does not require the evaluation of straights or straight flushes; however they may be included in optional enhancements. )
Hand Ranks of Three Card Poker
Rank
Description
Frequency
Probability
Straight flush
Three suited cards in sequence
48
0.22%
Three of a kind
Three cards of same rank
52
0.24%
Straight
Three cards in sequence
720
3.26%
Flush
Three suited cards
1,096
4.96%
Pair
Two cards of same rank
3,744
16.94%
High card
None of the above
16,440
74.39%
Total hands
-
22,100
-
Program Details
For this assignment, you are to write a simple computer program that randomly generates three cards, evaluates the resulting hand, and reports the results.
Your program should first print out your name and ACCC netID ( e.g. jbell ), and explain to the user what the program does.
Your program does not require any input for the base assignment, but may for some optional enhancements.
The program must then randomly generate 3 cards and report their values in a table as shown below. ( To get started quickly you may skip this step, and either hard-code in some card values or ask the user to enter them as integers described below. )
Finally classify the hand according to the hand ranks shown in the table above. If the hand classifies as “high card”, then the highest card in the hand should be reported using words, e.g. “king of hearts”.
1 For full details, including variations, wagering, and strategies, see https://en.wikipedia.org/wiki/Three_card_poker.
Generating a Random Playing Card
There are several ways to do this. For this assignment, a playing card will be represented by two integers – “rank” in the range from 0 to 12 representing the “number” of the card, and “suit” in the range from 0 to 3 representing the suit. ( For 3 cards, you will need 6 variables – rank1, rank2, rank3, suit1, suit2, and suit3 – since we are not using arrays for this assignment. ) The rank and suit values can be easily generated using the following method:
Initialize ( seed ) the random number generator by passing the current time as an argument to srand( ). The exact call is “srand( time( NULL ) );” You need to do this step exactly once, no matter how many random numbers you eventually generate. You also need to #include<ctime with your other #includes in order to use time( ).
Generate a random integer in the range of 0 to 51, by calling rand( ) and modding the result with
( rand( ) generates a random integer from 0 to RAND_MAX, which is around 2 billion or so on most machines. ) Let’s call this variable “cardIndex”.
Determine the suit of the card by modding cardIndex with 4, to give a result in the range of
to 3. Let’s call this “suit”.
Determine the number of the card ( in the range 0 to 12 ) by dividing cardIndex by 4, using integer division. If cardIndex is 0, 1, 2, or 3, this will yield 0. For 4, 5, 6, or 7, this will yield 1.
So on up to 48, 49, 50, and 51 which will yield 12. Let’s call this “rank”.
( You should satisfy yourself that this approach will always yield valid combinations of suits and ranks, and that if you could loop from cardIndex = 0 to 51 that you would generate a complete deck of 4 suits and 13 cards in each suit. )
For this assignment, repeat steps 2 to 4 for the remaining two cards of the hand, and report the results in a simple table as shown in the following example. ( For the basic assignment, you may ignore the problem of avoiding duplicates, so if a hand has two or more identical cards, that is okay. However, if this happens frequently, it probably indicates a problem in your code. )
Rank Suit
2
0
3
Classifying the Hand
This step is the real core and purpose of this assignment. Using a series of if-else statements, classify the hand according to the table shown above, and report the highest ranking hand for which it qualifies. ( For example, if all three cards have the same rank, it should be classified as three-of-a-kind, not a pair. ) For the base assignment, you do not need to consider straights or straight flushes.
Hint: the order in which you check for hands may make the work easier or harder. For example, it is easier to check for 3 of a kind before checking for pairs. If a hand has a pair or three of a kind then it cannot be a straight, and can only be a flush if the hand contains duplicates.
Simplification: It may be easier to start out asking the user to enter values for the card ranks and suits, or even hard-coding values into the program, instead of generating them randomly. That will let you get started first on the classification problem, which is the real important part of this assignment.
Reporting a Playing Card with Words
For initial development, it is easiest to simply print out the values of rank and suit as integers, and this will be acceptable for printing out the entire hand, ( as shown above in table format. ) However, you must also print out one card ( preferably the highest card ) of the hand with words, according to the following tables:
If the rank is:
then print:
0 to 8
rank + 2
9
Jack
10
Queen
11
King
12
Ace
If the
then print:
suit is:
0
Clubs
1
Diamonds
2
Hearts
3
Spades
So for example if a card had a rank of 11 and a suit of 2, it would be reported as the King of Hearts.
Special Notes:
The basic assignment should not use any loops, arrays, or functions. You may use these features in your program only if you implement one of the optional enhancements ( see below ), e.g. to allow the user to solve multiple problems without restarting the program.
Since you haven’t learned how to test things yet, you can assume that all user input is good.
What to Hand In:
Your code, including a user documentation file, should be handed in using Blackboard.
All files should be zipped together into a single file, whose name is comprised of your ACCC netID followed by the course number followed by the letters "HW", followed by the assignment number. ( E.g. jbell109HW3.zip ) The zip file should be handed in via Blackboard. ( Your TA may provide alternate instructions, which override these. )
The intended audience for the documentation file is a general end user, who might want to use this program to perform some work. They do not get to see the inner workings of the code, and have not read the homework assignment. You can assume, however, that they are familiar with the problem domain ( e.g. trigonometric identities. )
A secondary purpose of the documentation file is to make it as easy as possible for the grader to understand your program. If there is anything special the grader should know about your program, be sure to document it in the documentation file. In particular, if you do any of the optional enhancements, then you need to document what they are and anything special the TA needs to do to run your program and understand the results.
If there are problems that you know your program cannot handle, it is best to document them as well, rather than have the TA wonder what is wrong with your program.
Make sure that your name appears at the beginning of each of your files. Your program should also print this information when it runs.
Optional Enhancements:
It is course policy that students may go above and beyond what is called for in the base assignment if they wish. These optional enhancements will not raise any student’s score above 100 for any given assignment, but they may make up for points lost due to other reasons.
Handle straights and straight flushes.
In some variations of three card poker hands may be classified as “prime” if they are all the same color, e.g. either all hearts and diamonds or all clubs and spades.
Don’t allow duplicate cards. E.g. a king of hearts and a king of spades would be okay, but two kings of hearts would not be okay. Note that to do this right would require loops, and could in theory take an infinite amount of time if duplicates were repeatedly generated.
Deal two hands, one for the player and one for the dealer, and report who has the better hand. If both hands have the same rank, e.g. a pair, then the higher ranked number determines the winner. E.g. a pair of kings beats a pair of sevens. ( The potential for duplicate cards complicates this problem. At some point you may just have to declare the hands a tie. )
Ask the user how many hands are desired, and deal out that many hands. Classify them all and declare someone a winner.
Ask the user how many cards to deal to each player, and implement 4-card, 5-card, or more versions of poker. If more than 5 cards are involved, the program must select which 5 generates the best poker hand.
Explore the Wikipedia page referenced on the first page and implement some other aspects of the game not described here, such as wagering.
Write a function to print out a single card in words instead of numbers. With this function in place it becomes appropriate to print out the entire hand(s) in words instead of just the high card, by calling the function as many times as needed.
Ask the user if they would like to solve additional problems, and if so, repeat until they indicate they are done.
Check the data entered to verify that it is valid. ( Only relevant if the program reads in data. ) Other enhancements that you think of – Check with TA for acceptability.