Ask both players for new choices


#1



Hi everyone!

I have this for the rock, paper, scissors and rope game! From what I can see, it's working fine (considering a couple of changes on the rules of the game).

Question is, I can't manage to do this "In this version, if both players make the same choice, the game returns a tie. What if the game didn't end there but instead asked both players for new choices?"

How can I solve this?

Thanks for your time guys!


var valid = function(){
   var userChoice = prompt("Do you choose rock, paper, rope or scissors?");
   if (userChoice == "rock"){
     return "rock";
   }  else if (userChoice == "paper") {
       return "paper";
   } else if (userChoice == "scissors") {
       return "scissors";
   } else if (userChoice == "rope") {
       return "rope";
   } else {
       console.log("check your input");
   }
  };
userChoice = valid();

var computerChoice = Math.random();
if (computerChoice < 0.25) {
	computerChoice = "rock";
} else if(computerChoice <= 0.50) {
	computerChoice = "paper";
} else if(computerChoice <= 0.75) {
    computerChoice = "rope";
} else {
	computerChoice = "scissors";
} console.log("Computer: " + computerChoice);


var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
    return "The result is a tie!";
    
    } else if(choice1 === "rock") {
        if(choice2 === "paper") {
            return "rock wins";
        } else if(choice2 === "rope") {
            return "rock wins";
        } else {
            return "scissors wins";
        }
    }
    
    else if(choice1 === "paper") {
        if(choice2 === "rope") {
            return "paper wins";
        } else if(choice2 === "scissors") {
            return "paper wins";
        } else {
            return "rock wins";
        }
    }
    
    else if(choice1 === "rope") {
        if(choice2 === "scissors") {
            return "rope wins";
        } else if(choice2 === "rock") {
            return "rope wins";
        } else {
            return "rope wins";
        }
    }
    
    
    else if(choice1 === "scissors") {
        if (choice2 === "rock") {
            return "scissors wins";
        } else if(choice2 === "paper") {
            return "scissors wins";
        } else {
            return "scissors wins";
        }
    }
};

compare(userChoice, computerChoice);


#2

Hint:

Move your entire game logic inside one main function. If the choices happen to be the same, call the main function again.


#3

Great! I'm going to try that.

Thanks for your help!

Best.


#4

Hi gaurangtandon, sorry to bother you again but i'm still not sure how to solve this.

I have this:

var main = function(choice1, choice2) {
    if (choice1 === choice2) {
    main(choice1, choice2);
    
    } else if(choice1 === "rock") {
        if(choice2 === "paper") {
            return "rock wins";
        } else if(choice2 === "rope") {
            return "rock wins";
        } else {
            return "scissors wins";
        }
    } (continues)

I'm clearly failing to call the function again when choice1 === choice2.

How can I solve that?

Thanks for your help!!


#5

I said your entire game logic, that is, the code that handles prompting the user, should also be inside the main function :wink:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.