Just your basic poker hands. https://www.codeeval.com/open_challenges/86/
Started with the top hands then went to the lower ones. I use the positions before the decimal point to indicate the magnitude of the hand (royal flush being the greatest, singles being lowest). The positions after the decimal point are used to indicated additional subvalues that are required. IE two pairs can go down to the 2nd pair comparison or even to the single, thus need additional values. However, royal flush uses none of these additional slots because there can only be 1 royal flush, disregarding suit order.
Code:
var mapping = {
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'T': 10,
'J': 11,
'Q': 12,
'K': 13,
'A': 14
};
function Compare(line) {
var cards = line.split(" ");
var left = CheckHand(cards.slice(0,5));
var right = CheckHand(cards.slice(5));
if (left==right)
return "none";
else if (left > right)
return "left";
else
return "right";
}
function CheckHand(cards) {
cards.sort(function(x,y) {
return mapping[x.charAt(0)] > mapping[y.charAt(0)];
});
var straight = IsStraight(cards);
var flush = IsFlush(cards);
var cardCounts = CardCount(cards);
// just return straight flush
if (straight && flush) {
if (typeof cardCounts["12"]!=="undefined") {
// it is a royal flush
return 10;
}
return 9;
}
var pair1=0, pair2=0, triple=0;
var singles = [];
for (var i in cardCounts) {
if (cardCounts[i]==4) {
if (i<10)
return "8.0"+i; // four of a kind
return "8."+i;
} else if (cardCounts[i]==3) {
if (i<10)
triple="0"+i;
else
triple = i;
} else if (cardCounts[i]==2&&pair1==0) {
if (i<10)
pair1="0"+i; // one pair
else
pair1=i;
} else if (cardCounts[i]==2) {
if (i<10)
pair2="0"+i;
else
pair2=i;
} else { // singles
if (i<10) {
singles.push("0"+i);
} else {
singles.push(i);
}
}
}
// if hand is a fullhouse
if (pair1>0&&triple>0) {
if (triple<10)
return "7.0"+triple+pair1; // fullhouse (the triple first because that is the "high" since it is unique (you can't have 2 hands with three cards in the same round)
return "7."+triple+pair1;
} else if (flush) {
return "6.0"+singles.reverse().join(""); // flush
} else if (straight) {
// check if it was ace with a 2 (low)
if (singles[0]=="02")
return "5.05"; // straight
return "5."+singles.pop(); // straight
} else if (triple!=0) {
return "4."+triple+singles.pop()+singles.pop();
} else if (pair2!=0) {
return "3."+pair2+pair1+singles[0]; // two pair, + singles hand
} else if (pair1!=0) {
return "2."+pair1+singles.reverse().join(""); // one pair, + singles
}
return "1."+singles.reverse().join("");
}
function CardCount(cards) {
var counts = {};
for (var i=0, max = cards.length; max>i; i++) {
if (typeof counts[ mapping[cards[i].charAt(0)] ]=="undefined")
counts[ mapping[cards[i].charAt(0)] ]=0;
counts[ mapping[cards[i].charAt(0)] ]++;
}
return counts;
}
// assumes the hand is sorted already in ascending order
// ace is assumed to be sorted with value of 1
function IsStraight(cards) {
var start = mapping[cards[0].charAt(0)];
var max=cards.length;
var end = mapping[cards[max-1].charAt(0)];
if (end==14&&start==2)
{
for (var i=1; ii; i++) {
if (cards[i].charAt(1)!=suit)
return false;
}
return true;
}