【问题标题】:Very basic dice game in Javascript - trying to log 'wins' variableJavascript中非常基本的骰子游戏 - 尝试记录'wins'变量
【发布时间】:2015-02-09 16:55:40
【问题描述】:

我正在尝试制作一个非常基本的骰子游戏(Javascript 新手)。在页面加载时,“骰子”被“滚动”了 3 次并显示结果,并显示一条消息,说明您是否成功掷出 6。我正在尝试输入有关已赢得多少场比赛的永久消息-问题是,如果您查看下面的代码,则每次获胜时我用于此“胜利”的变量都会增加,但是它实际上只显示两个值:如果用户刚刚输了,则为 0,如果用户赢了,则为 1。无论掷骰子多少次,它都不会达到更高的数字。想知道是否有人有解决方案/解释?

代码:

console.log("Dice game. You have 3 tries to roll a 6 - go");
var rolls = 0;
var wins = 0;

function rollDice()  {
    var dice = Math.random();
    if (dice <= .17) {
        dice = 1;   
    }
    else if (dice <= .33) {
        dice = 2;   
    }
    else if (dice <= .50) {
        dice = 3;   
    }
    else if (dice <= .67) {
        dice = 4;   
    }
    else if (dice <= .84) {
        dice = 5;   
    }
    else if (dice <= 1) {
        dice = 6;   
    }
    return dice;
}

function diceGame() {
    do {
        var dice = rollDice();
        console.log(dice);
        rolls++;
        if (dice === 6) {
            console.log("You won!");
            wins++;
            if (rolls === 1) {
                console.log("It took " + rolls + " try");
            }
            else {
                console.log("It took " + rolls + " tries");
            }
            break;
        }
    }
    while (rolls <= 2);
    if (dice !== 6) {
        console.log("You lost");
    }
}

diceGame();
console.log("Times won: " + wins);

【问题讨论】:

  • 必须刷新/重新加载页面才能再次掷骰子?
  • 当你赢了你在做break;而不是continue;
  • 去掉“break”操作符

标签: javascript


【解决方案1】:

永远不会命中值 1,因为:

Math.random() 函数返回一个浮点伪随机数 [0, 1) 范围内的数字,即从 0(含)到但不 包括 1(独家),然后您可以将其缩放到您想要的 范围。实现选择初始种子随机 数字生成算法;用户不能选择或重置它。

参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random

【讨论】:

    【解决方案2】:

    你是如何运行代码的?每次运行时,它都会将 wins 变量重置为 0。您需要使用按钮或其他方式调用该函数,这样它就不必刷新代码块即可再次运行。

    【讨论】:

      【解决方案3】:

      一些改进 ;)

      1. 调用 continue 而不是 break MDN continue
      2. 更改掷骰子功能Random number from range
      3. 创建命名空间的匿名函数

      // 我们变量的命名空间 (功能(){ console.log("Dice game. You have 3 tries to roll a 6 - go"); var rolls = 0; var wins = 0; function rollDice() { // simple random from range ( https://stackoverflow.com/a/1527820/2746472 ) return Math.floor(Math.random()*6)+1; } function diceGame() { do { var dice = rollDice(); console.log(dice); rolls++; if (dice === 6) { console.log("You won!"); wins++; if (rolls === 1) { console.log("It took " + rolls + " try"); } else { console.log("It took " + rolls + " tries"); } continue; //instead of break! } } while (rolls <= 2); if (dice !== 6) { console.log("You lost"); } } diceGame(); console.log("Times won: " + wins); })();

      【讨论】:

      • 感谢您的建议。我已经实现了它们,虽然我的代码肯定更整洁,但问题还没有解决。有什么建议么?此外,“继续”似乎确实会导致游戏在获胜后继续进行,直到尝试了 3 次,这是我试图避免的。
      • 为我工作:Dice game. You have 3 tries to roll a 6 - go 6 You won! It took 1 try 6 You won! It took 2 tries 4 Times won: 2。也许你想要别的东西。描述你的目标;)
      【解决方案4】:

      您的 rollDice 函数可以简化为 1 行。

      var dice = Math.floor(Math.random() * 6) + 1;
      

      【讨论】:

      • 这是范围 0-6 并且分布不均匀。
      • 它为 0 的可能性极小。所以你永远不会看到它发生!
      • 但就是这样! General, we randomly select numbers from a specific range, 0 - send nuclear bomb to Russia, but is unlikely to happen, don't bother this.
      • 是的。 (还有 10 个字符)
      【解决方案5】:

      这是因为如果你赢了你有一个休息,所以它在第一次获胜后退出循环。

      【讨论】:

        猜你喜欢
        • 2013-10-14
        • 2012-11-27
        • 1970-01-01
        • 2016-05-25
        • 1970-01-01
        • 2012-02-29
        • 2021-12-14
        • 2015-08-25
        • 2015-12-24
        相关资源
        最近更新 更多