JSFiddle - React, Tailwind, and code Playground
JavaScript
Player = function(wonRound, currentCards, newCards) {
this.wonRound = wonRound;
this.currentCards = currentCards;
this.newCards = newCards;
}
Deck = {
suits: ["Clubs", "Diamonds", "Hearts", "Spades"],
cards: ["Ace", "King", "Queen", "Jack", "10", "9", "8", "7", "6", "5", "4", "3", "2"],
deck: [],
shuffledDeck: [],
TempshuffledDeck: [],
BuildDeck: function() {
//Builds the deck
for (var suit = 0; suit < this.suits.length; suit++) {
for (var card = 0; card < this.cards.length; card++) {
this.deck.push([this.cards[card], this.suits[suit]]);
}
}
console.log("DECK len" + this.deck.length);
return this.deck;
},
ShuffleDeck: function() {
var i = this.deck.length;
var j = 0;
while (i--) {
j = Math.floor(Math.random() * (i + 1));
this.shuffledDeck.push(this.deck[j]);
this.deck.splice(j, 1);
}
return this.shuffledDeck;
},
DistributeCards: function(playerDeck) {
//Distributes half the deck to each player
for (var i = 0; i < this.shuffledDeck.length / 2; i++) {
playerDeck.push(this.shuffledDeck[i]);
}
//console.log(playerDeck);
return playerDeck;
}
}
Player1 = new Player(false, [], []);
Player2 = new Player(false, [], []);
Deck.BuildDeck();
Deck.ShuffleDeck();
Deck.DistributeCards(Player1.currentCards);
for (var i = 0; i < Player1.currentCards.length; i++) {
console.log("P1:::" + Player1.currentCards[i][0], Player1.currentCards[i][1], Player1.currentCards.indexOf(Player1.currentCards[i]));
}