我如何获得我的骰子游戏程序以在每次掷骰子时生成新的“骰子”数字

时间:2019-12-03 18:03:30

标签: javascript random dice

每次我运行该程序时,都会出现相同的骰子数字。另外,我的while语句最终显示用户和计算机获胜。我也只想进行最多5轮比赛,之后程序会显示游戏的获胜者。如何获取,以便只有号码更大的玩家才能赢得比赛。

   <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>

1 个答案:

答案 0 :(得分:0)

您在这里!有几处错误。在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 \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();

干杯!