Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function rps() {
- if (!confirm('Do you want to play Rock Paper Scissors?'))
- return;
- playRPS();
- function playRPS() {
- var userChoice = prompt('Choose Rock, Paper, or Scissors!\n\nYou can shorten it to \"R\",\"P\", or \"S\"\nCapitals don\'t matter.')
- if (userChoice == null)
- return;
- userChoice = userChoice.toLowerCase();
- switch (Math.round(Math.random() * (3 - 1) + 1)) {
- case 1:
- var compChoice = "rock";
- break;
- case 2:
- var compChoice = "paper";
- break;
- case 3:
- var compChoice = "scissors";
- break;
- }
- switch (userChoice) {
- case 'r':
- userChoice = 'rock';
- break;
- case 'p':
- userChoice = 'paper';
- break;
- case 's':
- userChoice = 'scissors'
- break;
- }
- if (userChoice == 'rock') {
- switch (compChoice) {
- case 'rock':
- if (!confirm('It was a tie!\nBoth opponents chose Rock.\n\nPlay again?'))
- return;
- playRPS();
- break;
- case 'paper':
- if (!confirm('You lost!\nThe opponent chose Paper, while you chose Rock.\n\nPlay again?'))
- return;
- playRPS();
- break;
- case 'scissors':
- if (!confirm('You won!\nThe opponent chose Scissors, while you chose Rock.\n\nPlay again?'))
- return;
- playRPS();
- break;
- }
- } else if (userChoice == 'paper') {
- switch (compChoice) {
- case 'rock':
- if (!confirm('You won!\nThe opponent chose Rock, while you chose Paper.\n\nPlay again?'))
- return;
- playRPS();
- break;
- case 'paper':
- if (!confirm('It was a tie!\nBoth opponents chose Paper.\n\nPlay again?'))
- return;
- playRPS();
- break;
- case 'scissors':
- if (!confirm('You lost!\nThe opponent chose Scissors, while you chose Paper.\n\nPlay again?'))
- return;
- playRPS();
- break;
- }
- } else if (userChoice == 'scissors') {
- switch (compChoice) {
- case 'rock':
- if (!confirm('You lost!\nThe opponent chose Rock, while you chose Scissors.\n\nPlay again?'))
- return;
- playRPS();
- break;
- case 'paper':
- if (!confirm('You won!\nThe opponent chose Paper, while you chose Scissors.\n\nPlay again?'))
- return;
- playRPS();
- break;
- case 'scissors':
- if (!confirm('It was a tie!\nBoth opponents chose Scissors.\n\nPlay again?'))
- return;
- playRPS();
- break;
- }
- } else {
- alert('Invalid input!\nPlease type one of the valid choices.');
- return;
- }
- }
- }
- rps();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement