Advertisement
Fallen_Angel01134

rock paper scisoors

Jan 9th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.     // Your JavaScript goes here!
  3.     let playerScore = 0;
  4.     let computerScore = 0;
  5.     const PLAYER_WINS = 1;
  6.     const COMP_WINS = 0;
  7.  
  8.     function computerPlay() {
  9.       let computerTurn = Math.floor(Math.random() *3);
  10.       switch (computerTurn) {
  11.         case (0):
  12.           return "rock";
  13.           break;
  14.         case (1):
  15.           return "paper";
  16.           break;
  17.         case (2):
  18.           return "scissors";
  19.           break;
  20.       }
  21.     }
  22.  
  23.     function playRound(playerChoice, computerChoice) {
  24.       if (
  25.         (computerChoice == "paper" && playerChoice == "rock") ||
  26.         (computerChoice == "scissors" && playerChoice == "paper") ||
  27.         (computerChoice == "rock" && playerChoice == "scissors")
  28.       ) {
  29.         console.log("You lost the round");
  30.         return COMP_WINS;
  31.       } else if (
  32.         (playerChoice == "paper" && computerChoice == "rock") ||
  33.         (playerChoice == "scissors" && computerChoice == "paper") ||
  34.         (playerChoice == "rock" && computerChoice == "scissors")
  35.       ) {
  36.         console.log("You won the round");
  37.         return PLAYER_WINS;
  38.       } else {
  39.         console.log("It's a tie!")
  40.       }
  41.     }
  42.  
  43.     function game(n) {
  44.       for (let i=0; i<n; i++) {
  45.         let playerChoice = prompt("Rock, paper or scissors?: ".toLowerCase())
  46.         let computerChoice = computerPlay();
  47.         let resultRound = playRound(playerChoice, computerChoice);
  48.         if (resultRound == COMP_WINS) {
  49.           computerScore++;
  50.           console.log("Computer: " + computerScore);
  51.           console.log("Player: " + playerScore);
  52.         } else if (resultRound == PLAYER_WINS) {
  53.           playerScore++;
  54.           console.log("Computer: " + computerScore);
  55.           console.log("Player: " + playerScore);
  56.         }
  57.       }
  58.       if (playerScore < computerScore) {
  59.         console.log("The computer has won the game!")
  60.       } else if (playerScore > computerScore) {
  61.         console.log("The player has won the game!")
  62.       } else {
  63.         console.log("It is a tie!")
  64.       }
  65.     }
  66.  
  67.     let rounds = prompt("How many rounds would you like to play?: ")
  68.  
  69.     game(rounds)
  70.   </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement