Advertisement
ExtremeGamer1480

JavaScript Rock Paper Scissors

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