poker-evaluator
Version:
A library to evaluate 3, 5, 6 or 7 card poker hands
58 lines • 2.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var constants_1 = require("./constants");
// TODO update to allow number input
var ThreeCardConverter = /** @class */ (function () {
function ThreeCardConverter() {
}
// TODO refactor + extract to private functions
ThreeCardConverter.fillHand = function (cards) {
cards = tslib_1.__spreadArrays(cards);
var cardsUsed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
// Convert each card to vals 0 - 12, strip suit
cards.forEach(function (card) {
var index = Math.floor((constants_1.DECK[card.toLowerCase()] - 1) / 4);
cardsUsed[index] = 1;
});
var toFill = 2; // Need to fill 2 cards
// Fill in <toFill> cards to complete 5 card hand
for (var i = 0; i < cardsUsed.length - 1; i++) {
if (toFill === 0)
break; // Done filling
// Prevent adding a card to finish a straight
if (cardsUsed[i] === 0 && !this.makesStraight(cardsUsed, i)) {
cardsUsed[i] = 2;
toFill--;
}
}
// Fill dummy cards for lowest possible hand
var suit = ['s', 'd'];
cardsUsed.forEach(function (cardUsedValue, i) {
if (cardUsedValue === 2) {
var card = constants_1.CARD_RANKS[i] + suit[0];
suit.splice(0, 1); // remove 's' from suit (so 'd' is suit[0] next time)
cards.push(card);
}
});
return cards;
};
ThreeCardConverter.makesStraight = function (cardsUsed, rank) {
// Add ace to bottom as well
var newCards = [cardsUsed[cardsUsed.length - 1]].concat(cardsUsed);
// Add in new card (pushed up one by ace)
newCards[rank + 1] = 2;
// Determine if there are 5 cards in a row
return 5 === newCards.reduce(function (prev, next) {
if (prev === 5) {
return 5;
}
else {
return next ? prev + 1 : 0;
}
});
};
return ThreeCardConverter;
}());
exports.default = ThreeCardConverter;
//# sourceMappingURL=three-card-converter.js.map