0

不久前我问了一个关于同一批次代码的问题,但这次是一个不同的问题。使用我得到的关于那个不同问题的技巧,我试图解决这个问题,这是我在 JavaScript 二十一点游戏中的得分函数。当我在 Mozilla Scratchpad 上测试时,我的 score 函数不断打印出 NaN。我尝试调整返回值,合并两个 for 循环,甚至重命名我的 deal 函数中的变量,以确保它不会与其他变量混淆,但仍然没有。有人问了一个类似的问题,但它并没有解决我的问题。

function Card (s, n) {
    var suit = s;
    var number = n;
    this.getNumber = function () {
        return number;
    };
    this.getSuit = function () {
        return suit;
    };
    this.getValue = function () {
        if (number > 10) {
            return 10;
        } else if (number === 1) {
            return 11;
        } else {
            return number;
        }
    };
}

var cardNames = {1:"Ace", 2:"2", 3:"3", 4:"4", 5:"5", 6:"6", 7:"7", 8:"8", 9:"9", 10:"10", 11:"Joker", 12:"Queen", 13:"King"};
var suitNames = {1:"Clubs", 2:"Diamonds", 3:"Hearts", 4:"Spades"};

var deal = function () {
    var s = Math.floor(Math.random() * 4 + 1);
    var n = Math.floor(Math.random() * 13 + 1);
    return new Card(s, n);
};

function Hand(){
    var cards = [];
    var card1 = deal();
    var card2 = deal();
    cards = [card1, card2];
    this.getHand = function () {
        return cards;
    };
    this.score = function () {
        var points;
        for (i = 0; i < cards.length; i++) {
            points = points + cards[i].getValue();
        }
        for (i = 0; i < cards.length; i++) {
            if (points > 21 && cards[i].getValue() === 11) {
                points = points - 10;
            }
        } return points;
    };
    this.printHand = function () {
        for (i = 0; i < cards.length; i++) {
            var hand;
            if (i === 0) {
            hand = cardNames[cards[i].getNumber()] + " of " + suitNames[cards[i].getSuit()];
            } else {
            hand = hand + " and a " + cardNames[cards[i].getNumber()] + " of " + suitNames[cards[i].getSuit()];
            }
        } return hand;
    };
    this.hitMe = function () {
        cards.push(deal());
    };
}

var playAsDealer = function () {
    var playDealer = new Hand();
    while (playDealer.score() < 17) {
        playDealer.hitMe();
    }
    this.printHand = function () {
    return playDealer.printHand();
    };
    this.score = function () {
    return playDealer.score();
    };
    return playDealer;
};

var playAsUser = function () {
    var playUser = new Hand();
    var decision = confirm("Your hand is " + playUser.printHand() + ". Click OK to hit or Cancel to stand");
    for (i = 0; decision !== false; i++) {
        playUser.hitMe();
        decision = confirm("Your hand is " + playUser.printHand() + ". Click OK to hit or Cancel to stand");
    }
    this.printHand = function () {
    return playUser.printHand();
    };
    this.score = function () {
    return playUser.score();
    };
    return playUser;
};

var declareWinner = function (userHand, dealerHand) {
    if ((userHand.score < dealerHand.score) || userHand.score > 21) {
        alert("You lose.");
    } else if (userHand.score > dealerHand.score) {
        alert("You win.");
    } else {
        alert("You tied.");
    }
};

var playGame = function () {
    user = playAsUser();
    dealer = playAsDealer();
    declareWinner(user, dealer);
    alert("User got " + user.printHand() + " for a total score of " + user.score());
    alert("Dealer got " + dealer.printHand() + " for a total score of " + dealer.score());
};

playGame();
4

2 回答 2

6

您只需要在函数中初始化 的pointsscore。更改var points;var points = 0;,你应该没问题。

编辑:由于我确定这将是您的下一个问题,因此您还需要实际调用score. declareWinner您正在使用player.score而不是player.score()在多个地方。

于 2012-09-15T15:22:14.727 回答
0

当你声明它们时,你永远不会初始化它们。最初将它们初始化为一个数字,因为当您执行points = points + cards[i].getValue()javascript 所做的事情时,它会获取 LHS 和 RHS 的值,然后将它们转换为原语,使 points 成为 string ''。也每当你想确保获得一个数字执行parseInt(arg, base)

    var points = 0;
    for (i = 0; i < cards.length; i++) {
        points = points + cards[i].getValue();
    }
    for (i = 0; i < cards.length; i++) {
        if (points > 21 && cards[i].getValue() === 11) {
            points = points - 10;
        }
    } return points;
于 2012-09-15T15:28:27.327 回答