Javascript试图洗牌我的一副牌数组,但可以';我不知道如何传入数组

Javascript试图洗牌我的一副牌数组,但可以';我不知道如何传入数组,javascript,html,css,Javascript,Html,Css,我正在做一个个人项目/观看教程,只是为了在学校玩游戏。最终,这是我小时候玩的一种战争纸牌游戏——赢的次数最多。我正在尝试随机排列我的卡片阵列,我有52个对象按我想要的方式存储,但现在我遇到了这个路障 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> War Cards!

我正在做一个个人项目/观看教程,只是为了在学校玩游戏。最终,这是我小时候玩的一种战争纸牌游戏——赢的次数最多。我正在尝试随机排列我的卡片阵列,我有52个对象按我想要的方式存储,但现在我遇到了这个路障

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>
            War Cards!
        </title>
    </head>
    <body>
        <div id="wrapper">
            <div id="start"></div>
            <div id="message"></div>
            <div id="board">
                <div id="player1" class="players">
                    <div class="score"></div>
                    <div class="hand"></div>
                </div>
                <div id="player2">
                    <div class="score"></div>
                    <div class="hand"></div>
                </div>
            </div>
            <div id="action">
                <button id="btnBattle" type="button" class="btn">
                    Fight!
                </button>
            </div>
        </div>

        <script src="js/jquery-3.3.1.min.js">
        </script>
        <script>
            $('document').ready(function() {
                var suits = ["spades", "hearts", "clubs", "diams"];
                var cardFace = ["2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"];
                var cards = [];
                var players = [[], []];
                var firstRun = true;
                var fightButton = document.querySelector("#btnBattle");

                fightButton.addEventListener('click', battle);

                function battle()
                {
                    if (firstRun)
                    {
                        firstRun = false;
                        buildCards();
                        shuffleArray();
                    }
                    console.log('Works');
                }


                function buildCards()
                {
                    cards = [];
                    for (s in suits)
                    {
                        var suitNew = suits[s][0].toUpperCase();
                        for(n in cardFace)
                        {
                            var card = {
                                suit:suits[s],
                                num:cardFace[n],
                                cardValue:parseInt(n) +2,
                                icon:suitNew
                            }
                            cards.push(card);
                        }
                    }
                    console.log(cards);
                }

                function shuffleArray(array)
                {
                    for(var x = array.length -1; x > 0; x--)
                    {

                        var ii = Math.floor(Math.random() * (x + 1))
                        var temp = array[x];
                        console.log(temp)
                    }
                    return array;
                }










            });

        </script>
    </body>
</html>

战争卡片!
打仗
$('document').ready(函数(){
var套装=[“黑桃”、“红桃”、“梅花”、“钻石”];
var cardFace=[“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“J”、“Q”、“K”、“A”];
var卡=[];
变量玩家=[[],[];
var firstRun=true;
var fightButton=document.querySelector(“#btnBattle”);
fightButton.addEventListener('click',battle');
函数战()
{
如果(首次运行)
{
firstRun=false;
buildCards();
shuffleArray();
}
console.log('Works');
}
函数buildCards()
{
卡片=[];
(穿西装的)
{
var suitenew=suites[s][0].toUpperCase();
用于(卡片面中的n)
{
var卡={
西服:西服,,
num:cardFace[n],
cardValue:parseInt(n)+2,
图标:SuiteNew
}
卡片。推(卡片);
}
}
控制台.日志(卡片);
}
函数shuffleArray(数组)
{
对于(var x=array.length-1;x>0;x--)
{
var ii=数学地板(数学随机()*(x+1))
var-temp=数组[x];
控制台日志(临时)
}
返回数组;
}
});

避免将所有内容添加到全局范围。下面,我将从
buildCards()
返回卡片,并将卡片传递到
shufflearlay()
(我从中复制,因为原始的
shufflearlay
函数实际上没有移动任何元素):

var suits=[“黑桃”、“红桃”、“梅花”、“钻石”];
var cardFace=[“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“J”、“Q”、“K”、“A”];
变量玩家=[[],[];
var firstRun=true;
函数战()
{
如果(首次运行)
{
firstRun=false;
var cards=buildCards();
var随机化=洗牌牌(卡片);
控制台日志(随机);
}
console.log('Works');
}
函数buildCards()
{
var卡=[];
(穿西装的)
{
var suitenew=suites[s][0].toUpperCase();
用于(卡片面中的n)
{
var卡={
西服:西服,,
num:cardFace[n],
cardValue:parseInt(n)+2,
图标:SuiteNew
}
卡片。推(卡片);
}
}
回程卡;
}
函数shuffleArray(数组){
var m=数组长度,t,i;
//虽然还有一些元素需要洗牌…
while(m){
//选择剩余的元素…
i=Math.floor(Math.random()*m--);
//并将其与当前元素交换。
t=阵列[m];
数组[m]=数组[i];
数组[i]=t;
}
返回数组;
}

战斗()在声明和调用函数时,形式参数和实际参数之间的差异似乎存在问题

形式参数

当你声明一个函数时,你给它的参数正式的参数名,这样你就可以在函数中引用它们。在调用函数之前,它们没有实际值。例如,在shuffle函数声明中

function shuffle( array) {....
array
是一个形式参数

实际参数

调用函数时,形式参数将替换为实际参数

例如,要洗牌
数组,您可以将
洗牌
称为

shuffle( cards);
提供
卡片
作为实际参数


请注意,shuffle函数看起来是在实现Fisher-Yates算法,但缺少两行,这两行用于交换数组项
x
ii

下面是一个包含缺失行的随机播放函数的副本:

function shuffle( a) { // fisher yates algorithm ;
    for( var i = a.length; --i;) {
        var j = Math.floor( Math.random() * (i+1));
        var temp = a[i]; // swap entries at i and j
        a[i] = a[j];
        a[j] = temp;
    }
}
如果愿意,可以将形式参数名称
a
,以及在函数体中对它的引用更改为
数组


请注意,数组元素被“就地”洗牌,因此不需要返回数组参数值,因为它没有更改。

请参阅。实现起来非常简单。别忘了在
buildCards
函数中声明变量
s
n
,这很有效!我理解你所做的一切,直到shuffleArray功能,你能再解释一下吗?我只上了4周的javascript课哈哈,你想知道关于shuffleArray函数的什么?我刚刚在谷歌上搜索了“fisher-yates shuffle javascript”,找到了第一个方法,并从这里复制了它:@FrankerZ特定的shuffle函数会破坏其输入数组,作为其实现逻辑的一部分。。