Javascript while 循环(纸牌模拟)

Javascript while loop (Card deck simulation)

我在使用以下模拟卡片组的代码时遇到问题。

牌组创建正确(1 个数组包含 4 个数组(花色),每个数组包含 13 个元素(面值))并且当我使用 G.test();功能是正确拉出 13 张随机卡,然后 returns 39x "Empty"(总共 52 张)。

我不想寻求帮助,但我已经把问题留了一夜,然后又过了一​​段时间,我仍然找不到发生这种情况的原因。我感谢您提供的所有见解。

var G = {};
G.cards = [[], [], [], []];

G.newCard = function(v) { //currently a useless function, tried a few things
    return v;
};

G.deck = {    
    n: function() { //new deck
        var x; var list = [];
        list.push(G.newCard("A"));

        for (x = 2; x <= 10; x += 1) {
            list.push(G.newCard(x.toString()));
        }

        list.push(G.newCard("J"), G.newCard("Q"), G.newCard("K"));

        for (x = 0; x < G.cards.length; x += 1) {
            G.cards[x] = list;
        }
    },

    d: function() { //random card - returns suit & value
        var s; var c; var v; var drawn = false; var n;

        s = random(0, G.cards.length);
        c = random(0, G.cards[s].length);
        n = 0;

        while (!drawn) {
            if (G.cards[s].length > 0) {
                if (G.cards[s][c]) {
                    v = G.cards[s].splice(c, 1);
                    drawn = true;
                } else {
                    c = random(0, G.cards[s].length);
                }
            } else {
                s = (s + 1 >= G.cards.length) ? 0 : s + 1;
                n += 1;
                console.log(s);
                if (n >= G.cards.length) {
                    console.log(n);
                    return "Empty";

                }
            }
        }
        return {s: s, v: v[0]};
    },
}; //G.deck

G.test = function() {
    var x; var v;
    G.deck.n();
    for (x = 0; x < 52; x += 1) {
        v = G.deck.d();
        console.log(v);
    }
}; 

替换

for (x = 0; x < G.cards.length; x += 1) {
    G.cards[x] = list;
}

for (x = 0; x < G.cards.length; x += 1) {
    G.cards[x] = list.slice();
}

因为这会阻止 G.cards[x] 的所有元素绑定到同一个(单个)数组实例。

如果所有元素都绑定到同一实例,则改变一个元素等于改变所有元素。 list.slice() 创建 list 的新副本,从而创建一个新的数组实例以防止上述问题。

我不会仔细检查您的代码,但我构建了一个可以满足您要求的代码。我只为一副牌而不是多副牌游戏建造了这个。有两个函数,一个是生成牌组,另一个是从牌组中抽牌,根据你需要多少手牌,每手牌需要多少张牌。抽出一张牌后,将不会重新抽出。我可能会在不久的将来在 http://kevinhng86.iblog.website.

上发表一篇关于纸牌交易程序如何工作或类似内容的短文
function random(min, max){
   return Math.floor(Math.random() * (max - min)) + min;
}


function deckGenerate(){
    var output = [];
    var face = {1: "A", 11: "J", 12: "Q", 13: "K"};
    // Heart Space Diamond & Club;
    var suit = ["H", "S", "D", "C"];
    // Delimiter between card identification and suit identification.
    var d = "-";

    for(i = 0; i < 4; i++){
        output[i] = [];
        for(ind = 0; ind < 13; ind++ ){
           card = (ind + 1);
           output[i][ind] = (card > 10) || (card === 1)? face[card] + d + suit[i] : card.toString() + d + suit[i];
        }
     }
     return output;
}

function randomCard(deck, hand, card){
    var output = [];
    var randS = 0;
    var randC = 0;
    if( hand * card > 52 ) throw("Too many card, I built this for one deck only");

    for(i = 0; i < hand; i++){
        output[i] = [];
        for(ind = 0; ind < card; ind++){
            randS = random(0, deck.length);
            randC = random(0, deck[randS].length);
            output[i][ind] = deck[randS][randC];
            deck[randS].splice(randC,1);
            if(deck[randS].length === 0) deck.splice(randS,1);
        }
    }

    document.write( JSON.stringify(deck, null, 2) );

    return output;
}


var deck = deckGenerate()
document.write( JSON.stringify(deck, null, 2) );

document.write("<br><br>");

var randomhands = randomCard(deck, 5, 8);

document.write("<br><br>");
document.write("<br><br>");


document.write( JSON.stringify(randomhands, null, 2) );