Skip to content

Instantly share code, notes, and snippets.

@JohnnyBizzel
Last active March 30, 2018 22:22
Show Gist options
  • Save JohnnyBizzel/8a613940def285af55a65845ebde21cf to your computer and use it in GitHub Desktop.
Save JohnnyBizzel/8a613940def285af55a65845ebde21cf to your computer and use it in GitHub Desktop.
Poker
var Result = { "win": 1, "loss": 2, "tie": 3 }
function PokerHand(hand) {
this.hand = hand;
}
var CardValues = { "A": 14, "K": 13, "Q": 12, "J": 11, "T": 10, "9": 9,
"8": 8, "7": 7, "6": 6, "5": 5, "4": 4, "3": 3, "2": 2 }
// TODO: Rank Hand against all possible hand ranks, then compareWith
function compareNums(a, b) {
return a - b;
}
function getDiff(numArray) {
return Math.max(...numArray) - Math.min(...numArray);
}
function cardOrder(theHand) {
var cardVals = theHand.map(function(card) {
return CardValues[card[0]]
});
return cardVals.sort(compareNums);
}
function checkConsecutive(cardVals) {
// compare numbers for any out of sequence
var startNo = cardVals[0];
for (let i = 0; i < 4; i++) {
if (startNo != cardVals[i]) return false;
startNo++; // counts sequentially
}
return true;
}
function checkStraight(theHand) {
// check bottom straight
var checkHand =cardOrder(theHand);
if (checkHand[0] === 2 && checkHand[1] === 3 && checkHand[2] === 4 &&
checkHand[3] === 5 && checkHand[4] === 14)
return true;
return getDiff(cardOrder(theHand)) === 4 ? checkConsecutive(cardOrder(theHand)) : false;
}
function checkFlush(theHand) {
var suitsFound = '';
theHand.map(function(card) {
if (suitsFound.indexOf(card[1]) === -1)
suitsFound += card[1];
});
return (suitsFound.length === 1) ? true : false;
}
function checkCards(theHand) {
var flush = checkFlush(theHand);
var str8 = checkStraight(theHand);
var str8flush = (flush && str8) ? true : false;
var fourKind = false;
var fullHs = false;
var fourVal = 0;
var threeKind = false;
var threeVal = 0;
var twoPair = false;
var pairCount = 0;
var pair = false;
var pairVal = 0;
var nextCardVal = 0; // nextSignificantCard
var hs = {};
theHand.map(function(card) {
if( hs[card[0]] > 0) {
hs[card[0]] += 1;
} else {
hs[card[0]] = 1;
}
});
if (!flush && !str8) {
for (var key in hs) {
if (hs[key] === 4) {
fourKind = true;
fourVal = CardValues[key];
break;
}
if (hs[key] === 3) {
threeKind = true;
threeVal = CardValues[key];
}
if (hs[key] === 2) {
pair = true;
pairVal = CardValues[key] > pairVal ? CardValues[key] : pairVal;
pairCount ++;
}
}
if (pair && threeKind) fullHs = true;
if (pairCount === 2) twoPair = true;
}
// Work out next significant card for edge cases
if (str8flush) {
if (cardOrder(theHand)[4] === 14 && cardOrder(theHand)[3] === 5)
return { "handStrength": 9, "highCard": 5 }; // bottom straight
// any other straight
return { "handStrength": 9, "highCard": cardOrder(theHand)[4]};
}
if (fourKind) {
for (var key in hs) {
// the final card value
if (hs[key] === 1)
nextCardVal = CardValues[key];
}
return { "handStrength": 8, "highCard": fourVal, "otherCard" : nextCardVal };
}
if (fullHs) {
for (var key in hs) {
// the final card value
if (hs[key] === 2)
nextCardVal = CardValues[key];
}
return { "handStrength": 7, "highCard": threeVal, "otherCard" : nextCardVal };
}
if (flush)
return { "handStrength": 6, "allCards": cardOrder(theHand) };
if (str8) {
if (cardOrder(theHand)[4] === 14 && cardOrder(theHand)[3] === 5)
return { "handStrength": 5, "highCard": 5 }; // bottom straight
// any other straight
return { "handStrength": 5, "highCard": cardOrder(theHand)[4] };
}
if (threeKind) {
let remainingCards = [];
for (var key in hs) {
// the other cards
if (hs[key] != 3) {
if (CardValues[key] > nextCardVal) {
remainingCards.push(CardValues[key]);
} else {
remainingCards.unshift(CardValues[key]);
}
}
}
return { "handStrength": 4, "highCard": threeVal, "allCards": remainingCards };
}
if (twoPair) {
var high = 0;
var low = 0;
for (var key in hs) {
if (hs[key] === 2) {
if (CardValues[key] > high) {
low = high;
high = CardValues[key];
if (low > high) {
var temp = low;
high = low;
low = temp;
}
} else {
low = CardValues[key];
}
}
if (hs[key] === 1)
nextCardVal = CardValues[key];
}
var handCardVals = cardOrder(theHand);
return { "handStrength": 3, "highestPair": high, "lowestPair": low, "otherCard" : nextCardVal };
}
if (pair) {
let remainingCards = [];
for (var key in hs) {
// the other cards
if (hs[key] === 1) {
if (CardValues[key] > nextCardVal) {
remainingCards.push(CardValues[key]);
} else {
remainingCards.unshift(CardValues[key]);
}
}
}
return { "handStrength": 2, "highCard": pairVal, "allCards": remainingCards.sort(compareNums) };
}
// high card
return { "handStrength": 1, "allCards": cardOrder(theHand) };
//return handStrength.push(1, cardOrder(theHand)[4]);
}
PokerHand.prototype.compareWith = function(opp){
// Start your coding here...
var myHand = this.hand.split(' ');
var opHand = opp.hand.split(' ');
var my = checkCards(myHand);
var op = checkCards(opHand);
console.log(JSON.stringify(my));
console.log(JSON.stringify(op));
if (my["handStrength"] > op["handStrength"]) {
return Result.win;
} else if (my["handStrength"] === op["handStrength"]) {
switch (my["handStrength"]) {
case 9: // straight flush
if (my["highCard"] > op["highCard"]) {
return Result.win;
} else if (my["highCard"] < op["highCard"]) {
return Result.loss;
} else {
return Result.tie;
}
return Result.tie;
break;
case 8: // 4 of a kind
if (my["highCard"] > op["highCard"]) {
return Result.win;
} else if (my["highCard"] < op["highCard"]) {
return Result.loss;
} else if (my["otherCard"] > op["otherCard"]) {
return Result.win;
} else if (my["otherCard"] < op["otherCard"]) {
return Result.loss;
}
return Result.tie;
break;
case 7: // full house
if (my["highCard"] > op["highCard"]) {
return Result.win;
} else if (my["highCard"] < op["highCard"]) {
return Result.loss;
} else if (my["otherCard"] > op["otherCard"]) {
return Result.win;
} else if (my["otherCard"] < op["otherCard"]) {
return Result.loss;
}
return Result.tie;
break;
case 6: // flush
for (let i = 4; i >= 0; i--) {
if (my["allCards"][i] > op["allCards"][i]) {
return Result.win;
} else if (my["allCards"][i] < op["allCards"][i]) {
return Result.loss;
}
}
return Result.tie;
break;
case 5: // Straight...
if (my["highCard"] > op["highCard"]) {
return Result.win;
} else if (my["highCard"] < op["highCard"]) {
return Result.loss;
} else {
return Result.tie;
}
break;
case 4: // three of kind
if (my["highCard"] > op["highCard"]) {
return Result.win;
} else if (my["highCard"] < op["highCard"]) {
return Result.loss;
} else {
for (let i = 2; i >= 0; i--) {
if (my["allCards"][i] > op["allCards"][i]) {
return Result.win;
} else if (my["allCards"][i] < op["allCards"][i]) {
return Result.loss;
}
}
return Result.tie;
}
break;
case 3: // 2 pairs
if (my["highestPair"] > op["highestPair"]) {
return Result.win;
} else if (my["highestPair"] < op["highestPair"]) {
return Result.loss;
} else if (my["lowestPair"] > op["lowestPair"]) {
return Result.win;
} else if (my["lowestPair"] < op["lowestPair"]) {
return Result.loss;
} else if (my["otherCard"] > op["otherCard"]) {
return Result.win;
} else if (my["otherCard"] < op["otherCard"]) {
return Result.loss;
}
return Result.tie;
break;
case 2: // 1 pair
if (my["highCard"] > op["highCard"]) {
return Result.win;
} else if (my["highCard"] < op["highCard"]) {
return Result.loss;
} else {
for (let i = 2; i >= 0; i--) {
if (my["allCards"][i] > op["allCards"][i]) {
return Result.win;
} else if (my["allCards"][i] < op["allCards"][i]) {
return Result.loss;
}
}
}
return Result.tie;
break;
case 1: // high card
for (let i = 4; i >= 0; i--) {
if (my["allCards"][i] > op["allCards"][i]) {
return Result.win;
} else if (my["allCards"][i] < op["allCards"][i]) {
return Result.loss;
}
}
return Result.tie;
break;
}
} else {
return Result.loss;
}
// should be unreachable
return Result.tie;
}
var player = "4H 5H 6H TH AH";
var opponent = "3S 3H 3C TS 3D";
var p = new PokerHand(player);
var o = new PokerHand(opponent);
p.compareWith(o)
document.getElementById("app").innerHTML = JSON.stringify(p.compareWith(o));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment