15-111 Spring 2007

Homework Assignment 2

The Standard Poker Game

Program Due: January 31, 2007 at 11:59pm
Credit: Thanks to Victor Adamchik


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.


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:



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.


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.


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.

          EVALUATION:     Straight;

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.


  1. You must follow provided specifications.
  2. Feel free to add any private methods, fields and constants to all classes.
  3. When you randomly draw a card, make sure that it is deleted from the deck.
  4. You need to comment your code.
  5. 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.
  6. Don't rush on to the next stage unless the previous one works reasonably well.
  7. Use global variables wisely.
  8. 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:

We'll discuss this before the due date.