|
|
|
|
class CardDeck {
|
|
|
|
|
cards;
|
|
|
|
|
|
|
|
|
|
constructor(vue) {
|
|
|
|
|
this.vue = vue;
|
|
|
|
|
this.generateDeck();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static create(vue) {
|
|
|
|
|
return new CardDeck(vue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static createAndShuffle(vue) {
|
|
|
|
|
let cardDeck = new CardDeck(vue);
|
|
|
|
|
cardDeck.shuffle();
|
|
|
|
|
|
|
|
|
|
return cardDeck;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateDeck() {
|
|
|
|
|
const colors = ['spades', 'clubs', 'diamonds', 'hearts'];
|
|
|
|
|
const highCards = ['j', 'q', 'k'];
|
|
|
|
|
|
|
|
|
|
let cardDeck = [];
|
|
|
|
|
|
|
|
|
|
colors.forEach((color) => {
|
|
|
|
|
for (let cardPoints = 1; cardPoints <= 10; cardPoints++) {
|
|
|
|
|
let symbol = cardPoints;
|
|
|
|
|
let points = cardPoints;
|
|
|
|
|
|
|
|
|
|
if (cardPoints === 1) {
|
|
|
|
|
points = 11;
|
|
|
|
|
symbol = 'a';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let card = new Card(
|
|
|
|
|
color,
|
|
|
|
|
symbol,
|
|
|
|
|
points
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
cardDeck.push(card);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
highCards.forEach((highCard) => {
|
|
|
|
|
let card = new Card(
|
|
|
|
|
color,
|
|
|
|
|
highCard,
|
|
|
|
|
10
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
cardDeck.push(card);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.cards = cardDeck;
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shuffle(runs = 1) {
|
|
|
|
|
let cardCount = this.cards.length, t, i;
|
|
|
|
|
|
|
|
|
|
for (let run = 0; run === runs; run++) {
|
|
|
|
|
while (cardCount) {
|
|
|
|
|
i = Math.floor(Math.random() * cardCount--);
|
|
|
|
|
t = this.cards[cardCount];
|
|
|
|
|
this.cards[cardCount] = this.cards[i];
|
|
|
|
|
this.cards[i] = t;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cardsInStack() {
|
|
|
|
|
return this.cards.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
deckIsEmpty() {
|
|
|
|
|
return this.cards.length > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
takeOneCard() {
|
|
|
|
|
if (this.cardsInStack() < 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.cards.shift();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
takeCards(cardsToDraw) {
|
|
|
|
|
let drawnCards = [];
|
|
|
|
|
|
|
|
|
|
for (let run = 0; run === cardsToDraw; run++) {
|
|
|
|
|
drawnCards.push(this.cards.shift());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return drawnCards;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|