9. Next Steps: What if you get a tie and want to start over?


#1

I am working on my rock, paper, scissors game. What if the player gets a tie and wants to play again? I have coded this so far (I highlighted the part where it gets important):

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
console.log("You: " + userChoice);
var compare=function (choice1, choice2) {

if (choice1 === choice2) {

return prompt("The result is a tie. Play again? Write yes or no in the box below.")
var decide=function(word) {
if (word==="yes") {
var userChoice = prompt("Do you choose rock, paper or scissors?") }
else {
return confirm("I am finished playing this awesome rock, paper, scissors game.")
}
}
}
else if (choice1==="rock") {
if (choice2==="scissors") {
return "rock wins";
}
else {
return "paper wins";
}
}
else if (choice1==="scissors") {
if (choice2==="rock") {
return "rock wins";
}
else {
return "scissors wins";
}
}
else if (choice1==="paper") {
if (choice2==="rock") {
return "paper wins";
}
else {
return "rock wins";
}
}
}
compare(userChoice,computerChoice);

Now if they choose rock, paper or scissors based on the prompt I included, how can I get the game to start over again? Thanks!


#2

Hi @datarunner73465

I'm changing some lines in your code, tell me what do you think about it :

var userChoice;
var computerChoice;
var getReplay = true;

while(getReplay){
    computerChoice = Math.random();
    
    if(computerChoice < 0.34){
        computerChoice = "rock";
    }else if(computerChoice <= 0.67){
        computerChoice = "paper";
    }else{
        computerChoice = "scissors";
    };
    
    var userChoice = prompt("Do you choose rock, paper or scissors?");

    console.log("Computer: " + computerChoice);
    console.log("You: " + userChoice);
    
    var compare = function(choice1, choice2){
    if(choice1 === choice2){
            console.log("The result is a tie.");
    }else if(choice1 === "rock"){
        if(choice2 === "scissors"){
            console.log("rock wins");
        }else{
            console.log("paper wins");
        };
    }else if(choice1 === "scissors"){
        if(choice2 === "rock") {
            console.log("rock wins");
        }else{
            console.log("scissors wins");
        };
    }else if(choice1 === "paper"){
        if(choice2 === "rock"){
            console.log("paper wins");
        }else{
            console.log("scissors wins");
        };
        };
    };
    
    compare(userChoice,computerChoice);
        
if(confirm("Do you want to play again ?")){
getReplay = true;
}else{
getReplay = false;    
};
};

Hold on bro :wink:


#3

I appreciate your help @onepunch_man. I do have another question. When I enter that code, it automatically asks if I want to play again rather than stopping and saying "rock wins" etc. Thoughts?

Thanks again!


#4

It automatically asks if user wants to play again because I add a while loop who check the confirm after the code, while confirm is true the code's going to run and asks again and again and again when confirm will be false.

Did I answer to your question ?


#5

@onepunch_man Sorry, I did not word my question very clearly. How do I get the program to stop so that the user can see the result ("rock wins" etc) and THEN say would you like to play again? Or is that not possible?

Thanks


#6

Oh ok, so you can change "console.log" to "alert" inside your "if(choice1 === choice2)"
Test it and tell me


#7

No luck. Hmmmm. . .

Thanks again for your help!

Bailey


#8

Try this :

var userChoice;
var computerChoice;
var getReplay = true;

while(getReplay){
    computerChoice = Math.random();
    
    if(computerChoice < 0.34){
        computerChoice = "rock";
    }else if(computerChoice <= 0.67){
        computerChoice = "paper";
    }else{
        computerChoice = "scissors";
    };
    
    var userChoice = prompt("Do you choose rock, paper or scissors?");

    console.log("Computer: " + computerChoice);
    console.log("You: " + userChoice);
    
    var compare = function(choice1, choice2){
    if(choice1 === choice2){
            return("The result is a tie.");
    }else if(choice1 === "rock"){
        if(choice2 === "scissors"){
            return("rock wins");
        }else{
            return("paper wins");
        };
    }else if(choice1 === "scissors"){
        if(choice2 === "rock") {
            return("rock wins");
        }else{
            return("scissors wins");
        };
    }else if(choice1 === "paper"){
        if(choice2 === "rock"){
            return("paper wins");
        }else{
            return("scissors wins");
        };
        };
    };
    
var s = compare(userChoice,computerChoice);
alert (s)
        
if(confirm("Do you want to play again ?")){
getReplay = true;
}else{
getReplay = false;    
};
};

I changed your console.log's to returns and then added the variable for compare and then alerted the variable.

Hope that helps.


#9

I then decided to change

  console.log("Computer: " + computerChoice);
  console.log("You: " + userChoice);

to

alert("Computer: " + computerChoice);
alert("You: " + userChoice);

so that the whole game is played and displayed in prompts.


#10

Victory! Thanks you @jon4th4nhunt3r and @onepunch_man for your help!