如何让我的骰子游戏程序在每次掷骰子时生成新的"dice"数字



每次我运行程序时,骰子都会出现相同的数字。此外,我的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 nnYou shook a " + userRandomNumberOne + " and a " + userRandomNumberTwo + ", so you have " + userTotal);
alert("My turn to roll nnI shook a " + computerRandomNumberOne + " and a " + computerRandomNumberTwo + ", so I have " + computerTotal);
if(computerTotal > userTotal)
{
computerWin++;
computerPoints++;
alert("I win " + computerTotal + " to " + userTotal + "nnI am winning " + computerWin + " to " + userWin);
}
else if(computerTotal < userTotal)
{
userWin++;
userPoints++;
alert("You win " + userTotal + " to " + computerTotal+ "nnYou win " + computerTotal + " to " + userTotal + "nnYou are winning " + computerWin + " to " + userWin);
}
if(computerTotal == userTotal)
{   
alert("Tie! Roll Again! nn");
}
if(userPoints == 5)
{
alert("You win the game!");
}
else if(computerPoints == 5)
{
alert("The computer wins the game!");
}
}   
</script>

你去吧!有几处不对劲。在 while 循环的 (( 中,||必须分隔完整的布尔语句。在向变量添加变量之前,必须为变量赋值。在其中一个警报中,您连续两次明确键入"您赢了"短语。如果您刚刚赢了一轮,您的"我是/您正在获胜"语句就会运行,而不是检查整个游戏的状态。您不会在循环中重新计算掷骰子,因此它每次都使用相同的数字。我假设您希望 userWin 和 computerWin 运行每个玩家赢得的整个游戏数量的总数,但它的编码与回合总数相同(不会像它们应该的那样每轮重置为 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 nnYou shook a " + userRandomNumberOne + " and a " + userRandomNumberTwo + ", so you have " + userTotal);
alert("My turn to roll nnI 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 + "nn" + 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 + "nn" + winningMessage);
}
else{   
alert("Tie! Roll Again! nn");
}
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 nnYou shook a " + userRandomNumberOne + " and a " + userRandomNumberTwo + ", so you have " + userTotal);
alert("My turn to roll nnI 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 + "nn" + 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 + "nn" + winningMessage);
}
else{   
alert("Tie! Roll Again! nn");
}
if(userPoints == 5){
alert("You win the game!");
}
else if(computerPoints == 5){
alert("The computer wins the game!");
}
}
userPoints = 0;
computerPoints = 0;
}
play();

干杯!

最新更新