Skip to content

Instantly share code, notes, and snippets.

@vietkute02
Created November 19, 2014 09:10
Show Gist options
  • Save vietkute02/a60386440c5401e54fd5 to your computer and use it in GitHub Desktop.
Save vietkute02/a60386440c5401e54fd5 to your computer and use it in GitHub Desktop.
Hand
function Hand(cards) {
this.cards = cards.map(function(card) { return new Card(card); });
this.sortCards();
this.organized = this.organizeHand();
}
Hand.prototype.sortCards = function() {
this.cards.sort(function(a, b) {
return a.getValue() - b.getValue();
});
};
Hand.prototype.organizeHand = function() {
var organized = {
suits: {
spades: [],
clubs: [],
hearts: [],
diamonds: []
},
values: {
2: [], 3: [], 4: [], 5: [], 6: [],
7: [], 8: [], 9: [], 10: [], 11: [],
12: [], 13: [], 14: []
}
};
this.cards.forEach(function(card) {
organized.suits[card.getSuit() + 's'].push(card);
organized.values[card.getValue()].push(card);
});
return organized;
};
Hand.prototype.isRoyalFlush = function() {
if (!this.isStraight() || !this.isFlush()) { return false; }
var vals = this.cards.map(function(card) { return card.getValue(); });
return this.getHighCard() === 14;
};
Hand.prototype.isStraightFlush = function() {
return this.isFlush() && this.isStraight();
};
Hand.prototype.isQuads = function() {
return this.getSameValueCount() === 4;
};
Hand.prototype.isFullHouse = function() {
var hasTrips = false,
hasPair = false,
_this = this;
Object.keys(this.organized.values).forEach(function(key) {
if (_this.organized.values[key].length === 2) { hasPair = true; }
if (_this.organized.values[key].length === 3) { hasTrips = true; }
});
return hasPair && hasTrips;
};
Hand.prototype.isFlush = function() {
return this.getSameSuitCount() === 5;
};
Hand.prototype.isStraight = function() {
var vals = this.cards.map(function(card) { return card.getValue(); }),
previousCard = 0;
return vals.every(function(val) {
if (!previousCard || (previousCard + 1 === val || (val === 14 && previousCard === 5))) {
previousCard = val;
return true;
} else {
return false;
}
});
};
Hand.prototype.isTrips = function() {
return this.getSameValueCount() === 3;
};
Hand.prototype.isTwoPair = function() {
var pairCount = 0,
_this = this;
Object.keys(this.organized.values).forEach(function(key) {
if (_this.organized.values[key].length > 2) { pairCount++; }
});
return pairCount === 2;
};
Hand.prototype.isPair = function() {
return this.getSameValueCount() === 2;
};
Hand.prototype.getHighCard = function() {
var vals = this.cards.map(function(card) { return card.getValue(); });
return Math.max.apply(null, vals);
};
Hand.prototype.getSameValueCount = function() {
var sameValueCount = 0,
_this = this;
Object.keys(this.organized.values).forEach(function(key) {
if (_this.organized.values[key].length > sameValueCount) {
sameValueCount = _this.organized.values[key].length;
}
});
return sameValueCount;
};
Hand.prototype.getSameSuitCount = function() {
var sameSuitCount = 0,
_this = this;
Object.keys(this.organized.suits).forEach(function(key) {
if (_this.organized.suits[key].length > sameSuitCount) {
sameSuitCount = _this.organized.suits[key].length;
}
});
return sameSuitCount;
};
Hand.prototype.isPotentialStraight = function(cardsRequired) {
var vals = this.cards.map(function(card) { return card.getValue(); }),
previousCard = 0,
matches = 0;
// this could use some work
vals.forEach(function(val) {
if (~vals.indexOf(val + 1)) matches++;
});
return matches >= cardsRequired;
};
Hand.prototype.isPotentialFlush = function(cardsRequired) {
return this.getSameSuitCount() >= cardsRequired;
};
Hand.prototype.getPotential = function(state) {
switch (state) {
case 'pre-flop':
if (this.isPair() && this.getHighCard() >= 10) return 100;
if (this.getHighCard() >= 10) return -1;
if (this.isPotentialFlush(2)) return -1;
if (this.isPotentialStraight(2)) return -1;
break;
case 'flop':
if (this.isPotentialFlush(4)) return -1;
if (this.isPotentialStraight(4)) return -1;
break;
case 'river':
if (this.isPotentialFlush(4)) return -1;
if (this.isPotentialStraight(4)) return -1;
break;
case 'turn':
if (this.isPair() && this.getHighCard() >= 12) return -1;
break;
}
return 0;
};
Hand.prototype.getStrength = function(state) {
if (this.isRoyalFlush()) return 100;
if (this.isStraightFlush()) return 99;
if (this.isQuads()) return 95;
if (this.isFullHouse()) return 80;
if (this.isFlush()) return 60;
if (this.isStraight()) return 0;
if (this.isTrips()) return 0;
if (this.isTwoPair()) return 0;
return this.getPotential(state);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment