Saturday, September 17, 2011

The Game of Video Poker

Prerequisites
This is a continuation of previous works.
You would need to read "Console Poker" first.

Quick Start
If you just want to play the game, go to the Source Code section below.


Game Flow
There would be a deal button in the screen. Once it is pressed, five cards would be dealt to you.

If the initial deal forms a winning pattern, you would hold all the cards and press the deal button again. Otherwise, you may hold any number of cards, hoping that the second deal would improve the chance of winning.

The cards are actually buttons and therefore are clickable objects. Clicking on them would change the hold status.

Image Buttons
We can apply a image to the JButton component. But first of all, we must have the card image on hand.
Drawing a playing card is not difficult, and this is already demonstrated in a previous article. For details, read :

We will reuse the file CardPanel.java in the above article. Actually, all we need is the createImage() method, which would be called during initialization in VideoPoker.java

Note that VideoPoker.java will be the only source file created in this article, all other source files come from previous works. This is one of the important software design principle :

Don't reinvent the wheel

Poker Patterns
The table of winning patterns and payouts are shown in the previous article "Console Poker" The game flow logic are inside the source file ConsolePoker.java in the above article. This file will be reused here.


Design
Ideally, the GUI should be separated from the game logic, this is not 100% true in this sample, but it is getting close.

  • ConsolePoker.java - game flow logic
  • VideoPoker.java - GUI components
Auxiliary files :
  • Poker.java - the pattern matcher
  • Pattern.java - define pattern constants
  • CardPanel.java - create card images

Source Code
You will need all the five java files in the same folder.

  • VideoPoker.java - GUI components
  • ConsolePoker.java - game flow logic
  • Poker.java - the pattern matcher
  • Pattern.java - define pattern constants
  • CardPanel.java - create card images

To compile :
javac *.java
To run :
javac VideoPoker
Only VideoPoker.java is new in this article, all other source files are from previous works. Anyway, all of them will be listed here for your convenience.



ConsolePoker.java
Originally defined in : Console Poker




Poker.java
Originally defined in : Console Poker




Pattern.java
Originally defined in : Console Poker




CardPanel.java
Originally defined in : How to display a deck of playing card in a panel ?


Friday, September 9, 2011

Console Poker

Introduction
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
DK CJ SJ S4 H8
Hold : 23
HJ CJ SJ HQ DQ
Full house
You win 45
Credit : 90     Enter 'D' to deal : D
H3 DQ C3 SK H2
Hold : 13
H3 S7 C3 S4 C2
Nothing
You lose
Credit : 85     Enter 'D' to deal :



Winning Patterns and Payout Table

PatternsPayout
Jacks or better1
Two pair2
Three of a kind3
Straight4
Flush6
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)
  {
    this.name = 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 Poker.java 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 Pattern.java, Poker.java and ConsolePoker.java 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
Pattern.java is simple.

Poker.java is just a copy from the previous article, see the related article section for details.

ConsolePoker.java is the main game flow and it is not difficult to understand if you try to play the game yourself.



List 1 : Pattern.java


List 2 : ConsolePoker.java


List 3 : Poker.java


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.

Saturday, September 3, 2011

Poker Pattern Matching

Related Articles : If you didn't read "Probability by Counting", read it first.

Suppose you have drawn the following five cards from a deck :
C7 S2 S4 D8 H7

Recall that we use two characters to represent a card, the first character represents the suit and the second represents the point. Hence C7 means the Seven of Club, S2 means the Two of Spade, etc.

To recognize whether there is pair, we sort the five cards by points :
S2 S4 C7 H7 D8

Then we may compare consecutive cards to count the number of potential pairs. Note that the number of potential pairs not necessary means actual pairs. For example :

cards :S3 S7 C7 H7 D9
index : 0  1  2  3  4
card[1] and card[2] potentially forms a pair, so does card[2] and card[3]. Hence the number of potential pairs is two. But actually this is not a "two pair" pattern. It is known as a "Triple" pattern.


Pair Recognition
For any five card combination, it is straight forward to recognize whether it is a "pair pattern" in the poker game. Just count the potential pair formed by consecutive cards. If the result is one, then it is a "pair pattern".


Jack or Better
For the game of video poker, a pair is not necessary a winning pattern. You need to get a pair of "jack or better" in order to win. It is easy to modify the method isPair() to recognize a "Jack or Better" pattern.


Two Pairs
A "two pairs" recognizer is similar to a "pair" recognizer, it just counts the number of potential pairs form by consecutive cards, but it must be clever enough to understand that "a potential pair is not necessary an actual pair". The above comment says "In the caller, we must exclude the "triple" pattern". You will see how this is done in subsequent section.


Four of a kind
There are only two possible patterns : XXXXY or XYYYY. Therefore, two "if statements" would be sufficient to do the task.


Triple
It is also known as "Three of a kind". It is very similar to the above recognizer. Just like the "two pairs" recognizer, it is incomplete and expect some checking in the caller. The caller will be provided later. You will see it is much easier than you think.


Full House


Flush
The recognizer just need to make sure all the cards are of the same suit.


Straight
A recognizer for the "straight pattern" should ignore the suit of all cards. The first thing we should do is to remove the suit. For example, if we have the following cards :

{ "S2","S4","C7","H7","D8" }
The above cards are already sorted by points. Removing all the suit would become :
 "2","4","7","7","8" 
We can define a string to hold the point pattern.
String pointPattern="24778";
Of course it should not be hard coded and it should be built from the card list. Once we have the pointPattern on hand, we can compare it against a predefined list of "Straight patterns". See the following code segment for details.


The caller
We have seen several comments about what the caller must do. For example, in the implementation of isTwoPair(), the comment says "the caller must exclude the triple" pattern. Actually, if we implement the caller with a priority, we don't need extra checking at all.

If a pattern is a full house, then it is not a triple.
If a pattern is a triple, then it is not a "two pairs".
We just check the pattern in the priority order, and the result would be correct. See the code below and you will understand.


Demonstration
We are going to write a program that draws five card from a shuffled deck, then display the name of the pattern. A sample run follows :

java Poker
D8 S7 C7 H3 C3
Two pairs

java Poker
S6 C5 SA H3 HT
Nothing

java Poker
D6 S2 H2 D5 H6
Two pairs

java Poker
SA S7 H4 D4 HT
Nothing

java Poker
H5 CA ST H9 HT
Nothing

java Poker
S9 D3 SJ H5 CJ
Jack or better