card-factory
Version:
A comprehensive library for card manipulation
49 lines (48 loc) • 1.33 kB
JavaScript
/**
* The cards in a Pile are a REFERENCE to the cards in the deck
*/
export default class Pile {
_cards;
name;
constructor(name, cards = []) {
this._cards = [...cards];
this.name = name;
}
get cards() {
return this._cards;
}
_getCardIndex = (card) => {
return this.cards.indexOf(card);
};
receiveCard = (cards, conditions = true) => {
if (conditions === false) {
return false;
}
if (Array.isArray(cards)) {
cards.forEach((card) => {
this._cards.push(card);
});
}
else {
this._cards.push(cards);
}
return true;
};
passCard = (target, card = this._cards[this._cards.length - 1], rules = true) => {
if (target.receiveCard(card, rules) === false)
return false;
else {
this._cards.splice(this._getCardIndex(card), 1);
return true;
}
};
shuffle = () => {
const copy = [...this._cards];
const shuffledPile = [];
for (let i = 0; i < this._cards.length; i++) {
const randomNum = Math.floor(Math.random() * copy.length);
shuffledPile.push(copy.splice(randomNum, 1)[0]);
}
this._cards = shuffledPile;
};
}