Friday, September 9, 2011

Console Poker

We are going to write a game of video poker. Typically, it should have a graphical interface. However, to make thing simple, we will start coding the game with a console interface first. The graphical interface will be presented in the next article.

Game Flow
This will be a text mode game and the user will be required to input the command with the keyboard. The only commands are "deal" and "hold". The user will input "D" for "deal", and input a sequences of digit for holding cards. For example, entering "124" will hold the first, second and the forth card.

Sample Run
In the following, the user entered 'D' to draw five cards at random, he then enter "245" to hold the second, forth and the fifth card, and replace the remaining cards with other randomly picked cards. Finally he got "three of a kind" which is a winning pattern.

java ConsolePoker
Credit : 50     Enter 'D' to deal : D
C7 C3 H4 S3 H3
Hold : 245
C6 C3 DK S3 H3
Three of a kind
You win 15
Credit : 60     Enter 'D' to deal :
Example 2 : In the following case, the user entered 'D' to get five cards from deck, which already formed a pair. He held the second and the third card by entering '23' at the command line. He was lucky enough to draw another Jack and two Queens to get a "Full House" winning pattern. Then he played again, held the first and the third card, but this time he was unable to improve the pattern and he lost 5 credits. The game will continue until the credit goes below zero.
java ConsolePoker
Credit : 50     Enter 'D' to deal : D
Hold : 23
Full house
You win 45
Credit : 90     Enter 'D' to deal : D
H3 DQ C3 SK H2
Hold : 13
H3 S7 C3 S4 C2
You lose
Credit : 85     Enter 'D' to deal :

Winning Patterns and Payout Table

Jacks or better1
Two pair2
Three of a kind3
Full House9
Four of a kind25
Straight Flush 50
Royal Flush 800

The Pattern class
Since the name and the payout are the two attributes of a pattern, it is natural to define a Pattern class with two attributes :

// Define constants for winning pattern
class Pattern
  String name;
  int payout;

  private Pattern(String name, int payout)
  { = name;
    this.payout = payout;

Private Constructor
The above Pattern class has a private constructor, that means no other class can define new instances of Pattern. So we must declare the Pattern constants inside the pattern class. This technique is useful to represent a predefined set of constants.

Rewriting the Pattern matcher
We are now going to rewrite presented in the previous article

We have already defined a method known as getPatternName() Now we are going to define another method known as getPattern() to replace the above one. Now we no longer need getPatternName(). But to maintain compatibility, you may define a new one if you wish :

Game Control
Now we can define the game control of the Console Poker. If you have read the sample run above, it should be easy to understand.

Full Source Code

You will need, and in the same folder to run the game.

To compile :

javac *.java
To run :
java ConsolePoker
I will present the full listing of the three files below. You may double click on the source code to make a copy for yourself.

A little hints is simple. is just a copy from the previous article, see the related article section for details. is the main game flow and it is not difficult to understand if you try to play the game yourself.

List 1 :

List 2 :

List 3 :

Related Articles

Coming next
A graphical interface of the same game will be presented. We will reuse the three source files in this article and just write a graphical interface on top of that.

No comments:

Post a Comment