cardation
Version:
fundation of card games, card model
42 lines (41 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const CardError_1 = require("../../error/CardError");
const SuitCard_1 = require("../card/SuitCard");
class Flush {
/**
* Test if the cards form a flush.
* @param {Card[]}cards cards to be tested
* @returns {boolean} true if the cards form a flush
*/
static isFlush(cards) {
const [firstCard] = cards;
const isNoiseExisting = cards.some((card) => {
if (!(card instanceof SuitCard_1.default)) {
return true;
}
if (card.getCardSuit() != firstCard.getCardSuit()) {
return true;
}
return false;
});
return !isNoiseExisting;
}
_suit;
_cards;
constructor(cards) {
const [firstCard] = cards;
if (!Flush.isFlush(cards)) {
throw new CardError_1.default('[Flush][constructor]: all the cards must be of the same suit to form a flush!');
}
this._suit = firstCard.getCardSuit();
this._cards = [...cards];
}
getCards() {
return [...this._cards];
}
getSuit() {
return this._suit;
}
}
exports.default = Flush;