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:
- A constructor that generates a random hand with five distinct cards.
These cards should be stored in (five) instance variables. Be sure
to check that they are distinct -- you should use a separate (private)
method to do this.
- Boolean methods to determine whether the hand contains a straight, flush,
three of a kind, etc. You will generally need one method for each kind of hand
you are testing for (public boolean straight(),
public boolean threeOfAKind(), etc). These are easier to write
if you assume that the stronger hands have already been tried. For
example, in the pair method, if you find a pair you don't have to worry
about whether it's part of a full house -- if it was, you wouldn't have
called the pair method in the first place. Just be sure you actually
use the methods this way!
- A toString method that returns a string containing the toString
values of the cards in the hand, one per line,
in the format we used in class (e.g., "King of Clubs"). Remember you
can use "\n" to insert a newline into a string.
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.