私はゲームに似たカードゲームを作成しようとしています戦争。ゲームには2人のプレイヤーがいて、それぞれに自分のデッキがあります。
私は、各プレイヤーが持つデッキである空の配列を渡す関数を持っています。
この配列には、デッキ全体である別の配列の項目の半分が入力されます。
ほとんどの場合、これは正しく動作しますが、アレイには26の値があり、それ以外の場合は値が重複することがあります。
私の質問は、重複がプレイヤーのデッキ配列に渡されないようにするにはどうすればいいですか?時折重複値を返す配列
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]));
}
それを第2のプレーヤーにも配布するための私の答えを確認してください:) –