From dca60ccf1a0ec9b4c43f523094149355136551b8 Mon Sep 17 00:00:00 2001 From: Prabu Date: Thu, 7 Mar 2024 19:58:11 +0530 Subject: [PATCH] Finish all the necessary features for game --- index.html | 13 ++++++++++ script.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 index.html create mode 100644 script.js diff --git a/index.html b/index.html new file mode 100644 index 0000000..a704dfb --- /dev/null +++ b/index.html @@ -0,0 +1,13 @@ + + + + + + Rock Paper Scissors + + + + + + + \ No newline at end of file diff --git a/script.js b/script.js new file mode 100644 index 0000000..6cf29ef --- /dev/null +++ b/script.js @@ -0,0 +1,74 @@ +let options = ["ROCK", "PAPER", "SCISSORS"]; +let playerScore = 0; +let computerScore = 0; + + +function getComputerChoice(){ + let randomNum = Math.floor(Math.random() * 3); + return options[randomNum]; +} + +function playRound(playerSelection, computerSelection) { + + playerSelection = playerSelection.toUpperCase(); + + if ((playerSelection == "ROCK" && computerSelection == "ROCK") || (playerSelection == "PAPER" && computerSelection == "PAPER") || (playerSelection == "SCISSORS" && computerSelection == "SCISSORS")){ + return "You Draw!"; + } + + else if (playerSelection == "ROCK" && computerSelection == "SCISSORS"){ + playerScore++; + return "You Win! Rock beats Scissors"; + } + + else if (playerSelection == "ROCK" && computerSelection == "PAPER") { + computerScore++; + return "You Lose! Paper beats Rock" + } + + else if (playerSelection == "PAPER" && computerSelection == "SCISSORS"){ + computerScore++; + return "You Lose! Scissors beats Paper"; + } + + else if (playerSelection == "PAPER" && computerSelection == "ROCK") { + playerScore++; + return "You Win! Paper beats Rock" + } + + else if (playerSelection == "SCISSORS" && computerSelection == "ROCK"){ + computerScore++; + return "You Lose! Rock beats Scissors"; + } + + else if (playerSelection == "SCISSORS" && computerSelection == "PAPER") { + playerScore++; + return "You Win! Scissors beats Paper"; + } + +} + + +function playGame() { + + for(let i=0; i<5; i++){ + let playerSelection = prompt("Choose Rock, Paper or Scissors..."); + let computerSelection = getComputerChoice(); + console.log(playRound(playerSelection, computerSelection)); + console.log(playerSelection); + console.log(computerSelection); + console.log("Player Score:"+ playerScore); + console.log("Computer Score:"+ computerScore); + } + if (playerScore > computerScore){ + console.log("You Win :)"); + } + else if(playerScore == computerScore){ + console.log("It's a Draw:|"); + } + else { + console.log("You Lose :("); + } +} + +playGame();