15-200 Fall 2006
Homework Assignment 2
The Standard Poker Game
Program Due: Thursday Sep. 21, 2006 at 11:59pm
This assignment is designed to review the basic programming concepts that you have learned in your previous programming course. You are to implement some fragments of the Poker game. Namely, you will have to shuffle a deck of card, to randomly choose five cards and then to evaluate a hand.
- To reinforce the concept of object-oriented programming;
- To reinforce the concept of aggregation and collection classes;
- To gain experience with designing and implementing several classes;
- To gain experience dealing with global variables/constants;
- To gain experience with the java.util.Comparator interface
The Rules of Poker:
Poker uses a standard pack of 52 playing cards. The card ranking is as follows Ace (the highest), King, Queen, Jack, 10, 9, 8, 7, 6, 5, 4, 3, 2 (the lowest).
There are four suits (spades, hearts, diamonds and clubs). No suit is higher than another. The suit has no impact on value.
A poker hand consists of five cards. The categories of hand, from highest to lowest, are listed below. Any hand in a higher category beats any hand in a lower category (so for example any three of a kind beats any two pairs).
The use of Wild Cards depends on the variations. We won't be using wild
cards in this game.
Ranking of the hands:
- Straight Flush. Five cards of the same suit in sequence, such as 7-8-9-10-J. An Ace high straight-flush is called a Royal Flush and is the highest natural hand.
- Four of a Kind. Four cards of the same value, for example four Aces
or four tens. If there are two or more hands that qualify, the hand with the higher-rank four of a kind wins.
- Full House. A full house is three cards of the same value and a pair,
for example Q-Q-Q-5-5. When there are two full houses, the tie is broken by
the three of a kind. An example would be J-J-J-5-5 would beat 9-9-9-K-K.
- Flush. A flush is a hand where all five cards are of the same suit, for example A-K-10-4-2, all of Diamonds. When flushes ties, the tie is broken
by the High Card.
- Straight. Five cards with consecutive values, but not of the same suit. An example of a straight is 2-3-4-5-6. An ace may either be high (A-K-Q-J-T) or low (5-4-3-2-A). However, a straight may not 'wrap around' (such as Q-K-A-2-3). When two straights tie, the highest straight wins, K-Q-J-10-9 would beat 5-4-3-2-A. If two straights have the same value, A-K-Q-J-10 vs A-K-Q-J-10, the pot is split.
- Three of a Kind. Three cards of the same. Once
again the highest ranking three of a kind would win. K-K-K-2-4 would beat
- Two Pair. Two distinct pairs of card and a 5th card. The highest ranking pair wins. If both hands have the same high pair, the second pair wins. If both hands have the same pairs, the high card wins.
- Pair. Two cards of the same value and three distinct cards. Highest ranking pair wins. High card breaks ties.
- High Card. When a hand doesn't even have a pair, then it comes down to who is holding the highest ranking card. If there is a tie for the high card then the next high card determines the pot, if that card is a tie than it continues down till the third, fourth, and fifth card. The High card is also used to break ties when the high hands both have the same type of hand (pair, flush, straight, etc).
PART - I
You are to implement the Card and Deck classes. See provided templates for methods to implement and design requirements.
The Deck class has an important method getRandomCard(). In this method you generate a card with a random choice of its suit and value, and then
delete this card from the deck (using the method deleteCard()). In
the deletion procedure you first need to find this card in the deck and then
logically remove it. You do not resize the array but set the null reference
at a correspondent index. The success of searching depends on the equals()
method, that has to be implemented in the Card class. In getRandomCard(), you have to make sure that there should be no duplicates. One possible implementation, is one where you generate a random number (0-51) and choose a card from the deck at that index, making sure that the chosen card is actually present in the deck (not null).
Another complex method in the Deck class is shuffle(). There are several ways to implement it, however the requirement is to use a method of that name from the Collections class.
PART - II
You are to design the PokerHand class and implement s evaluation function. See provided template for implementation requirements.
The evaluate() method has to analyze the hand of five random cards and rank it according to the game rules. You are encouraged to implement auxiliary (helper) methods in the private context for each rule of the game.
We provide you with two classes SuitSort and ValueSort that are used for sorting a hand with respect to either suit or value. Java's Comparator interface is a flexible tool for enforcing application-specific orderings.
PART - III
Finally, you simulate playing a game between two players. You deal these players 5 cards and then compute the winning hand.
A sample output (to the console) is as follows:
Player 1: Two of Diamond,
Three of Spade,
Four of Diamond,
Five of Club,
Six of Diamond.
Player 2: Three of Diamonds,
Four of Clubs,
Four of Hearts,
Queen of Hearts,
Queen of Spades.
EVALUATION: Two Pairs;
Winner is: Player 1.
If players have the same ranking, you DO NOT evaluate the individual cards to break ties. Instead, you print a message that the game ends in a tie.
BONUS (10 pts) Attempt to implement a complete evaluation including tie-breaking rules.
- You must follow provided specifications.
- Feel free to add any private methods, fields and constants to all classes.
- When you randomly draw a card, make sure that it is deleted from the deck.
- You need to comment your code.
- The modularity is a big deal when you implement
evaluate(). You have to split your code into several helper methods, otherwise it becomes very difficult to debug and correct.
- Don't rush on to the next stage unless the previous one works reasonably well.
- Use global variables wisely.
- See output.txt for a likely output.
What You'll Need:
Create a private directory for your work, download the file lab.zip into it, and then unzip it. You should see the following files:
Handing in your Solution:
FTP your implementation to