Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

7 Card Poker Hand Evaluator

People also ask

How many 7 poker cards combinations exist?

hands of two pairs of the second type. Adding the two gives 31,433,400 7-card hands with two pairs. hands with a pair.

How many 7-card poker hands are there?

Eliminating identical hands that ignore relative suit values leaves 6,009,159 distinct 7-card hands. The number of distinct 5-card poker hands that are possible from 7 cards is 4,824.

Does a 7-card straight beat a flush?

A straight is a hand that contains five cards of sequential rank, not all of the same suit, such as 7♣ 6♠ 5♠ 4♥ 3♥ (a "seven-high straight"). It ranks below a flush and above three of a kind.


I wrote one in JavaScript. The core evaluating method uses only bit manipulations so is extremely fast. With this in mind, looking at 21 combinations is still very fast. The only time we need to go deeper is when a tie occurs. When this happens, we need to look into more details to see which 5 card hand is actually the best. Here is the solution I came up with:

hands=["4 of a Kind", "Straight Flush", "Straight", "Flush", "High Card",
       "1 Pair", "2 Pair", "Royal Flush", "3 of a Kind", "Full House" ];
var A=14, K=13, Q=12, J=11, _ = { "♠":1, "♣":2, "♥":4, "♦":8 };

//Calculates the Rank of a 5 card Poker hand using bit manipulations.
function rankPokerHand(cs,ss) {
  var v, i, o, s = 1<<cs[0]|1<<cs[1]|1<<cs[2]|1<<cs[3]|1<<cs[4];
  for (i=-1, v=o=0; i<5; i++, o=Math.pow(2,cs[i]*4)) {v += o*((v/o&15)+1);}
  v = v % 15 - ((s/(s&-s) == 31) || (s == 0x403c) ? 3 : 1);
  v -= (ss[0] == (ss[1]|ss[2]|ss[3]|ss[4])) * ((s == 0x7c00) ? -5 : 1);

  document.write("Hand: "+hands[v]+((s == 0x403c)?" (Ace low)":"")+"<br/>");
}

//Royal Flush   
rankPokerHand( [ 10, J, Q, K, A],  [ _["♠"], _["♠"], _["♠"], _["♠"], _["♠"] ] ); 

Explanation Here
Demo Here


This site lists a bunch of Poker Hand Evaluator libraries and gives a few details about each of them. Most of them are for 5 card hands, but there is at least one for a 7 card hand called The Snezee7 Evaluator. Plus the site give a great overview of the different techniques and algorithms used to analyze poker hands quickly.

I've used the Keith Rule C# Port of the Pokersource Evaluator in a few different poker projects and think that it is an excellent library. There are many clever tricks you can use to make really fast hand evaluators, but writing the code is a lot of work and I would highly suggest using an existing library.


Glad you asked :) Yes, here's a brand new solution that may be just the ticket:

Code: http://code.google.com/p/specialkpokereval/
Blog: http://specialk-coding.blogspot.com/2010/04/texas-holdem-7-card-evaluator_23.html

A commercial-grade evolution of this evaluator is available for the iPhone/iPod Touch via iTunes Store. It's called "Poker Ace".

An excellent summary of various solutions complete with links is found on James Devlin's blog "Coding The Wheel".

One evaluator not yet discussed there is Klaatu's.

Good luck!


I developed an algorithm for 7-card hand evaluation without iterating all 21 combinations.

Basically, it splits the 7-card hand into two categories: flush and not a flush. If it's a flush, it would be easy to look up the value in a table of 8192 entries. If it's not a flush, it'll run a hash function with techniques of dynamic programming, and then look up the value in a hash table of 49205 entries.

If you are interested, please check my work at github.

https://github.com/HenryRLee/PokerHandEvaluator