Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- alert("Welcome to Rock, Paper, Scissors, Lizard, Spock\n\t\t*Programmed by Schamanu*");
- /*Variables and game setup*/
- var played = 0;
- var score1 = 0;
- var score2 = 0;
- var times = Number(prompt("How often do you want to play?"));
- if (isNaN(times)) {
- times = 1;
- alert("\nInvalid input!\nNumber of rounds set to 1!");
- }
- else
- {
- alert("\nYou chose to play "+ times +" times.");
- /*FUNCTION COMPARING CHOICES*/
- var compare=function(choice1,choice2) {
- if (choice1===choice2) {
- alert("Its a tie. Choose again!");
- }
- else if (choice1==="rock") {
- if (choice2==="scissors") {
- alert("Rock crushes scissors.");
- alert("Player 1 wins this round.");
- score1+=1;
- }
- else if (choice2==="paper") {
- alert("Paper covers rock.");
- alert("The computer wins this round.");
- score2+=1;
- }
- else if (choice2==="spock") {
- alert("Spock vaporizes rock.");
- alert("The computer wins this round.");
- score2+=1;
- }
- else { // choice2=lizard
- alert("Rock crushes lizard.");
- alert("Player 1 wins this round.");
- score1+=1;
- }
- }
- else if (choice1==="paper") {
- if (choice2==="rock") {
- alert("Paper covers rock.");
- alert("Player 1 wins this round.");
- score1+=1;
- }
- else if (choice2==="scissors") {
- alert("Scissors cut paper.");
- alert("The computer wins this round.");
- score2+=1;
- }
- else if (choice2==="spock") {
- alert("Paper disproves Spock.");
- alert("Player 1 wins this round.");
- score1+=1;
- }
- else { // choice2=lizard
- alert("Lizard eats paper.");
- alert("The computer wins this round.");
- score2+=1;
- }
- }
- else if (choice1==="scissors") {
- if (choice2==="rock") {
- alert("Rock crushes scissors.");
- alert("The computer wins this round.");
- score2+=1;
- }
- else if (choice2==="paper") {
- alert("Scissors cut paper.");
- alert("Player 1 wins this round.");
- score1+=1;
- }
- else if (choice2==="spock") {
- alert("Spock smashes scissors.");
- alert("The computer wins this round.");
- score2+=1;
- }
- else { // choice2=lizard
- alert("Scissors decapitate lizard.");
- alert("Player 1 wins this round.");
- score1+=1;
- }
- }
- else if (choice1==="spock") {
- if (choice2==="rock") {
- alert("Spock vaporizes rock.");
- alert("Player 1 wins this round.");
- score1+=1;
- }
- else if (choice2==="paper") {
- alert("Paper disproves Spock.");
- alert("The computer wins this round.");
- score2+=1;
- }
- else if (choice2==="scissors") {
- alert("Spock smashes scissors.");
- alert("Player 1 wins this round.");
- score1+=1;
- }
- else { // choice2=lizard
- alert("Lizard poisons Spock.");
- alert("The computer wins this round.");
- score2+=1;
- }
- }
- else if (choice1==="lizard") {
- if (choice2==="rock") {
- alert("Rock crushes lizard.");
- alert("The computer wins this round.");
- score2+=1;
- }
- else if (choice2==="paper") {
- alert("Lizard eats paper.");
- alert("Player 1 wins this round.");
- score1+=1;
- }
- else if (choice2==="scissors") {
- alert("Scissors decapitate lizard.");
- alert("The computer wins this round.");
- score2+=1;
- }
- else { // choice2=spock
- alert("Lizard poisons Spock.");
- alert("Player 1 wins this round.");
- score1+=1;
- }
- }
- };
- /*END OF FUNCTION*/
- do { // repeats game x times, as specified by player
- played += 1;
- do { // repeats code as long as game is tied
- do { // repeats code until player enters proper input
- if (userChoice==="rules") {
- alert("\nRules:\nScissors cuts paper. Paper covers rock.");
- alert("Rock crushes lizard. Lizard poisons Spock.\nSpock smashes scissors. Scissors decapitates lizard.");
- alert("Lizard eats paper. Paper disproves Spock.\nSpock vaporizes rock. Rock crushes scissors.");
- }
- var userChoice = prompt("Do you choose rock, paper, scissors, lizard or spock?","Type <rules> to see the rules.").toLowerCase();
- } while (userChoice !="rock" && userChoice !="paper" && userChoice !="scissors" && userChoice !="spock" && userChoice !="lizard");
- 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 = "spock";
- }
- else {
- computerChoice = "lizard";
- }
- if (times >1) {
- alert("\nRound "+ played+"/"+times);
- }
- alert("\nYour choice: "+ userChoice);
- alert("Computer: " + computerChoice);
- compare(userChoice,computerChoice);
- } while (userChoice === computerChoice);
- } while (played <= times-1); // times minus 1 because its a do-loop
- if (times >1) {
- alert("\nFinal Score: "+ score1+":"+score2);
- if (score1>score2) {
- alert("Player 1 is the overall winner!");
- }
- else if (score2>score1) {
- alert("The computer is the overall winner!");
- }
- else {
- alert("It's a tie after all.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment