deck-of-cards-ts
Version:
Deck of cards package with TypeScript types.
50 lines • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("./constants");
class Deck {
cards = [];
numberOfFullDecks;
constructor(numberOfFullDecks = 1) {
this.numberOfFullDecks = numberOfFullDecks;
this.shuffle();
}
resetDeck() {
for (let i = 0; i < this.numberOfFullDecks; i++) {
this.cards.push(...this.getFullDeck());
}
}
getFullDeck() {
const cards = [];
for (let suit of constants_1.ALL_SUITS) {
for (let rank of constants_1.ALL_RANKS) {
const value = constants_1.RANK_TO_VALUE[rank];
cards.push({
suit,
rank,
value: Array.isArray(value) ? value[0] : value,
secondaryValue: Array.isArray(value) ? value[1] : null,
toString: () => `${rank} of ${suit}`,
});
}
}
return cards;
}
getCards() {
return [...this.cards];
}
shuffle() {
this.resetDeck();
for (let i = this.cards.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]];
}
}
draw() {
return this.cards.pop() ?? null;
}
isThisYourCard(cardOne, cardTwo) {
return cardOne.rank === cardTwo.rank && cardOne.suit === cardTwo.suit;
}
}
exports.default = Deck;
//# sourceMappingURL=deck.js.map