我正在学习一些javascript并且遇到了绊脚石..我想从我的数组中选择2个随机值并将它们保存到一个名为Deal的变量(我猜的是另一个数组)中,我可以得到一个随机值但不是2 ..我的代码如下
var deckOfCards = []; //declaring an empty array to put Cards into later
function Card(name, value, altValue) { //function that defines a card object ( key part is "this")
this.cardName = name;
this.cardValue = value;
this.cardAltValue = altValue || false;
}
var suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs'];
var values = ['Ace', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King'];
for(var i in suits) //loops through each of the suits
{
for(var j in values) { // loops through each of the cards for the suit
if(values[j] != 'Ace') {//j is the index within the array,
var cardValue = parseInt(j, 10) + 1;
if(cardValue > 10) cardValue = 10;
deckOfCards.push(new Card(values[j] +' of ' +suits[i], cardValue));//adding a new card object to deckOfCards
} else {
deckOfCards.push(new Card(values[j] +' of ' +suits[i], 1, 11));// adding alternate value to deckOfCards
}
}
}
var Deal = deckOfCards[Math.floor(Math.random()*deckOfCards.length)];;
console.log(Deal);
有人可以给我指点吗
由于
答案 0 :(得分:2)
function randomCard(deck, numToExtract) {
var extracted = [];
for (var i = 0; i < numToExtract; i++){
extracted.push(deck.splice(Math.floor(Math.random()*deckOfCards.length, 1));
}
return extracted;
}
返回值将是从牌组中提取的牌阵列,牌组阵列将取出这些牌。您需要跟踪卡片的位置并确保没有“消失”。
答案 1 :(得分:2)
你可能会考虑改组整张卡片(随机化所有52张牌),然后交易变成只是从牌组中拉出顶牌。
一个可能有帮助的观察结果是,洗牌后的牌组只需要有一个定义的deckOfCards的索引,所以它只是一组随机值0..51(或者你喜欢的1..52)。
//define your cards
deckOfCards[] = ... //left up to you
//define Dealt[], to make checking whether already shuffled, dealt
card = 0 //or 1, however you prefer
while (card<52) {
Dealt[card] =0; card++;
}
//Shuffle
card = 0 //or 1, however you prefer
while (card<52) {
//try to pick a card not yet shuffled
pick = Math.floor(Math.random()*deckOfCards.length);
while (Dealt[pick]) { //used, pick again
pick = Math.floor(Math.random()*deckOfCards.length);
}
Dealt[card] =1; //used
ShuffleDeck[card] =pick; card++;
}
//now dealing becomes simply
top=0
//define a function to deal a card
var deal = function() {
if( !(top<52) ) {
return -1; //or, handle error here
}
result = top;
top++;
return result; //work with indices
//or
//return deckOfCards[ShuffleDeck[result]];
}
carddealt = deal();
//deal two cards to a Hand,
Hand[player].push( deal() );
Hand[player].push( deal() );
//suppose you want to deal four hands, five cards each
round = 0;
while (round < 5 ) {
hand = 0;
while (hand < 4 ) {
Hand[hand].push( deal() );
}
}
或者,您可以在处理时将掉落的[]处理的每张卡丢弃。
答案 2 :(得分:1)
试试这个:
var Deal = deckOfCards[Math.floor(Math.random()*deckOfCards.length)];
var Deal2 = deckOfCards[Math.floor(Math.random()*deckOfCards.length)];
while (Deal2.equals(Deal)) { // if you want Deal and Deal2 have to different
Deal2 = deckOfCards[Math.floor(Math.random()*deckOfCards.length)];
}
您还需要实现equals
功能。
function Card(name, value, altValue) {
this.cardName = name;
this.cardValue = value;
this.cardAltValue = altValue || false;
equals = function(otherObject) {
return (otherObject.cardName == this.cardName && otherObject.cardValue == this.cardValue && otherObject.cardAltValue == this.cardAltValue)
}
}
答案 3 :(得分:1)
你有几个选择。您可以再次对整个牌组调用相同的随机函数,然后确保第二个值与第一个值不同(使用while
循环),或者您实际上可以从牌组中取出卡片第一次,使用类似splice
的东西,然后你可以继续调用它,直到牌组为空。
splice
选项可能更好。
答案 4 :(得分:1)
var cardsHeld = {};
for( var x = 0; x < 2; x++ ) {
var newCard = deckOfCards[Math.floor(Math.random()*deckOfCards.length)];
//If Not found within current CardsHeld Obj, Append.
if ( !cardsHeld.hasOwnProperty( newCard.cardName )) {
cardsHeld[newCard.cardName] = newCard;
}
}
console.log( cardsHeld );
/**
* Prints:
* Object
* Five of Spades: Card
* Two of Hearts: Card
*/