如何让我的骰子游戏程序在每次掷骰时生成新的 "dice" 数字
How do I get my dice game program to generate new "dice" numbers each roll
每次我 运行 程序,都会出现相同的骰子数字。此外,我的 while 语句最终显示用户和计算机都获胜。我也只想进行最多五轮,之后程序显示游戏的获胜者。我如何获得它,以便只有拥有较高数字的玩家才能赢得这一轮。
<script>
var computerRandomNumberOne = Math.floor((Math.random() * 10) + 1);
var computerRandomNumberTwo = Math.floor((Math.random() * 10) + 1);
var userRandomNumberOne = Math.floor((Math.random() * 10) + 1);
var userRandomNumberTwo = Math.floor((Math.random() * 10) + 1);
var userPoints;
var computerPoints;
var userTotal;
var computerTotal;
var userWin = 0;
var computerWin = 0;
alert("Let's shake some dice!");
userTotal = userRandomNumberOne + userRandomNumberTwo;
computerTotal = computerRandomNumberOne + computerRandomNumberTwo;
while(userPoints || computerPoints != 5)
{
alert("Your turn to roll \n\nYou shook a " + userRandomNumberOne + " and a " + userRandomNumberTwo + ", so you have " + userTotal);
alert("My turn to roll \n\nI shook a " + computerRandomNumberOne + " and a " + computerRandomNumberTwo + ", so I have " + computerTotal);
if(computerTotal > userTotal)
{
computerWin++;
computerPoints++;
alert("I win " + computerTotal + " to " + userTotal + "\n\nI am winning " + computerWin + " to " + userWin);
}
else if(computerTotal < userTotal)
{
userWin++;
userPoints++;
alert("You win " + userTotal + " to " + computerTotal+ "\n\nYou win " + computerTotal + " to " + userTotal + "\n\nYou are winning " + computerWin + " to " + userWin);
}
if(computerTotal == userTotal)
{
alert("Tie! Roll Again! \n\n");
}
if(userPoints == 5)
{
alert("You win the game!");
}
else if(computerPoints == 5)
{
alert("The computer wins the game!");
}
}
</script>
给你!有几处不对劲。在 while 循环的 () 中,||必须分隔完整的布尔语句。您必须先给变量赋值,然后再添加它们。在其中一个提醒中,您已经连续两次明确输入 "You win" 短语。你的 "I am/You are winning" 声明 运行 如果你刚刚赢得了回合而不是检查整个游戏的状态。您不会在循环中重新计算骰子掷骰,因此它每次都使用相同的数字。我假设您希望 userWin 和 computerWin 是每个玩家赢得的整个游戏数量的 运行ning 总数,但它的编码与回合总数相同(不会像他们那样每回合重置为 0应该)。我相信就是这样。这是修改后的代码:
var userWin = 0;
var computerWin = 0;
function play(){
var userTotal = 0;
var computerTotal = 0;
var userPoints = 0;
var computerPoints = 0;
alert("Let's shake some dice!");
while(userPoints < 5 && computerPoints < 5){
var computerRandomNumberOne = Math.floor((Math.random() * 10) + 1);
var computerRandomNumberTwo = Math.floor((Math.random() * 10) + 1);
var userRandomNumberOne = Math.floor((Math.random() * 10) + 1);
var userRandomNumberTwo = Math.floor((Math.random() * 10) + 1);
userTotal = userRandomNumberOne + userRandomNumberTwo;
computerTotal = computerRandomNumberOne + computerRandomNumberTwo;
alert("Your turn to roll \n\nYou shook a " + userRandomNumberOne + " and a " + userRandomNumberTwo + ", so you have " + userTotal);
alert("My turn to roll \n\nI shook a " + computerRandomNumberOne + " and a " + computerRandomNumberTwo + ", so I have " + computerTotal);
if(computerTotal > userTotal){
computerWin++;
computerPoints++;
var winningMessage = "You are winning " + userWin + " to " + computerWin;
var computerIsWinning = (computerWin > userWin);
if(computerWin == userWin){
winningMessage = "We are tied " + userWin + " to " + computerWin;
}
else if(computerIsWinning){
winningMessage = "I am winning " + computerWin + " to " + userWin;
}
alert("I win " + computerTotal + " to " + userTotal + "\n\n" + winningMessage);
}
else if(computerTotal < userTotal){
userWin++;
userPoints++;
var winningMessage = "You are winning " + userWin + " to " + computerWin;
var computerIsWinning = (computerWin > userWin);
if(computerWin == userWin){
winningMessage = "We are tied " + userWin + " to " + computerWin;
}
else if(computerIsWinning){
winningMessage = "I am winning " + computerWin + " to " + userWin;
}
alert("You win " + userTotal + " to " + computerTotal + "\n\n" + winningMessage);
}
else{
alert("Tie! Roll Again! \n\n");
}
if(userPoints == 5){
alert("You win the game!");
}
else if(computerPoints == 5){
alert("The computer wins the game!");
}
}
userPoints = 0;
computerPoints = 0;
}
play();
我保留了你的随机性,但如果你想让它更干净一点,你可以使用 randojs.com 让它变得简单。您所要做的就是输入以下内容:
rando(1, 10)
而不是每次都这样:
Math.floor((Math.random() * 10) + 1)
但这是一个偏好问题!如果您想使用 randojs.com,只需在 HTML 文档的 head 标签内的顶部添加:
<script src="https://randojs.com/1.0.0.js"></script>
然后您就可以改用此代码了:
var userWin = 0;
var computerWin = 0;
function play(){
var userTotal = 0;
var computerTotal = 0;
var userPoints = 0;
var computerPoints = 0;
alert("Let's shake some dice!");
while(userPoints < 5 && computerPoints < 5){
var computerRandomNumberOne = rando(1, 10);
var computerRandomNumberTwo = rando(1, 10);
var userRandomNumberOne = rando(1, 10);
var userRandomNumberTwo = rando(1, 10);
userTotal = userRandomNumberOne + userRandomNumberTwo;
computerTotal = computerRandomNumberOne + computerRandomNumberTwo;
alert("Your turn to roll \n\nYou shook a " + userRandomNumberOne + " and a " + userRandomNumberTwo + ", so you have " + userTotal);
alert("My turn to roll \n\nI shook a " + computerRandomNumberOne + " and a " + computerRandomNumberTwo + ", so I have " + computerTotal);
if(computerTotal > userTotal){
computerWin++;
computerPoints++;
var winningMessage = "You are winning " + userWin + " to " + computerWin;
var computerIsWinning = (computerWin > userWin);
if(computerWin == userWin){
winningMessage = "We are tied " + userWin + " to " + computerWin;
}
else if(computerIsWinning){
winningMessage = "I am winning " + computerWin + " to " + userWin;
}
alert("I win " + computerTotal + " to " + userTotal + "\n\n" + winningMessage);
}
else if(computerTotal < userTotal){
userWin++;
userPoints++;
var winningMessage = "You are winning " + userWin + " to " + computerWin;
var computerIsWinning = (computerWin > userWin);
if(computerWin == userWin){
winningMessage = "We are tied " + userWin + " to " + computerWin;
}
else if(computerIsWinning){
winningMessage = "I am winning " + computerWin + " to " + userWin;
}
alert("You win " + userTotal + " to " + computerTotal + "\n\n" + winningMessage);
}
else{
alert("Tie! Roll Again! \n\n");
}
if(userPoints == 5){
alert("You win the game!");
}
else if(computerPoints == 5){
alert("The computer wins the game!");
}
}
userPoints = 0;
computerPoints = 0;
}
play();
干杯!
每次我 运行 程序,都会出现相同的骰子数字。此外,我的 while 语句最终显示用户和计算机都获胜。我也只想进行最多五轮,之后程序显示游戏的获胜者。我如何获得它,以便只有拥有较高数字的玩家才能赢得这一轮。
<script>
var computerRandomNumberOne = Math.floor((Math.random() * 10) + 1);
var computerRandomNumberTwo = Math.floor((Math.random() * 10) + 1);
var userRandomNumberOne = Math.floor((Math.random() * 10) + 1);
var userRandomNumberTwo = Math.floor((Math.random() * 10) + 1);
var userPoints;
var computerPoints;
var userTotal;
var computerTotal;
var userWin = 0;
var computerWin = 0;
alert("Let's shake some dice!");
userTotal = userRandomNumberOne + userRandomNumberTwo;
computerTotal = computerRandomNumberOne + computerRandomNumberTwo;
while(userPoints || computerPoints != 5)
{
alert("Your turn to roll \n\nYou shook a " + userRandomNumberOne + " and a " + userRandomNumberTwo + ", so you have " + userTotal);
alert("My turn to roll \n\nI shook a " + computerRandomNumberOne + " and a " + computerRandomNumberTwo + ", so I have " + computerTotal);
if(computerTotal > userTotal)
{
computerWin++;
computerPoints++;
alert("I win " + computerTotal + " to " + userTotal + "\n\nI am winning " + computerWin + " to " + userWin);
}
else if(computerTotal < userTotal)
{
userWin++;
userPoints++;
alert("You win " + userTotal + " to " + computerTotal+ "\n\nYou win " + computerTotal + " to " + userTotal + "\n\nYou are winning " + computerWin + " to " + userWin);
}
if(computerTotal == userTotal)
{
alert("Tie! Roll Again! \n\n");
}
if(userPoints == 5)
{
alert("You win the game!");
}
else if(computerPoints == 5)
{
alert("The computer wins the game!");
}
}
</script>
给你!有几处不对劲。在 while 循环的 () 中,||必须分隔完整的布尔语句。您必须先给变量赋值,然后再添加它们。在其中一个提醒中,您已经连续两次明确输入 "You win" 短语。你的 "I am/You are winning" 声明 运行 如果你刚刚赢得了回合而不是检查整个游戏的状态。您不会在循环中重新计算骰子掷骰,因此它每次都使用相同的数字。我假设您希望 userWin 和 computerWin 是每个玩家赢得的整个游戏数量的 运行ning 总数,但它的编码与回合总数相同(不会像他们那样每回合重置为 0应该)。我相信就是这样。这是修改后的代码:
var userWin = 0;
var computerWin = 0;
function play(){
var userTotal = 0;
var computerTotal = 0;
var userPoints = 0;
var computerPoints = 0;
alert("Let's shake some dice!");
while(userPoints < 5 && computerPoints < 5){
var computerRandomNumberOne = Math.floor((Math.random() * 10) + 1);
var computerRandomNumberTwo = Math.floor((Math.random() * 10) + 1);
var userRandomNumberOne = Math.floor((Math.random() * 10) + 1);
var userRandomNumberTwo = Math.floor((Math.random() * 10) + 1);
userTotal = userRandomNumberOne + userRandomNumberTwo;
computerTotal = computerRandomNumberOne + computerRandomNumberTwo;
alert("Your turn to roll \n\nYou shook a " + userRandomNumberOne + " and a " + userRandomNumberTwo + ", so you have " + userTotal);
alert("My turn to roll \n\nI shook a " + computerRandomNumberOne + " and a " + computerRandomNumberTwo + ", so I have " + computerTotal);
if(computerTotal > userTotal){
computerWin++;
computerPoints++;
var winningMessage = "You are winning " + userWin + " to " + computerWin;
var computerIsWinning = (computerWin > userWin);
if(computerWin == userWin){
winningMessage = "We are tied " + userWin + " to " + computerWin;
}
else if(computerIsWinning){
winningMessage = "I am winning " + computerWin + " to " + userWin;
}
alert("I win " + computerTotal + " to " + userTotal + "\n\n" + winningMessage);
}
else if(computerTotal < userTotal){
userWin++;
userPoints++;
var winningMessage = "You are winning " + userWin + " to " + computerWin;
var computerIsWinning = (computerWin > userWin);
if(computerWin == userWin){
winningMessage = "We are tied " + userWin + " to " + computerWin;
}
else if(computerIsWinning){
winningMessage = "I am winning " + computerWin + " to " + userWin;
}
alert("You win " + userTotal + " to " + computerTotal + "\n\n" + winningMessage);
}
else{
alert("Tie! Roll Again! \n\n");
}
if(userPoints == 5){
alert("You win the game!");
}
else if(computerPoints == 5){
alert("The computer wins the game!");
}
}
userPoints = 0;
computerPoints = 0;
}
play();
我保留了你的随机性,但如果你想让它更干净一点,你可以使用 randojs.com 让它变得简单。您所要做的就是输入以下内容:
rando(1, 10)
而不是每次都这样:
Math.floor((Math.random() * 10) + 1)
但这是一个偏好问题!如果您想使用 randojs.com,只需在 HTML 文档的 head 标签内的顶部添加:
<script src="https://randojs.com/1.0.0.js"></script>
然后您就可以改用此代码了:
var userWin = 0;
var computerWin = 0;
function play(){
var userTotal = 0;
var computerTotal = 0;
var userPoints = 0;
var computerPoints = 0;
alert("Let's shake some dice!");
while(userPoints < 5 && computerPoints < 5){
var computerRandomNumberOne = rando(1, 10);
var computerRandomNumberTwo = rando(1, 10);
var userRandomNumberOne = rando(1, 10);
var userRandomNumberTwo = rando(1, 10);
userTotal = userRandomNumberOne + userRandomNumberTwo;
computerTotal = computerRandomNumberOne + computerRandomNumberTwo;
alert("Your turn to roll \n\nYou shook a " + userRandomNumberOne + " and a " + userRandomNumberTwo + ", so you have " + userTotal);
alert("My turn to roll \n\nI shook a " + computerRandomNumberOne + " and a " + computerRandomNumberTwo + ", so I have " + computerTotal);
if(computerTotal > userTotal){
computerWin++;
computerPoints++;
var winningMessage = "You are winning " + userWin + " to " + computerWin;
var computerIsWinning = (computerWin > userWin);
if(computerWin == userWin){
winningMessage = "We are tied " + userWin + " to " + computerWin;
}
else if(computerIsWinning){
winningMessage = "I am winning " + computerWin + " to " + userWin;
}
alert("I win " + computerTotal + " to " + userTotal + "\n\n" + winningMessage);
}
else if(computerTotal < userTotal){
userWin++;
userPoints++;
var winningMessage = "You are winning " + userWin + " to " + computerWin;
var computerIsWinning = (computerWin > userWin);
if(computerWin == userWin){
winningMessage = "We are tied " + userWin + " to " + computerWin;
}
else if(computerIsWinning){
winningMessage = "I am winning " + computerWin + " to " + userWin;
}
alert("You win " + userTotal + " to " + computerTotal + "\n\n" + winningMessage);
}
else{
alert("Tie! Roll Again! \n\n");
}
if(userPoints == 5){
alert("You win the game!");
}
else if(computerPoints == 5){
alert("The computer wins the game!");
}
}
userPoints = 0;
computerPoints = 0;
}
play();
干杯!