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>
Here you go! There were a few things wrong. Within the () of the while loop, || must separate complete boolean statements. You must assign values to variables before adding to them. In one of the alerts, you've explicitly typed out a "You win" phrase twice in a row. Your "I am/You are winning" statements run if you've just won the round instead of checking on the status of the game as a whole. You aren't recalculating dice rolls within the loop, so it just uses the same numbers every time. I'm assuming you want userWin and computerWin to be running totals of the number of entire games each player has won, but it's coded the same as the round totals (which don't reset to 0 each round like they should). I believe that's it. Here's the revised code:
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();
I've left the randomness as you had it, but if you want to make it a little cleaner, you can use randojs.com to make it simple. All you'd have to do is type this:
rando(1, 10)
instead of this everytime:
Math.floor((Math.random() * 10) + 1)
But that's a matter of preference! If you want to use randojs.com, just add this at the top within the head tag of your HTML document:
<script src="https://randojs.com/1.0.0.js"></script>
and then you'll be able to use this code instead:
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();
Cheers!