JavaScript-数组偶尔返回重复值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScript-数组偶尔返回重复值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2758字,纯文字阅读大概需要4分钟。
内容图文
![JavaScript-数组偶尔返回重复值](/upload/InfoBanner/zyjiaocheng/667/c49191f033b14392b8c96fa8d09de469.jpg)
我正在尝试制作类似于《战争》的纸牌游戏.游戏中有两名玩家,每个玩家都有自己的套牌.
我有一个传递空数组的函数,该数组是每个玩家拥有的套牌.
该数组由整个甲板上另一个数组中一半的项目填充.
在大多数情况下,这可以正常工作,但有时,数组将具有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]));
}
解决方法:
之所以会得到重复,是因为您从同一数组中进行选择,而没有排除已经选择的值.一种方法是删除(使用拼接)所选的项目,这样就不会再次选择它们.像这样:
ShuffleDeck: function() { // while there still items in the deck
while(this.deck.length) {
var index = Math.floor(Math.random() * this.deck.length); // get a random index
this.shuffledDeck.push(this.deck.splice(index, 1)[0]); // remove the item at index (splice will return an array so we use [0] to get the item)
}
return this.shuffledDeck;
},
注意:卡座之后将是空的,但是由于您不再使用它,因此不会有问题.
编辑:
将DistributeCards更改为此:
DistributeCards: function(player1, player2) {
for (var i = 0; i < this.shuffledDeck.length / 2; i++) {
player1.push(this.shuffledDeck[i]); // push the first half to player1's deck (0, 1, 2, ...)
player2.push(this.shuffledDeck[this.shuffledDeck.length - i - 1]); // push the other half to player2's deck (len - 1, len - 2, len - 3, ...)
}
// no need for return if you won't use the return value (my advice is to remove return from all the function that you don't use their return value)
}
然后像这样使用它:
Deck.DistributeCards(Player1.currentCards, Player2.currentCards);
内容总结
以上是互联网集市为您收集整理的JavaScript-数组偶尔返回重复值全部内容,希望文章能够帮你解决JavaScript-数组偶尔返回重复值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。