Next Steps (RPS): Rock, Paper,Scissors, Lizard, Spock


#1

Hello guys, so I've extended my code from "Rock, Paper, Scissors" to "Rock, Paper, Scissors, Lizard, Spock". It works for me. Maybe there's a much shorter way of writing it. I'm just too excited because it's my first working code ever (Yea, I just started with programming). Hope this helps anyone looking to try "Rock, Paper, Scissors, Spock" :slightly_smiling:

var userChoice = prompt("Do you choose rock, paper, scissors, lizard or spock?");

// if user makes inappropriate choice like "dog"
if(userChoice === "dog"){      
    
    // ask user for an appropriate choice
    var appropriate = prompt("Inappropriate choice! Please choose again!"); 
    //set userChoice to appropriate
    userChoice = appropriate;
}
var computerChoice = Math.random();

// Get computerChoice in random
if (computerChoice <= 0.14) {
    computerChoice = "rock";
} 
else if(computerChoice <= 0.37) {
    computerChoice = "paper";
} 
else if(computerChoice <= 0.53){
    computerChoice = "scissors";
} 
else if(computerChoice <= 0.75){
    computerChoice = "lizard";
}
else{
        computerChoice = "spock";
 }

// Print computerChoice
console.log("Computer: " + computerChoice);

// Now compare both userChoice with computerChoice
var compare = function(choice1,choice2){
    if (choice1 === choice2){
        // Request user for new choice if equals computerChoice
        var newUserChoice = prompt("Please try again!");
        choice1 = newUserChoice;
        return choice1;
        
        // If userChoice is "rock"
        }
        else if(choice1 === "rock"){
                if (choice2 === "scissors"){
                    return "Rock crushes Scissors";
                    }
                    else if(choice2 === "lizard"){
                            return "Rock crushes Lizard";
                    }
                    else if(choice2 === "paper"){
                            return "Paper covers Rock";
                    }
                    else{
                            return "Spock vaporizes Rock";
                        }
            }

         // If userChoice is "paper"
         else if(choice1 === "paper"){
                if (choice2 === "rock"){
                        return "Paper covers Rock";
                    }
                    else if(choice2 === "lizard"){
                            return "Lizard eats Paper";
                    }
                    else if(choice2 === "spock"){
                            return "Paper disproves Spock";
                    }
                    else{
                            return "Scissors cuts Paper";
                        }
             }

         // if userChoice is "scissors"
         else if(choice1 === "scissors"){
                if (choice2 === "rock"){
                        return "Rock crushes Scissors";
                    }
                    else if(choice2 === "lizard"){
                            return "Scissors decapitates Lizard";
                    }
                    else if(choice2 === "spock"){
                            return "Spock smashes Scissors";
                    }
                    else{
                            return "Scissors cuts Paper";
                    }
             }

         // if userChoice is "lizard"
         else if(choice1 === "lizard"){
                if(choice2 === "spock"){
                        return "Lizard poisons Spock;"
                    }
                    else if(choice2 === "scissors"){
                        return "Scissors decapitates Lizard";    
                    }
                    else if(choice2 === "paper"){
                        return "Lizard eats Paper";
                    }
                    else{
                            return "Rock crushes Lizard";
                        }
             }

         // if userChoice is "spock"
         else if(choice1 === "spock"){
                if(choice2 === "scissors"){
                    return "Spock smashes Scissors";
                }
                else if(choice2 === "paper"){
                    return "Paper disproves Spock";
                }
                else if(choice2 === "rock"){
                    return "Spock vaporizes Rock";    
                }
                else{
                        return "Lizard poisons Spock";
                    }
             }
}

compare(userChoice,computerChoice);

"Rock, Paper, Scissors" part 8
#2

Pretty good :+1:
Suggestions:
Currently your picks for computerChoice are a bit unfair:
14% chance to get rock
37-14=23 % chance to get paper
53-37 =16% chance to get scissors
75-53 = 22% chance to get lizard
100-75 = 25% chance to get spock

Why not make it easier and use <= 1/5, <=2/5 aso so you're save that all have equal chance and do not even need to bother about the exact numbers.

And for the tie case:

    var newUserChoice = prompt("Please try again!");
    choice1 = newUserChoice;
    return choice1;

This lets the user pick again and returns the new choice which might be pretty strange:

User: Tell me who wins: "Rock vs Rock"
Almighty compare function: Spock
User: ????

I would recommend that you just stick with the tie message and in the end get the result of

compare(userChoice,computerChoice);

and use this to determine your next steps:
- console.log
- let player and user choose again and run compare a second time


#3

Awesome Job!

I am just going to give a few pointers to clean up your code a little and give you a little more functionality :slightly_smiling:

 else if(choice1 === "spock"){
            switch(choice2){
                case "scissors":
                    return "Spock smashes Scissors";
                    break;
                case "paper":
                    return "Paper disproves Spock";
                    break;
                case "rock":
                    return "Spock vaporizes Rock";    
                    break;
                default:
                    return "Lizard poisons Spock";
            }
        }
  1. You could have each if else go into a switch/case statement, this would get rid of alot of else if's and if's that are cluttering your code.

  2. I would take the user prompt and put in toLowerCase() incase they decide to capitalize the first letter of their choice :slightly_smiling:

var userChoice = prompt("Do you choose rock, paper, scissors, lizard or spock?").toLowerCase();
``` 

3. You should add something to take care of the instance that a user does not enter a correct item

```
if(userChoice !== "rock" || userChoice !== "paper" || userChoice !== "scissors" || userChoice !== "lizard"|| userChoice !== "spock" || userChoice !== "dog"){
    alert("This item does not exist, please type rock,paper,scissors,lizard, or spock :)")
}

Hope that helps :sunny:


#4

And you would not even need the break as return exits the function anyway :wink:


#5

Oh yeah :slightly_smiling: , you're right.


#6

Thanks for the suggestions and tips. I sure will try them out. Yea, about computerChoice, I was just too excited to get it working than being fair :stuck_out_tongue_closed_eyes:. But your explanations have made me understand that I need to a bit more critical with my code. Thanks once again and I'll repost the new cleaned out code.


#7

Thanks so much for this. It'll help me understand more about writing clean code and also minimize the else if's and if's. This is incredible tips here. Merci beaucoup :relaxed: