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!
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>