javascript - 如何摆脱代码中的 undefined(s) 和其他问题?

标签 javascript undefined blackjack

我已经在一千次不同的时间里尝试过一千种不同的方法,但我的 JS 代码不会以我想要的方式出现。当我在 Mozilla 暂存器中运行它时,我得到“userHand 未定义”并且第二个 printHand 也显示为未定义。谁能告诉我 21 点游戏中的错误在哪里?

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 = [];
    cards.push(deal());
    cards.push(deal());
    this.getHand = function () {
        return cards;
    };
    this.score = function () {
        var score;
        for (i = 0; i < cards.length; i++) {
            score = score + cards[i].getValue();
        }
        for (i = 0; i < cards.length; i++) {
            if (score > 21 && cards[i].getValue() === 11) {
                score = score - 10;
            }
        } return score;
    };
    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()];
            }
        } alert(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();
    };
};

var playAsUser = function () {
    var playUser = new Hand();
    this.printHand = function () {
    return playUser.printHand();
    };
    this.score = function () {
    return playUser.score();
    };
    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");
    }
};

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

var playGame = function () {
    var user = playAsUser();
    var dealer = playAsDealer();
    declareWinner(user, dealer);
    console.log("User got " + user.printHand());
    console.log("Dealer got " + dealer.printHand());
};

playGame();

最佳答案

您在 printHand() 上没有返回任何内容

我刚刚添加了返回语句并开始工作。看这个fiddle

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()];
            }
        } 
        //alert(hand);  //remove this alert
        return hand; // <----- solution
};

关于javascript - 如何摆脱代码中的 undefined(s) 和其他问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12389420/

相关文章:

c - 储存二十一点手牌的最佳方式是什么?

java - 为 JButton 提供多个鼠标点击选项(二十一点游戏)

javascript - 是否有等效于 php.net 的 JavaScript?

javascript - 检测 'undefined' 被覆盖位置的可能方法?

Angular 4 可观察捕获服务未定义

JavaScript onchange 函数未定义?

javascript - 浏览器之间使用 "let"关键字的不同变量作用域

javascript - 两个日期之间的自定义过滤器

循环反转数组的Javascript

java - 为二十一点程序返回 bufferdimage 的方法