2

我正在用 Javascript 创建一个二十一点纸牌游戏。到目前为止,我很欣赏你的评论。我正在寻找关于为什么我似乎只能在 cardDealer 函数中调用 cardFace 函数一次的反馈。目前 cardFace(0) 给了我一个值,而 cardFace(1) 或任何其他数字都没有,除非它是 cardDealer 函数中唯一被调用的 cardFace(x) 。任何想法,将不胜感激。谢谢

下面的代码并不完整,但我相信足以从中辨别出足够的信息。谢谢

    function card(name, suit, face, value)
    {
        this.name = name;
        this.suit = suit;
        this.face = face;
        this.value = value;
    }

    aceOfHearts = new card("aceofhearts","hearts", "ace", 11);
    kingOfHearts = new card("kingofhearts","hearts", "king", 10);
    queenOfHearts = new card("queenofhearts","hearts", "queen", 10);
    jackOfHearts = new card("jackofhearts","hearts", "jack", 10);
    tenOfHearts = new card("tenofhearts","hearts", "ten", 10);
    nineOfHearts = new card("nineofhearts","hearts", "nine", 9);
    eightOfHearts = new card("eightofhearts","hearts", "eight", 8);
    sevenOfHearts = new card("sevenofhearts","hearts", "seven", 7);
    sixOfHearts = new card("sixofhearts","hearts", "six", 6);


    var deck;
    deck = [];


    deck.push(aceOfHearts);
    deck.push(aceOfDiamonds);
    deck.push(aceOfClubs);
    deck.push(aceOfSpades);
    deck.push(kingOfClubs);
    deck.push(kingOfDiamonds);
    deck.push(kingOfHearts);
    deck.push(kingOfSpades);
    deck.push(queenOfClubs);
    deck.push(queenOfDiamonds);
    deck.push(queenOfHearts);
    deck.push(queenOfSpades);
    deck.push(jackOfClubs);
    deck.push(jackOfDiamonds);
    deck.push(jackOfHearts);
    deck.push(jackOfSpades);
    deck.push(tenOfClubs);
    deck.push(tenOfDiamonds);
    deck.push(tenOfHearts);
    deck.push(tenOfSpades);


    var cardDealer = function()
    {  
        fisherYates(deck);

        document.getElementById("yourFirstCard").textContent = "Your First Card is " + cardFace(0) ;
        document.getElementById("yourSecondCard").textContent = "Your Second Card is " + cardFace(1) ;

    };

    var cardFace = function(x)
    {   
        cardFace = deck[x].face;
        return cardFace;
    };


    var cardSuit = function(x)
    {
        cardSuit = deck[x].suit;
        return cardSuit;
    };

    <body>
<div>Black Jack 1.1</div>
<br/>
<button type="button" onClick="cardDealer()">Deal</button> 
<div id="yourFirstCard"></div>
<div id="yourSecondCard"></div>
<div id="yourThirdCard"></div>
<div id="yourFourthCard"></div>
<div id="yourFifthCard"></div>
<div id="playerTotal"></div></div>
<div id="playerSit"></div>
<div id="playerMessage"></div>
<div id="sitter"><button type="button" onClick="sit()">Sit</button></div>
<div id="hitter"><button type="button" onClick="hitMe()">Hit</button></div> 
<br/>
<br/>
<div id="dealersFirstCard"></div></div>
<div id="dealersSecondCard"></div></div>
<div id="dealersThirdCard"></div></div>
<div id="dealersFourthCard"></div></div>
<div id="dealersFifthCard"></div></div>
<div id="dealerTotal"></div></div>
<div id="dealerSit"></div></div>
<div id="dealerMessage"></div></div>
</body>
4

2 回答 2

0

cardFace移至上方cardDealer或将其更改为定义function cardFace(x)

这是一个 js 行为的事情。function x可以从上面编写的代码调用以该格式定义的函数。哪里var x = function()不能,因为它是在运行时确定的。

有关Resig 先生的更多示例,请参见:http ://ejohn.org/apps/learn/#4。

试试看:http ://ejohn.org/apps/learn/这是一件好事。

编辑:还有很多你应该在这个线程中查看的好建议。包括其他发布的答案。你应该调查一下scopes

编辑 2

再次查看您的代码后,我相信我的解决方案实际上不是正确的,因为它让我忘记了cardFace在其范围内被调用,cardDealer这意味着它可能会在您调用时定义cardDealer。因此,我认为当您将其更改为时,function您可能已经更改了名称,正如另一个答案所暗示的那样。事实上是正确的答案。

//Test function calling forward defined function
var f = function() { x(); };
var x = function() { console.log('a'); }
f()
>>> a

// Test function modifying it's own scoped name
var f = function() { f = 'test'; return f; };
f()
>>> "test"
f()
>>> TypeError: string is not a function

// Test function modifying it's own scoped name when declaring as function
function f() { f = 'test'; return f; };
f()
>>> "test"
f()
>>> TypeError: string is not a function
于 2013-02-07T22:43:14.997 回答
0

这是我看到问题的地方:

var cardFace = function(x)
{   
    cardFace = deck[x].face;
    return cardFace;
};

首先,您声明一个名为的变量cardFace并将其分配给一个函数。这个函数实际上做了什么它重新分配你的cardFace变量并作为结果返回它。就是这样,你的函数现在已经消失了,你不能再引用它了,因为cardFace已经被重新分配了。

您需要做的是更改函数的主体,使其不会重新分配变量:

var cardFace = function(x)
{   
    var cardFaceValue = deck[x].face;
    return cardFaceValue;
};

函数也是如此cardSuit

于 2013-02-07T21:29:13.633 回答