blackjack logic written in vue
https://luna-development.net/blackjack/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.1 KiB
100 lines
2.1 KiB
|
6 years ago
|
class CardDeck {
|
||
|
|
constructor() {
|
||
|
|
this.generateDeck();
|
||
|
|
}
|
||
|
|
|
||
|
|
create() {
|
||
|
|
return new CardDeck();
|
||
|
|
}
|
||
|
|
|
||
|
|
createAndShuffle() {
|
||
|
|
let cardDeck = new CardDeck();
|
||
|
|
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;
|
||
|
|
let isAnAce = false;
|
||
|
|
|
||
|
|
if (cardPoints === 1) {
|
||
|
|
points = 11;
|
||
|
|
symbol = 'a';
|
||
|
|
isAnAce = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
let card = new Card(
|
||
|
|
color,
|
||
|
|
symbol,
|
||
|
|
points,
|
||
|
|
isAnAce
|
||
|
|
)
|
||
|
|
|
||
|
|
cardDeck.push(card);
|
||
|
|
}
|
||
|
|
|
||
|
|
highCards.forEach((highCard) => {
|
||
|
|
let card = new Card(
|
||
|
|
color,
|
||
|
|
highCard,
|
||
|
|
10,
|
||
|
|
false
|
||
|
|
)
|
||
|
|
|
||
|
|
cardDeck.push(card);
|
||
|
|
});
|
||
|
|
|
||
|
|
this.deck = cardDeck;
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
shuffle(runs = 1) {
|
||
|
|
let cardCount = this.deck.length, t, i;
|
||
|
|
|
||
|
|
for (let run = 0; run === runs; run++) {
|
||
|
|
while (cardCount) {
|
||
|
|
i = Math.floor(Math.random() * cardCount--);
|
||
|
|
t = this.deck[cardCount];
|
||
|
|
this.deck[cardCount] = this.deck[i];
|
||
|
|
this.deck[i] = t;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
cardsInStack() {
|
||
|
|
return this.deck.length;
|
||
|
|
}
|
||
|
|
|
||
|
|
deckIsEmpty() {
|
||
|
|
return this.deck.length > 0 ? true : false;
|
||
|
|
}
|
||
|
|
|
||
|
|
takeOneCard(cardsToDraw = 1) {
|
||
|
|
if (this.cardsInStack() > 0) {
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
return this.deck.shift();
|
||
|
|
}
|
||
|
|
|
||
|
|
takeCards(cardsToDraw) {
|
||
|
|
let drawnCards = [];
|
||
|
|
|
||
|
|
for (let run = 0; run === cardsToDraw; run++) {
|
||
|
|
drawnCards.push(this.deck.shift());
|
||
|
|
}
|
||
|
|
|
||
|
|
return drawnCards;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|