Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 洗牌_Javascript_Arrays_Shuffle - Fatal编程技术网

Javascript 洗牌

Javascript 洗牌,javascript,arrays,shuffle,Javascript,Arrays,Shuffle,我有一个问题,如何在数组中洗牌这个?请帮助我。我真的很感激你们帮了大忙,非常感谢 我想做的是在数组中洗牌这个 // ______________________________________________Function to declare our var -> deck of cards function getDeck() { var deck = [ {suit: "H", face: "A"}, {suit: "H", fa

我有一个问题,如何在数组中洗牌这个?请帮助我。我真的很感激你们帮了大忙,非常感谢

我想做的是在数组中洗牌这个

    // ______________________________________________Function to declare our var -> deck of cards

function getDeck() {

    var deck = [
        {suit: "H", face: "A"},
        {suit: "H", face: "2"},
        {suit: "H", face: "3"},
        {suit: "H", face: "4"},
        {suit: "H", face: "5"},
        {suit: "H", face: "6"},
        {suit: "H", face: "7"},
        {suit: "H", face: "8"},
        {suit: "H", face: "9"},
        {suit: "H", face: "10"},
        {suit: "H", face: "J"},
        {suit: "H", face: "Q"},
        {suit: "H", face: "K"},
        {suit: "C", face: "A"},
        {suit: "C", face: "2"},
        {suit: "C", face: "3"},
        {suit: "C", face: "4"},
        {suit: "C", face: "5"},
        {suit: "C", face: "6"},
        {suit: "C", face: "7"},
        {suit: "C", face: "8"},
        {suit: "C", face: "9"},
        {suit: "C", face: "10"},
        {suit: "C", face: "J"},
        {suit: "C", face: "Q"},
        {suit: "C", face: "K"},
        {suit: "D", face: "A"},
        {suit: "D", face: "2"},
        {suit: "D", face: "3"},
        {suit: "D", face: "4"},
        {suit: "D", face: "5"},
        {suit: "D", face: "6"},
        {suit: "D", face: "7"},
        {suit: "D", face: "8"},
        {suit: "D", face: "9"},
        {suit: "D", face: "10"},
        {suit: "D", face: "J"},
        {suit: "D", face: "Q"},
        {suit: "D", face: "K"},
        {suit: "S", face: "A"},
        {suit: "S", face: "2"},
        {suit: "S", face: "3"},
        {suit: "S", face: "4"},
        {suit: "S", face: "5"},
        {suit: "S", face: "6"},
        {suit: "S", face: "7"},
        {suit: "S", face: "8"},
        {suit: "S", face: "9"},
        {suit: "S", face: "10"},
        {suit: "S", face: "J"},
        {suit: "S", face: "Q"},
        {suit: "S", face: "K"},
    ];

    return deck;
}

// ______________________________________________Function to shuffle cards

function shuffle(o) { // o is passed-in array

     for (var j, x, i = o.length; i; j = parseInt(Math.random() * i, 10), x = o[--i], o[i] = o[j], o[j] = x);
     return o;

};
这是另一个代码,我认为问题出在这里,还是没有

// ______________________________________________Function to retrieve our deck of cards & save into localStorage

function getShoe(decks) {

    var shoe = [];
    var deck = getDeck();
    this.decks = decks;

        for (z=1;z<=decks;z++) {
            shoe.push(deck);
        }

    shoe = shuffle(shoe);
    return shoe;


    localStorage.setItem('shoe', JSON.stringify(shoe));

}
/\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
功能(甲板){
var=[];
var deck=getDeck();
这个。甲板=甲板;

对于(z=1;z将其用于洗牌功能:

o.sort(function() {return 0.5 - Math.random()})

牌组仍然没有洗牌。很有趣。你有错误吗,或者什么都没有发生?这是一种不好的洗牌方式。我在这里的帖子更详细地介绍了这些概念:简而言之:它很容易失败,因为给定了三项(a、b和c)在一个更大的列表中,在洗牌过程中的不同点,它可能会进行比较,例如a>b、b>c和c>a。即使这不是真的,这种方法也容易产生偏差。有没有人能让这种方法起作用?@user2910182检查你问题评论中的链接。