在掷骰子游戏中输出得分结果的问题

Issue with outputting scoring result in a Craps game

我正在开发一个有点简单的骰子游戏,但是当我单击按钮时我无法让它显示警报弹出窗口。

我也在尝试找出最简单的方法来检查 housescore 或 myscore = maxscore 然后提醒 "congratulations, you win" 或 "sorry, you lost"

这是我的代码:

var myscore = 0;
var maxScore = 100;
var housescore = 0;

function rollDice() {
  var x = Math.floor(Math.random() * 6) + 1;
  var y = Math.floor(Math.random() * 6) + 1;

  if (x + y == 7 || 11) {
    housescore = (housescore + 10);
    alert("CRAPS" + " Your Score is " + "Player: " + myscore + "House: " + housescore);
  } else if (x == y && x + y == isEven(n)) {
    myscore = (myscore + 10);
    alert("Even Up" + " Your Score is " + "Player: " + myscore + "House: " + housescore);
  }
  if (x == y && x + y == isOdd(n)) {
    housescore = (housescore + 10);
    alert("Odd Ball" + " Your Score is " + "Player: " + myscore + "House: " + housescore);
  } else {
    alert("You rolled a " + x + " and a " + y + " Your Score is " + "Player: " + myscore + "House: " + housescore);
  }
}

function isEven(n) {
  return n % 2 == 0;
}

function isOdd(n) {
  return Math.abs(n % 2) == 1;
}
<input type="button" value="Roll The Dice" onClick="rollDice()" />

这样的代码是错误的:

if (x == y && x + y == isEven(n))

您尚未声明 [​​=15=] 变量。并且将两个数字的加法与 truefalse 的值进行比较是没有意义的,即 isEven()isOdd() return。我想你的意思是:

if (x == y && isEven(x + y))

但是当两个数相等时,将它们相加总是偶数,所以我不确定该测试的意义何在。也许你的意思是:

if (x == y && isEven(x))

我不熟悉 Craps 中的规则,即庄家或玩家获胜取决于相等的骰子是偶数还是奇数。

这也是错误的:

if (x == 7 && y == 11)

xy是从16的数字,所以不能是711。在花旗骰中,你将两个骰子相加,所以这应该是:

if (x + y == 7 || x + y == 11)

几乎不需要 isEven()isOdd() 函数 -- 如果数字不是偶数,则为奇数。

这就是我认为您想要的。我分解了你的 ifs 以处理一个分支中 x == y 的两种情况,并使用一个简单的 mod 操作来确定掷骰是否为偶数,并使用 else 来处理奇数掷骰。

var myscore = 0;
var maxScore = 100;
var housescore = 0;

function rollDice() {
  var x = Math.floor(Math.random() * 6) + 1;
  var y = Math.floor(Math.random() * 6) + 1;
  
  var total = x + y;
  msg = "You rolled " + x + "," + y + " = " + total + "\n";

  if (total == 7 || total == 11) {
    housescore += 10;
    msg += "CRAPS";
  } else if (x == y) { // this condition contains two possible outcomes, handle both within this else if
    if (x % 2 == 0) {
      myscore += 10;
      msg += "Even Up";
    } else {
      housescore += 10;
      msg += "Odd Ball";
    }
  }
  msg += " Your Score is " + "Player: " + myscore + ", House: " + housescore;
  alert(msg);
}
<!DOCTYPE html>
<html>

<body>
  <input type="button" value="Roll The Dice" onClick="rollDice()" />
</body>

</html>

<!DOCTYPE html>
<html>
<body>

<input type="button" value="Roll The Dice" onClick="rollDice()" />
<br />

<script type="text/javascript">
var score = 0;
var maxScore = 50;
var rolls = 0;
var maxRolls = 20;


function rollDice()
{
    var x = Math.floor( Math.random() * 6 ) + 1;
    var y = Math.floor( Math.random() * 6 ) + 1;

    if( x == y )
    {
        score = getScore( x );
        alert("You threw a Double " + x + " Your Score is "+ score);
    }
    else
    {
        alert("You threw a " + x + " and a " + y + " Your Score is " + score);
    }

    rolls++;

    if (rolls == maxRolls && score < maxScore)
    {
        alert("Sorry You Lose!");
        score = 0;
        rolls = 0;
        return;
    }
    else if (score >= maxScore)
    {
        alert("Congratulations You Win!");
        score = 0;
        rolls = 0;
        return;
    }
}

function getScore(x)
{
    switch( x )
    {
        case 1:
            score += 5;
            break;
        case 2:
            score += 5;
            break;
        case 3:
            score = 0;
            break;
        case 4:
            score += 5;
            break;
        case 5:
            score += 5;
            break;
        case 6:
            score += 25;
            break;
    }

    return score;
}
</script>
</body>
</html>
</html>