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

  2. I can set up my new idea from this post. It gives in depth information. Thanks for this valuable information for all,.. Loren Poker

  3. Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info.

  4. exclusive bonuses, casino games, casino betting tips and strategies by 바둑이사이트

  5. I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often. click my page

  6. This is also a very good post which I really enjoy reading. It is not everyday that I have the possibility to see something like this. pg

  7. This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again. ogłoszenia towarzyskie olsztyn

  8. รับเครดิตฟรีปัจจุบันเว็บเราได้เอาระบบใหม่ๆ มาให้กับผู้เล่นได้ทดสอบใช่งาน SLOT789