mardi 3 décembre 2019

How do I get my dice game program to generate new "dice" numbers each roll

Each time I run the program, the same numbers for the dice appear. In addition, my while statements end up displaying both the user and the computer winning. I also only want to have rounds that go up to five, after that the program displays the winner of the game. How do I get it so that only the player with the higher number wins the round.

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



Aucun commentaire:

Enregistrer un commentaire