Does anyone know a fast algorithm for evaluating 7 card poker hands? Something which is more efficient than simply brute-force checking a every 21 5-card combination of hands from a set of 7.
Cheers,
Pete
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], [ _["♠"], _["♠"], _["♠"], _["♠"], _["♠"] ] );