Lab 9: A Game of Poker

Due by the end of class

We are yet again making another foray into the wide world of cards. A popular casino game is Three Card Poker. In the game of Three Card Poker, you are dealt three cards. You win if you get a pair or better. Otherwise, the house gets your bet.

Your mission is to write methods that will:

  • Return the name of a given card
  • Determine if three cards form three of a kind
  • Determine if three cards form a flush
  • Determine if three cards form a pair

Specification

Create a project called Lab9. Add a class called Poker. Inside Poker, delete all the code, and paste in the code from my skeleton code file Poker.java. As you can see, the four methods you must write have been created. All you have to do is fill in the TODO's. Even the main() method has already been created.

The main() method will generate 3 cards, with numerical values in the range 0-51. Cards 0-12 are the Ace through the King of Spades. Cards 13-25 are the Ace through the King of Hearts. Cards 26-38 are the Ace through the King of Diamonds. Cards 39-51 are the Ace through the King of Clubs.

Given a numerical value stored, for example, in the variable card, you can determine rank and suit using the following code.

rank = (card % 13) + 1;
suit = (card / 13) + 1;

The first statement will give a rank in the range from the Lab 6 (with 1 as Ace, 11 as Jack, 12 as Queen, 13 as King, and all other ranks as their number). The second statement will give a suit in the range from Lab 6 (with 1 as Spades, 2 as Hearts, 3 as Diamonds, and 4 as Clubs).

getName() Method

This method takes a numerical card value and returns the corresponding name. Using the equations above and the innermost part of your loops from Lab 6, you should easily be able to come up with this code.

three() Method

This method takes three numerical card values. If all three cards have the same rank, this method returns true, otherwise it returns false. Three of a kind is the second highest hand in Three Card Poker.

two() Method

This method takes three numerical card values. If any two of the three cards have the same rank, this method returns true, otherwise it returns false. A pair is the fifth highest hand in Three Card Poker. Don't worry about checking for three of a kind. That is, a three of a kind hand should return true if check by the two() method.

flush() Method

This method takes three numerical card values. If all three cards have the same suit, this method returns true, otherwise it returns false. A flush is the fourth highest hand in Three Card Poker.

Sample Output

The provided main() method will generate 3 distinct card values randomly and then call your printName() method to print each one out. Next, the program will call your three(), flush(), and two() methods to determine how good your hand was. It will list the highest score your hand was able to achieve or simply "High card!" for a hand with no special score. Note: Straight flushes and straights are ignored by our program because they are more difficult to calculate.

Here are a few sets of sample output. Your output should be similar but will probably not match, since the values are generated randomly. Consider hacking the main() method to fix particular values so that you can test your code. (Waiting for three of a kind to pop up can take a while.)

Output 1

Your three cards are:
Ace of Spades
Ace of Hearts
Ace of Diamonds
Three of a kind!

Output 2

Your three cards are:
4 of Clubs
3 of Spades
9 of Clubs
High card!

Output 3

Your three cards are:
4 of Diamonds
4 of Clubs
5 of Hearts
Pair!

Output 4

Your three cards are:
2 of Clubs
Jack of Clubs
8 of Clubs
Flush!

Turn In

Turn in your code by uploading Poker.java from the Lab9\src folder wherever you created your project to Blackboard. Do not upload the entire project. I only want the Poker.java file.

All work must be done individually. Never look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.