Hi! I’m writing a basic BlackJack game. I splitted different classes into separate files. Now i have a Deck.js, with this code
export class Deck {
constructor() {
this.suits = ['Hearts', 'Clubs', 'Diamonds', 'Spades'];
this.values = ['Ace', 'King', 'Queen', 'Jack', 'Ten', 'Nine', 'Eight',
'Seven', 'Six', 'Five', 'Four', 'Three', 'Two'];
this.deck = [];
this.card = '';
this.cardValues = {
King: 10,
Queen: 10,
Jack: 10,
Ten: 10,
Nine: 9,
Eight: 8,
Seven: 7,
Six: 6,
Five: 5,
Four: 4,
Three: 3,
Two: 2
}
this.cardValue = 0;
}
//Creates the Deck and shuffles it
createDeck() {
this.deck = this.values
.map( value => this.suits
.map( suit => `${value} of ${suit}`))
.flat();
return this.deck;
}
shuffleDeck() {
var currentIndex = this.deck.length;
var temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = this.deck[currentIndex];
this.deck[currentIndex] = this.deck[randomIndex];
this.deck[randomIndex] = temporaryValue;
}
return this.deck;
}
getCard() {
let cardNum = Math.round(Math.random() * 51);
this.card = this.deck[cardNum];
this.deck.splice(cardNum, 1);
return this.card;
return this.deck;
}
getCardValue() {
const words = this.card.split(' ');
this.cardValue = this.cardValues[words[0]];
return this.cardValue;
}
resetDeck() {
this.createDeck();
this.shuffleDeck();
}
}
A Player.js
export class Player {
constructor() {
this.hasPlayerWon = false;
this.playerScore = 0;
this.playerCards = [];
}
}
And a index.js
import { Deck } from '/Deck.js';
import { Player } from '/Player.js';
const deck1 = new Deck();
const player1 = new Player();
deck1.createDeck();
I’m struggling to understand how, after calling deck.getCard(), i can push the card into playerCards array, considering that it’s in another class.
Any clue on how to?