get5cardPokerScore

Creates an equivalence score for a 5 card Poker hand that can be used to compare hands.

HTML

<div id="output"/>

JavaScript

output = document.getElementById('output');
var A=14, K=13, Q=12, J=11, _ = { "♠":1, "♣":2, "♥":4, "♦":8 };
var names = [0,0,2,3,4,5,6,7,8,9,10,"Jack","Queen","King","Ace"];

function get5cardScore(cs,ss){ //calculates the equivalence score of 5 cards
	var v, i, o, c, d={}, s = 1<<cs[0]|1<<cs[1]|1<<cs[2]|1<<cs[3]|1<<cs[4];
  	for (i=v=o=0; i<5; i++) {v += (o=Math.pow(2,cs[i]*4))*(d[cs[i]] = (v/o&15)+1);}
  	v = v%15-((s/(s&-s)==31)||(s==0x403c)?3:1)-(ss[0]==(ss[1]|ss[2]|ss[3]|ss[4]))*((s==0x7c00)?-5:1);
    c = (s==0x403c)?[5,4,3,2,1]:cs.slice().sort(function(a,b){return (d[a]<d[b])?1:(d[a]>d[b])?-1:(b-a);});
	return [7,8,4,5,0,1,2,9,3,6][v]<<20|c[0]<<16|c[1]<<12|c[2]<<8|c[3]<<4|c[4];
}

function getTypeDetail(x){
	var cat = x>>20, c1 = x>>16&15, c3 = x>>8&15,c4 = x>>4&15;
 
	switch(cat){
		case 0: return names[c1] + " high";
		case 1: return "Pair of "+names[c1]+"s";
		case 2: return "Two pair, "+names[c1]+"s and "+names[c3]+"s";
		case 3: return "Three of a kind, "+names[c1]+"s";
		case 4: return names[c1] +" high straight";
		case 5: return names[c1] +" high flush";
		case 6: return names[c1] + "s full of "+names[c4]+"s";
		case 7: return "Four of a kind, "+names[c1]+"s";
		case 8: return names[c1]+" high straight flush";
		case 9: return "Royal flush (holy sh*t)";	
	}
}

var h01 = get5cardScore( [ 10, J, Q, K, A], [_["♠"],_["♠"],_["♠"],_["♠"],_["♠"] ] );// Royal Flush
var h02 = get5cardScore( [ 10, J, Q, K, 9 ],[_["♠"],_["♠"],_["♠"],_["♠"],_["♠"] ] );// Straight Flush
var h03 = get5cardScore( [ 2, 3, 4, 5, A],  [_["♠"],_["♠"],_["♠"],_["♠"],_["♠"] ] );// Straight Flush (♠ low)
var h04 = get5cardScore( [ 8, 8, 8, 8, 9 ], [_["♠"],_["♣"],_["♥"],_["♦"],_["♠"] ] );// 4 of a Kind
var h05 = get5cardScore( [ A, A, A, 9, 9 ], [_["♠"],_["♣"],_["♥"],_["♠"],_["♣"] ] );// Full house
var h06 = get5cardScore( [ 10, J, 6, K, 9 ],[_["♣"],_["♣"],_["♣"],_["♣"],_["♣"] ] );// Flush
var h07 = get5cardScore( [ 10, J, Q, 8, 9 ],[_["♠"],_["♣"],_["♥"],_["♣"],_["♦"] ] );// Straight
var...