Created
March 24, 2017 18:00
-
-
Save jglass/79c0c9e19ae5e138f238fb4f4af67205 to your computer and use it in GitHub Desktop.
Poker hand sorter w/ Lodash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _ = require("lodash"); | |
const suits = ["S", "H", "C", "D"]; | |
const values = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]; | |
createHand = (cards) => { | |
hand = []; | |
cards.forEach(function(card) { | |
hand.push( | |
{ | |
suit: card[1], | |
value: card[0], | |
display: card, | |
numericValue: values.indexOf(card[0]) + 1 | |
} | |
); | |
}); | |
sortCards(hand); | |
return hand; | |
}; | |
isStraightFlush = (sHand) => { | |
return isStraight(sHand) && isFlush(sHand); | |
}; | |
isFourOfAKind = (sHand) => { | |
let grouped = groupedVals(sHand); | |
return _.isEqual(_.sortBy(_.map(grouped, 'length')), [1, 4]); | |
}; | |
isFullHouse = (sHand) => { | |
let grouped = groupedVals(sHand); | |
return _.isEqual(_.sortBy(_.map(grouped, 'length')), [2, 3]); | |
}; | |
isFlush = (sHand) => { | |
let suits = _.map(sHand, 'suit'); | |
return _.uniq(suits).length === 1; | |
}; | |
isStraight = (sHand) => { | |
let numeric = _.map(sHand, 'numericValue'); | |
return _.uniq(numeric).length === numeric.length && (numeric[4] - numeric[0]) === 4; | |
}; | |
isThreeOfAKind = (sHand) => { | |
let grouped = groupedVals(sHand); | |
return _.isEqual(_.sortBy(_.map(grouped, 'length')), [1, 1, 3]); | |
}; | |
isTwoPair = (sHand) => { | |
let grouped = groupedVals(sHand); | |
return _.isEqual(_.sortBy(_.map(grouped, 'length')), [1, 2, 2]); | |
} | |
isOnePair = (sHand) => { | |
let grouped = groupedVals(sHand); | |
return _.isEqual(_.sortBy(_.map(grouped, 'length')), [1, 1, 1, 2]); | |
} | |
groupedVals = (sHand) => { | |
let numeric = _.map(sHand, 'numericValue'); | |
return _.values(_.groupBy(numeric)); | |
}; | |
sortCards = (hand) => { | |
_doSort(hand); | |
let tempRanks = _.map(hand, 'numericValue'); | |
if(_.isEqual(tempRanks, [1, 2, 3, 4, 13])) { | |
hand[4].numericValue = 0; | |
_doSort(hand); | |
} | |
return false; | |
}; | |
_doSort = (hand) => { | |
hand.sort((a, b) => { | |
return a.numericValue - b.numericValue; | |
}); | |
return false; | |
} | |
module.exports.createHand = createHand; | |
module.exports.sortCards = sortCards; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "poker_hands", | |
"version": "0.0.1", | |
"description": "Poker hands ranker", | |
"main": "index.js", | |
"scripts": { | |
"start": "electron .", | |
"test": "mocha --harmony" | |
}, | |
"author": "JGlass", | |
"license": "ISC", | |
"dependencies": { | |
"electron": "^1.6.2", | |
"lodash": "^4.17.4" | |
}, | |
"devDependencies": { | |
"chai": "^3.5.0", | |
"mocha": "^3.2.0" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chai = require('chai'); | |
const expect = require('chai').expect; | |
const index = require('./index.js'); | |
let hand; | |
describe('hand ranking', () => { | |
let hand = []; | |
describe('straight', () => { | |
it('ranks a straight correctly', function() { | |
hand = index.createHand(["5H", "4S", "3C", "6S", "7S"]); | |
expect(isStraight(hand)).to.equal(true); | |
}); | |
it('ranks the wheel correctly', () => { | |
hand = index.createHand(["4D", "5H", "2C", "3S", "AD"]); | |
expect(isStraight(hand)).to.equal(true); | |
}); | |
it('ranks a high straight correctly', () => { | |
hand = index.createHand(["AD", "KS", "TD", "JC", "QD"]); | |
expect(isStraight(hand)).to.equal(true); | |
}); | |
it('does not rank a non-straight as a straight', () => { | |
hand = index.createHand(["2D", "3H", "4C", "5S", "7D"]); | |
expect(isStraight(hand)).to.equal(false); | |
}); | |
}); | |
describe('flush', () => { | |
it('ranks a flush correctly', () => { | |
hand = index.createHand(["5H", "2H", "AH", "QH", "2H"]); | |
expect(isFlush(hand)).to.equal(true); | |
}); | |
it('does not rank a non-flush as a flush', () => { | |
hand = index.createHand(["2H", "3H", "4H", "5H", "7D"]); | |
expect(isFlush(hand)).to.equal(false); | |
}); | |
}); | |
describe('straight flush', () => { | |
it('ranks a straight flush correctly', () => { | |
hand = index.createHand(["8H", "9H", "TH", "JH", "QH"]); | |
expect(isStraightFlush(hand)).to.equal(true); | |
}); | |
it('does not rank a straight as a straight flush', () => { | |
hand = index.createHand(["2H", "3H", "4H", "5H", "6D"]); | |
expect(isStraightFlush(hand)).to.equal(false); | |
}); | |
it('does not rank a flush as a straight flush', () => { | |
hand = index.createHand(["2H", "3H", "4H", "5H", "7H"]); | |
expect(isStraightFlush(hand)).to.equal(false); | |
}); | |
}); | |
describe('four of a kind', () => { | |
it('identifies it correctly', () => { | |
hand = index.createHand(["8H", "8S", "JH", "8C", "8D"]); | |
expect(isFourOfAKind(hand)).to.equal(true); | |
}); | |
it('does not id 2 pair as 4 of a kind', () => { | |
hand = index.createHand(["8H", "8S", "JH", "JC", "6D"]); | |
expect(isFourOfAKind(hand)).to.not.equal(true); | |
}); | |
it('does not id full house as 4 of a kind', () => { | |
hand = index.createHand(["8H", "8S", "JH", "JC", "JD"]); | |
expect(isFourOfAKind(hand)).to.not.equal(true); | |
}); | |
}); | |
describe('full house', () => { | |
it('identifies it correctly', () => { | |
hand = index.createHand(["TS", "TH", "9D", "TC", "9C"]); | |
expect(isFullHouse(hand)).to.equal(true); | |
}); | |
it('does not id 3 of a kind as a full house', () => { | |
hand = index.createHand(["JS", "JH", "9D", "JC", "8C"]); | |
expect(isFullHouse(hand)).to.not.equal(true); | |
}); | |
it('does not id two pair as a full house', () => { | |
hand = index.createHand(["8H", "8S", "JH", "JC", "AD"]); | |
expect(isFullHouse(hand)).to.not.equal(true); | |
}); | |
}); | |
describe('three of a kind', () => { | |
it('identifies it correctly', () => { | |
hand = index.createHand(["JS", "JH", "9D", "JC", "8C"]); | |
expect(isThreeOfAKind(hand)).to.equal(true); | |
}); | |
it('does not id a full house as 3 of a kind', () => { | |
hand = index.createHand(["TS", "TH", "9D", "TC", "9C"]); | |
expect(isThreeOfAKind(hand)).to.not.equal(true); | |
}); | |
it('does not id two pair as 3 of a kind', () => { | |
hand = index.createHand(["2H", "2S", "JH", "4C", "4D"]); | |
expect(isThreeOfAKind(hand)).to.not.equal(true); | |
}); | |
}); | |
describe('two pair', () => { | |
it('identifies it correctly', () => { | |
hand = index.createHand(["2H", "2S", "JH", "4C", "4D"]); | |
expect(isTwoPair(hand)).to.equal(true); | |
}); | |
it('does not id a full house as 2 pair', () => { | |
hand = index.createHand(["TS", "TH", "9D", "TC", "9C"]); | |
expect(isTwoPair(hand)).to.not.equal(true); | |
}); | |
it('does not id 3 of kind as 2 pair', () => { | |
hand = index.createHand(["JS", "JH", "9D", "JC", "8C"]); | |
expect(isTwoPair(hand)).to.not.equal(true); | |
}); | |
}); | |
describe('one pair', () => { | |
it('identifies it correctly', () => { | |
hand = index.createHand(["2H", "2S", "JH", "5C", "4D"]); | |
expect(isOnePair(hand)).to.equal(true); | |
}); | |
it('does not id 2 pair as 1 pair', () => { | |
hand = index.createHand(["2H", "2S", "JH", "4C", "4D"]); | |
expect(isOnePair(hand)).to.not.equal(true); | |
}); | |
it('does not id 3 of kind as 1 pair', () => { | |
hand = index.createHand(["JS", "JH", "9D", "JC", "8C"]); | |
expect(isOnePair(hand)).to.not.equal(true); | |
}); | |
it('does not id a full house as 1 pair', () => { | |
hand = index.createHand(["TS", "TH", "9D", "TC", "9C"]); | |
expect(isOnePair(hand)).to.not.equal(true); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment