CPSC 120B Fall 2003
Program 4: Poker Hands
Due Friday, December 5

Problem

Write a program that simulates dealing 5-card poker hands to determine the probability of being dealt each the following hands. The hands are listed here from strongest to weakest:
straight flush -- straight in which all five cards are of the same suit 
four of a kind -- four cards of the same rank
full house -- a pair and three of a kind (of different ranks)
flush -- all five cards of the same suit
straight -- all five cards of consecutive ranks
three of a kind -- three cards of the same rank
two pair -- two pairs of different ranks
one pair -- two cards of the same rank 
The usual rule for a straight is that Ace can be low or high but not both (so no QKA23), but we will always treat Ace as low. Note that a hand is categorized as the strongest hand that can made from the given cards; e.g., a "two pair" hand is not also a "one pair" hand.

Program Structure

You will need three classes for this project: Card, Hand, and Poker. We wrote most of the Card class in class, although it may need minor modification. But most of the work is in the other two classes.

The Hand Class

The Hand class should have the following public methods:

You will find that it is easier to determine what the hand is if you sort the cards. This is complex with five variables with the tools we have, so you can use my sort method. This method assumes that your five cards are stored in instance variables card1, card2, card3, card4, and card5, and it uses the getRank() method of the Card class. When sort returns, card1 holds the lowest card (by rank), card2 the next, and so on. Just insert this method into your Hand class and call it when you want to sort the cards. Be sure you have already instantiated the card variables.

The Poker Class

The Poker class is the driver. It should ask the user how many hands to simulate, then run through a loop that each time generates a new hand, determines what it is and increments the appropriate counter. After the loop, it should print the probability of each hand in a "1 in x" format, so a run of your program might look something like this:
How many hands to simulate? 1000000
*********************************************
** Probabilities on 1000000 simulated hands**
*********************************************
one pair:               1 in 2.363675041955232
two pair:               1 in 20.862453841820876
three of a kind:        1 in 47.12091226086137
straight:               1 in 288.43380444188057
flush:                  1 in 535.6186395286556
full house:             1 in 705.7163020465773
four of a kind:         1 in 4201.680672268908
straight flush:         1 in 71428.57142857143

Your probabilities will vary a little, but for large numbers of hands they should be pretty close to these. You'll get the most variation in the least likely hands. Note that our numbers for straight and straight flush are not quite accurate because we require that Ace be low, so we're missing the 10JQKA straight.

Documentation and Style

Provide a program header that gives your name, the name of the file, and a description of the program. Also provide a header for each method that gives its name, its parameters, and its return value, and briefly explains what it does. Use good names for variables and constants, and use an explanatory comment whenever a portion of code might not be clear to the reader. Also be sure to follow the capitalization conventions discussed in class.

What to Turn In

Turn in hardcopy of your program and e-mail the source code to bloss@roanoke.edu. Put cs120 prog4 in the subject line. Both the hardcopy and the e-mail are due by 4:00 on the date above.