I am trying to make a card game that is similar to the game War. The game has two players, each with their own deck they play from.
I have a function that passes in an empty array that is the deck each player has.
This array is populated by half of the items in another array that is the entire deck.
Most of the time, this works correctly, but sometimes, the array will have 26 values, other times, some values will be duplicated.
My question is, how do I stop duplicates being passed into the player's deck array?
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: [],
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]]);
}
}
return this.deck;
},
ShuffleDeck: function() {
//Shuffles the deck
for (var card = 0; card < this.deck.length; card++) {
this.shuffledDeck.push(this.deck[Math.floor(Math.random() * this.deck.length)]);
}
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]);
}
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(Player1.currentCards[i][0], Player1.currentCards[i][1], Player1.currentCards.indexOf(Player1.currentCards[i]));
}