我正在开发一个稍微简单的骰子游戏,但是单击按钮时无法显示警告弹出窗口。
我还试图找出最简单的方法来检查housescore或myscore = maxscore,然后发出“祝贺,您赢了”或“对不起,您输了”的警报
这是我的代码:
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()" />
答案 0 :(得分:1)
这样的代码是错误的:
if (x == y && x + y == isEven(n))
您尚未声明n
变量。而且,比较两个数字与true
和false
返回的isEven()
或isOdd()
值的加法运算是没有意义的。我想你的意思是:
if (x == y && isEven(x + y))
但是,当两个数字相等时,将它们加在一起始终是偶数,因此我不确定该测试的意义是什么。也许你的意思是:
if (x == y && isEven(x))
我对“掷骰子”中的规则不熟悉,在该规则中,房子或玩家根据相等的骰子是偶还是奇来获胜。
这也是错误的:
if (x == 7 && y == 11)
x
和y
是从1
到6
的数字,因此它们不能是7
或11
。在胡扯中添加两个骰子,因此应为:
if (x + y == 7 || x + y == 11)
isEven()
和isOdd()
函数几乎都不需要-如果不是偶数,则为奇数。
答案 1 :(得分:1)
这就是我所追求的。我将您的if分解为在一个分支中处理x == y的两种情况,并使用一个简单的mod操作确定骰子掷骰是否是偶数,而另一个则处理奇数骰子掷骰。
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>
答案 2 :(得分:0)
<!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>