ExtremeGamer1480

Basic JavaScript RPS Game

Mar 3rd, 2021 (edited)
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function rps() {
  2.     var playornah = confirm('Do you want to play Rock Paper Scissors?')
  3.     if (playornah == true) {
  4.         playRPS();
  5.     }
  6.  
  7.     function playRPS() {
  8.         var userChoice = prompt('Choose Rock, Paper, or Scissors!\n\n \(All lowercase.\)')
  9.         switch (Math.round(Math.random() * (3- 1) + 1)) {
  10.             case 1:
  11.                 compChoice = "rock";
  12.                 break;
  13.             case 2:
  14.                 compChoice = "paper";
  15.                 break;
  16.             case 3:
  17.                 compChoice = "scissors";
  18.                 break;
  19.         }
  20.         if (compChoice == "rock" && userChoice == "rock") {
  21.             playagain = confirm('It was a tie!\nBoth opponents chose Rock!\n\nPlay again?')
  22.             if (playagain == true) {
  23.                 playRPS();
  24.             } else {
  25.                 alert('Closing Game.')
  26.             }
  27.         } else if (compChoice == "paper" && userChoice == "paper") {
  28.             playagain = confirm('It was a tie!\nBoth opponents chose Paper!\n\nPlay again?')
  29.             if (playagain == true) {
  30.                 playRPS();
  31.             } else {
  32.                 alert('Closing Game.')
  33.             }
  34.         } else if (compChoice == "scissors" && userChoice == "scissors") {
  35.             playagain = confirm('It was a tie!\nBoth opponents chose Scissors!\n\nPlay again?')
  36.             if (playagain == true) {
  37.                 playRPS();
  38.             } else {
  39.                 alert('Closing Game.')
  40.             }
  41.         } else if (compChoice == "rock" && userChoice == "scissors") {
  42.             playagain = confirm('You lost!\nThe opponent chose Rock, while you chose Scissors!\n\nPlay again?')
  43.             if (playagain == true) {
  44.                 playRPS();
  45.             } else {
  46.                 alert('Closing Game.')
  47.             }
  48.         } else if (compChoice == "rock" && userChoice == "paper") {
  49.             playagain = confirm('You won!\nThe opponent chose Rock, while you chose Paper!\n\nPlay again?')
  50.             if (playagain == true) {
  51.                 playRPS();
  52.             } else {
  53.                 alert('Closing Game.')
  54.             }
  55.         } else if (compChoice == "paper" && userChoice == "rock") {
  56.             playagain = confirm('You lost!\nThe opponent chose Paper, while you chose Rock!\n\nPlay again?')
  57.             if (playagain == true) {
  58.                 playRPS();
  59.             } else {
  60.                 alert('Closing Game.')
  61.             }
  62.         } else if (compChoice == "paper" && userChoice == "scissors") {
  63.             playagain = confirm('You won!\nThe opponent chose Paper, while you chose Scissors!\n\nPlay again?')
  64.             if (playagain == true) {
  65.                 playRPS();
  66.             } else {
  67.                 alert('Closing Game.')
  68.             }
  69.         } else if (compChoice == "scissors" && userChoice == "rock") {
  70.             playagain = confirm('You won!\nThe opponent chose Scissors, while you chose Rock!\n\nPlay again?')
  71.             if (playagain == true) {
  72.                 playRPS();
  73.             } else {
  74.                 alert('Closing Game.')
  75.             }
  76.         } else if (compChoice == "scissors" && userChoice == "paper") {
  77.             playagain = confirm('You lost!\nThe opponent chose Scissors, while you chose Paper!\n\nPlay again?')
  78.             if (playagain == true) {
  79.                 playRPS();
  80.             } else {
  81.                 alert('Closing Game.')
  82.             }
  83.         } else if (userChoice == null) {
  84.             alert('Closing Game.')
  85.         } else {
  86.             alert('Invalid Input.\nPlease type all inputs in lowercase with correct spelling.')
  87.             playRPS();
  88.         }
  89.     }
  90. }
  91. rps();
Add Comment
Please, Sign In to add comment