Next Steps - Rock, Paper, Scissors, Lizard, Spock

Rock, Paper, Scissors, Lizard, Spock

Here is my simple extension to the Rock Paper Scissors game which you can play here: Codecademy Labs
Try it and tell me what you think! :slight_smile:

I have implemented the four challenges posed in ‘Next Steps’:
Q1) What if a user makes an inappropriate choice like ‘dog’?
Q2) How would it be valuable to wrap the entire game in one function?
Q3) What if players in the game could also choose Rope in this game?
Q4) What if the game didn’t with a tie but instead asked both players for new choices?

For Q3 it is impossible to have a fair game with only four choices so I went with 5. The rules for this game are based on an episode of The Big Bang Theory.
More info here: Big Bang Theory Wiki
Also I found this website based on this game: RPSLS Online game

```

/* Game rules are as follows:
* Scissors cut paper
* Paper covers rock
* Rock crushes lizard
* Lizard poisons Spock
* Spock smashes scissors
* Scissors decapitate lizard
* Lizard eats paper
* Paper disproves Spock
* Spock vaporizes rock
* Rock crushes scissors
*/

//Intro
confirm(“Try and beat the computer at Rock, Paper, Scissors, Lizard, Spock!”);

//Wraps entire game in one function (ANSWERS Q2)
var game = function() // ‘game’ function starts here
{
//‘rpsls’ function contains logic to determine the result
var rpsls = function(choice1, choice2)
{
//If user and computer’s choices match (ANSWERS Q4)
if (choice1 === choice2)
{
alert (“It’s a tie!”);
}
//If user picks ROCK
else if (choice1 == “rock”)
{
if (choice2 === “paper”)
{alert (“YOU LOSE! (paper covers rock)”);}
else if (choice2 === “scissors”)
{alert (“YOU WIN! (rock crushes scissors)”);}
else if (choice2 === “lizard”)
{alert (“YOU WIN! (rock crushes lizard)”);}
else //Computer must have chosen spock
{alert (“YOU LOSE! (spock vaporises rock)”);}
}
//If user picks PAPER
else if (choice1 === “paper”)
{
if (choice2 === “rock”)
{alert (“YOU WIN! (paper covers rock)”);}
else if (choice2 === “scissors”)
{alert (“YOU LOSE! (scissors cut paper)”);}
else if (choice2 === “lizard”)
{alert (“YOU LOSE! (lizard eats paper)”);}
else //Computer must have chosen spock
{alert (“YOU WIN! (paper disproves spock)”);}
}
//If user picks SCISSORS
else if (choice1 === “scissors”)
{
if (choice2 === “rock”)
{alert (“YOU LOSE! (rock crushes scissors)”);}
else if (choice2 === “paper”)
{alert (“YOU WIN! (scissors cut paper”);}
else if (choice2 === “lizard”)
{alert (“YOU WIN! (scissors decapitate lizard)”);}
else //Computer must have chosen spock
{alert (“YOU LOSE! (spock smashes scissors)”);}
}
//If user picks LIZARD
else if (choice1 === “lizard”)
{
if (choice2 === “rock”)
{alert (“YOU LOSE! (rock crushes lizard)”);}
else if (choice2 === “paper”)
{alert (“YOU WIN! (lizard eats paper)”);}
else if (choice2 === “scissors”)
{alert (“YOU LOSE! (scissors decapitate lizard)”);}
else //Computer must have chosen spock
{alert (“YOU WIN! (lizard poisons spock)”);}
}
//User must have picked SPOCK
else
{
if (choice2 === “rock”)
{alert (“YOU WIN! (spock vaporises rock)”);}
else if (choice2 === “paper”)
{alert (“YOU LOSE! (paper disproves spock”);}
else if (choice2 === “scissors”)
{alert (“YOU WIN! (spock smashes scissors)”);}
else //Computer must have chosen lizard
{alert (“YOU LOSE! (lizard poisons spock)”);}
}
};

//Players choose
var userChoice = prompt("Do you choose rock, paper, scissors, lizard or spock?");
//If user doesn't pick one of the options (ANSWERS Q1)
if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors" && userChoice !== "lizard" && userChoice !== "spock")
{
    alert ("You need to pick one of the five choices.");
} else {

    var computerChoice = Math.random();
    // 5 choices (ANSWERS Q3)
    if (computerChoice <=0.20)
    {computerChoice = "rock";}
    else if(computerChoice <=0.40)
    {computerChoice = "paper";}
    else if(computerChoice <=0.60)
    {computerChoice = "scissors";}
    else if(computerChoice <=0.80)
    {computerChoice = "lizard";}
    else
    {computerChoice = "spock";}
    
    console.log("You chose " + userChoice + " & the computer chose " + computerChoice);
    
    rpsls(userChoice,computerChoice);
}

//Choice to play again
var choice = prompt("Play again? 'yes' or 'no'");
var playagain = function (choice)
{
	if (choice === "yes")
	{
        game(); //Replays the game
    } else { 
        alert("Thank you for playing!");
    }	
};
playagain(choice);

}; // ‘game’ function ends here

//Call ‘game’ function
game();

<do not remove the three backticks above>

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