我在獲取數組時需要正確填充時遇到了一些麻煩。我嘗試着將一副牌加載到一個數組中,然後進行洗牌,然後在開始檢查後看看是否有足夠的牌,陣列無法正確加載,而且更重要或更少的中斷。Javascript Array未被正確填充
下面是相關的代碼。任何幫助將不勝感激。謝謝!
var deck = {
//base deck before shuffle
baseDeck: ['d02', 'd03', 'd04', 'd05', 'd06', 'd07', 'd08', 'd09', 'd10', 'd11', 'd12', 'd13', 'd14', 'h02', 'h03', 'h04', 'h05', 'h06', 'h07', 'h08', 'h09', 'h10', 'h11', 'h12', 'h13', 'h14', 'c02', 'c03', 'c04', 'c05', 'c06', 'c07', 'c08', 'c09', 'c10', 'c11', 'c12', 'c13', 'c14', 's02', 's03', 's04', 's05', 's06', 's07', 's08', 's09', 's10', 's11', 's12', 's13', 's14'],
//Deck Shoe
shoe: [],
//pull deck #, return to shoe
shuffleDeck: function() {
this.shoe.length = 0;
this.shoe = this.baseDeck;
for (i = 0; i < this.shoe.length; i++) {
var randomPlace = Math.floor(Math.random() * 50) + 1;
var currentPlace = this.shoe[i];
this.shoe[i] = this.shoe[randomPlace];
this.shoe[randomPlace] = currentPlace;
}
}
}
var cardRetrieval = {
//return card vals
getCard: function (curHand) {
var q = curHand.push(deck.shoe.shift());
this.checkValue(curHand);
showCards(curHand, q);
if (deck.shoe.length <= 40) {
deck.shuffleDeck();
}
}
一切正常,直到if語句在底部,檢查鞋陣列中是否有40多張牌。但是當它試圖再次洗牌時,它會破裂。
感謝您的建議。這對我來說非常有意義。 – Bernard 2011-02-13 14:22:47
你是否知道爲什麼當deck.shoe.length <= 40時,實際的shuffleDeck函數沒有被正確加載? – Bernard 2011-02-13 14:24:04