card-games-utils
Version:
Utility package for card games
228 lines (227 loc) • 8.92 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TeenPatti = void 0;
var TeenPattiEnum_1 = require("../constants/TeenPattiEnum");
var StandardCardHelper_1 = require("../helpers/StandardCardHelper");
/**
* Class to handle operations of TeenPatti game
* @class
*/
var TeenPatti = /** @class */ (function () {
function TeenPatti() {
}
/**
* @method
* @static
* Calculates the Teen Patti hand based on an array of three cards.
*
* @param {[StandardCard, StandardCard, StandardCard]} cards - The array of three cards to be used for the hand calculation.
* @returns {keyof typeof TeenPattiHand} - The type of Teen Patti hand that is formed by the given cards.
*/
TeenPatti.calculateHand = function (cards) {
var sortedCards = StandardCardHelper_1.StandardCardHelper.sortCards(cards);
// check for trail condition
if (sortedCards[0].number === sortedCards[1].number &&
sortedCards[1].number === sortedCards[2].number) {
return TeenPattiEnum_1.TeenPattiHand.TRAIL;
}
// check for pure sequence
if (
// they need to be from same suite
sortedCards[0].suite === sortedCards[1].suite &&
sortedCards[0].suite === sortedCards[2].suite) {
// checking for normal sequence
if (sortedCards[0].number + 1 === sortedCards[1].number &&
sortedCards[1].number + 1 === sortedCards[2].number) {
return TeenPattiEnum_1.TeenPattiHand.PURE_SEQUENCE;
}
else {
if (
// 1 exception check for Q(12),K(13),A(1/14)
sortedCards[0].number === 12 &&
sortedCards[1].number === 13 &&
sortedCards[2].number === 14) {
return TeenPattiEnum_1.TeenPattiHand.PURE_SEQUENCE;
}
}
}
// checking for normal sequence
if (sortedCards[0].number + 1 === sortedCards[1].number &&
sortedCards[1].number + 1 === sortedCards[2].number) {
return TeenPattiEnum_1.TeenPattiHand.SEQUENCE;
}
else {
if (
// 1 exception check for Q,K,A
sortedCards[0].number === 12 &&
sortedCards[1].number === 13 &&
sortedCards[2].number === 14) {
return TeenPattiEnum_1.TeenPattiHand.SEQUENCE;
}
}
// checking for color
if (sortedCards[0].suite === sortedCards[1].suite &&
sortedCards[0].suite === sortedCards[2].suite) {
return TeenPattiEnum_1.TeenPattiHand.COLOR;
}
// checking for pair
if (sortedCards[0].number === sortedCards[1].number ||
sortedCards[1].number === sortedCards[2].number) {
return TeenPattiEnum_1.TeenPattiHand.PAIR;
}
// finally, if not any other hand found, returning High
return TeenPattiEnum_1.TeenPattiHand.HIGH;
};
/**
* @method
* @static
* convert the given array of 3 cards into Hand interface
*
* @param {[StandardCard,StandardCard,StandardCard]} cards - the array of cards to be sorted
* @returns {Hand} - the sorted array of cards
*/
TeenPatti.makeHand = function (cards) {
var hand = {
cards: cards,
hand: this.calculateHand(cards),
};
return hand;
};
/**
* @method
* @static
* Calculates the winner(s) from the array of hands given based on Teen Patti rules
* winner could be multiple if players have identical cards with different suites
*
* @param {Hand[]} hands - The array of hands to be used for winner calculation.
* @returns {number[]} - array of indexes of the winner(s) in the array given
*/
TeenPatti.calculateWinners = function (hands) {
var _this = this;
if (hands.length < 2) {
throw new Error('need at least 2 hand to compare');
}
var highestHand;
var handsPriority = [
TeenPattiEnum_1.TeenPattiHand.HIGH,
TeenPattiEnum_1.TeenPattiHand.PAIR,
TeenPattiEnum_1.TeenPattiHand.COLOR,
TeenPattiEnum_1.TeenPattiHand.SEQUENCE,
TeenPattiEnum_1.TeenPattiHand.PURE_SEQUENCE,
TeenPattiEnum_1.TeenPattiHand.TRAIL,
];
hands.forEach(function (hand) {
if (highestHand == null) {
highestHand = hand;
}
if (handsPriority.indexOf(hand.hand) >= handsPriority.indexOf(highestHand.hand)) {
highestHand = hand;
}
});
var handsToCompare = hands.filter(function (hand) {
if (hand.hand === (highestHand === null || highestHand === void 0 ? void 0 : highestHand.hand)) {
return true;
}
return false;
});
if (handsToCompare.length === 1) {
return [hands.indexOf(handsToCompare[0])];
}
var winnerHands = [];
var tempWinnerHand = handsToCompare[0];
handsToCompare.forEach(function (hand) {
if (tempWinnerHand === null) {
tempWinnerHand = hand;
}
else if (tempWinnerHand !== hand) {
try {
if (_this.isRankHigher(hand, tempWinnerHand)) {
winnerHands = [];
tempWinnerHand = hand;
}
}
catch (error) {
winnerHands.push(tempWinnerHand);
tempWinnerHand = hand;
}
}
});
if (tempWinnerHand != null) {
winnerHands.push(tempWinnerHand);
}
var winnerIndexes = [];
winnerHands.forEach(function (hand) {
winnerIndexes.push(hands.indexOf(hand));
});
return winnerIndexes;
};
/**
* @method
* @static
* decide if the rank of first given card is higher then second given card
* this method should only be used when you have cards with same TeenPattiHand and you want to know the higher ranking
*
* @param {Hand} hand - the Hand that needs to be compared
* @param {Hand} handToCompare - the Hand to compare with
* @returns {boolean} - boolean showing is the card higher or not
* @throws {Error} - when the comparison results in a tie
*/
TeenPatti.isRankHigher = function (hand, handToCompare) {
// first checking if any card in each hand has the ace in it, if yes then changing it's number from 1 to 14(since ace should be the highest while comparing)
var cards = hand.cards.map(function (card) {
if (card.number === 1) {
var modifiedCard = __assign(__assign({}, card), { number: 14 });
return modifiedCard;
}
else {
return card;
}
});
var cardsToCompare = handToCompare.cards.map(function (card) {
if (card.number === 1) {
var modifiedCard = __assign(__assign({}, card), { number: 14 });
return modifiedCard;
}
else {
return card;
}
});
// sorting cards is ascending order, so lowest card will be first
cards = StandardCardHelper_1.StandardCardHelper.sortCards(cards);
cardsToCompare = StandardCardHelper_1.StandardCardHelper.sortCards(cardsToCompare);
// after sorting, we start comparing each card(from highest to lowest) to see who has the highest card
if (cards[2].number > cardsToCompare[2].number) {
return true;
}
else if (cardsToCompare[2].number > cards[2].number) {
return false;
}
if (cards[1].number > cardsToCompare[1].number) {
return true;
}
else if (cardsToCompare[1].number > cards[1].number) {
return false;
}
if (cards[0].number > cardsToCompare[0].number) {
return true;
}
else if (cardsToCompare[0].number > cards[0].number) {
return false;
}
// if cards are identical in numbers, then it will be considered TIE and throw an error
throw new Error('Tie occurred in the comparison of the two hands');
};
return TeenPatti;
}());
exports.TeenPatti = TeenPatti;