Just wanted to share my rock, paper, scissors, lizard, Spock. Any suggestions for ties or invalid entries?


#1

4.9 "Next Steps"

I've been having fun with this, and I decided to add Spock and lizard into the pool of choices. What can I do automatically issue the prompt again if it's a tie or if the user input is an invalid choice? I haven't done the loops lesson yet, so maybe that will provide some ideas. Also, is there a way to have a prompt with click-able choices, instead of manually typing in? Maybe I'm getting ahead of myself. Thanks in advance!

var userChoice = prompt("Do you choose Rock, Paper, Scissors, Lizard, or Spock? Think carefully!");
var computerChoice = Math.random();
if (computerChoice <= 0.2) {
	computerChoice = "Rock";
} 
else if(computerChoice <= 0.4) {
	computerChoice = "Paper";
} 
else if(computerChoice <= 0.6) {
    computerChoice = "Scissors";
} 
else if(computerChoice <= 0.8) {
    computerChoice = "Lizard";
}
else computerChoice = "Spock";

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

var compare = function(choice1, choice2) {
    if(choice1 === choice2) {
        console.log("Tie! Play again.");
    }
    
    else if(choice1 === "Rock") {
        if(choice2 === "Scissors") {
            console.log("Rock crushes Scissors. You win! Suck it, computer!");
        }
        else if(choice2 === "Lizard") {
            console.log("Rock crushes Lizard. You win! Suck it, computer!");
        }
        else if(choice2 === "Paper") {
            console.log("Paper covers Rock. Computer wins! You suck!");
        }
        else {
            console.log("Spock vaporizes Rock. Computer wins! You suck!");
        }
    }
    
    else if(choice1 === "Paper") {
        if(choice2 === "Rock") {
            console.log("Paper covers Rock. You win!  Suck it, computer!");
        }
        else if(choice2 === "Spock") {
            console.log("Paper disproves Spock. You win! Suck it, computer!");
        }
        else if(choice2 === "Scissors") {
            console.log("Scissors cuts Paper. Computer wins! You suck!");
        }
        else {
            console.log("Lizard eats Paper. Computer wins! You suck!");
        }
    }
    
    else if(choice1 === "Scissors") {
        if(choice2 === "Paper") {
            console.log( "Scissors cuts Paper. You win!  Suck it, computer!");
        }
        else if(choice2 === "Lizard") {
            console.log("Scissors decapitates Lizard. You win! Suck it, computer!");
        }
        else if(choice2 === "Spock") {
            console.log("Spock smashes Scissors. Computer wins! You suck!");
        }
        else {
            console.log("Rock crushes Scissors. Computer wins!  You suck!");
        }
    }
    
    else if(choice1 === "Spock") {
        if(choice2 === "Rock") {
            console.log( "Spock vaporizes Rock. You win!  Suck it, computer!");
        }
        else if(choice2 === "Scissors") {
            console.log("Spock smashes Scissors. You win! Suck it, computer!");
        }
        else if(choice2 === "Paper") {
            console.log("Paper disproves Spock. Computer wins! You suck!");
        }
        else {
            console.log("Lizard poisons Spock. Computer wins!  You suck!");
        }
    }
    
    else if(choice1 === "Lizard") {
        if(choice2 === "Spock") {
            console.log( "Lizard poisons Spock. You win!  Suck it, computer!");
        }
        else if(choice2 === "Paper") {
            console.log("Lizard eats Paper. You win! Suck it, computer!");
        }
        else if(choice2 === "Rock") {
            console.log("Rock crushes Lizard. Computer wins! You suck!");
        }
        else {
            console.log("Scissors decapitates lizard. Computer wins!  You suck!");
        }
    }
    
    else {
        console.log("Go home. You're drunk.")
    }
} 

compare(userChoice,computerChoice)


#2

Your else statements should really have a set of { }. Wait until you get to cases, and loops. As for having prerecorded buttons, I'm not sure. For your current knowledge, you could put the prompt in an if statement that checks to see if what they typed are one of those things. The best thing to do is wait for loops though, so if they type an invalid input, it asks them again until they do.

while(True){
     userChoice = prompt("What do you choose?");

     if(userChoice === "Rock" || userChoice === "Paper" || userChoice === "Scissors"){
          break;
     }else{
          continue;
     }
}

My syntax may be a little bit off, but that creates a loop that runs forever until they enter a correct choice.


#3

Whoops! I didn't realize I left out the { } on that else statement. I think the rest had them though. I'll go through the loops lesson and revisit this later. Thanks for the response!


#4

Something you can do is to put the rules in a list and figure out who wins from that.

["Lizard eats Paper",
"Lizard poisons Spock",
"Paper covers Rock",
"Paper disproves Spock",
"Rock crushes Lizard",
"Rock crushes Scissors",
"Scissors cuts Paper",
"Scissors decapitates Lizard",
"Spock smashes Scissors",
"Spock vaporizes Rock"]

(should probably be re-ordered to how it's said in the show, I sorted it while removing duplicates)

See the pattern? The first word beats the last, that's enough information, then just code what to do with that information instead of repeating it for each possibility

This also makes it significantly easier to add or remove rules.


#5

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