Skip to content

Instantly share code, notes, and snippets.

@runofthemill
Created April 18, 2016 02:07
Show Gist options
  • Save runofthemill/acd852e2365ccdf6579c935523077633 to your computer and use it in GitHub Desktop.
Save runofthemill/acd852e2365ccdf6579c935523077633 to your computer and use it in GitHub Desktop.
var _ = require('lodash');
var hand = []
var input = [process.argv[2], process.argv[3], process.argv[4], process.argv[5], process.argv[6]]
var isSuited
var isStraight
var sortedHand = []
var cardOrder = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
var analyzeHand = function() {
input.forEach(function(card, i, array) {
var card = {
rank: input[i].slice(0,-1),
suit: input[i].slice(-1)
}
hand.push(card)
})
checkSuit()
sortHand()
checkStraight()
checkOther()
handStrength()
}
function checkSuit() {
isSuited = hand.every(function(card){
return hand[0].suit == card.suit
})
return isSuited
}
function sortHand() {
var temp = []
if (_.indexOf(_.map(hand,'rank'), '2') < 0)
cardOrder.push(cardOrder.shift())
_.forEach(hand, function(value, key) {
var i = _.indexOf(cardOrder, value.rank)
temp[i] = value
})
sortedHand = _.compact(temp)
}
function checkStraight() {
isStraight = true
for (var i = 0; i < sortedHand.length -1; i++) {
var j = i+1
if (_.indexOf(cardOrder, sortedHand[i].rank) != _.indexOf(cardOrder, sortedHand[j].rank) -1)
return isStraight = false
}
}
function checkOther() {
var groups = []
var temp = _.groupBy(hand, 'rank')
_.forEach(temp, function(value){
if (value.length > 1)
groups.push(value.length)
})
console.log(groups)
if (_.isMatch(groups, [4]))
console.log("Four of a kind!")
else if (_.isMatch(groups, [2,3]))
console.log('Full house!')
else if (_.isMatch(groups, [3]))
console.log('Three of a kind!')
else if (_.isMatch(groups, [2,2]))
console.log('Two pair!')
else if (_.isMatch(groups, [2]))
console.log('One pair!')
else
console.log('High card!')
}
function handStrength() {
if (isStraight && isSuited)
if (sortedHand[4] == 'A' && sortedHand[3] == 'K')
console.log('Royal flush!')
else
console.log('Straight flush!')
else if (isSuited)
console.log('Flush!')
else if (isStraight)
console.log('Straight!')
}
analyzeHand()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment