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.

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

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

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 :
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.

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

java Poker
D6 S2 H2 D5 H6
Two pairs

java Poker
SA S7 H4 D4 HT

java Poker

java Poker
S9 D3 SJ H5 CJ
Jack or better

1 comment:

  1. This is awful informatics, brittle and clear. playrouletterules I anticipate that Everything has been declared in analytical address so that clairvoyant could get best advice and apprentice abounding things. This is one of the best blogs I accept read.