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.
98 lines
2.1 KiB
98 lines
2.1 KiB
class CardDeck { |
|
cardBack; |
|
|
|
constructor(cardBack = 'img/cardBack_red5.png') { |
|
this.generateDeck(); |
|
this.cardBack = cardBack; |
|
} |
|
|
|
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; |
|
|
|
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.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; |
|
} |
|
} |
|
|
|
|