My Development Notes

Programming Made Easier


Rock-Paper-Scissors Game Logic in JavaScript

This is the total breakdown of the Rock-Paper-Scissors Game Logic in JavaScript. Here’s the full code first. Afterwards I’ll break down as to how it all works behind the scenes.

//--------------------First Part-------------------------
// randomly generates Rock, Paper or Scissors

function getComputerChoice() {
  let choice = ["Rock", "Paper", "Scissors"];
  let randomRPS = choice[Math.floor(Math.random() * choice.length)];
  return randomRPS;
}



//-------------Second Part--------------------------------

// play a single round of the game (this is the game logic)

function playRound(playerSelection, computerSelection) {
  if (playerSelection == "Rock" && computerSelection == "Paper") {
    return "You lose! Paper beats Rock";
  } else if (playerSelection == "Paper" && computerSelection == "Rock") {
    return "You win! Paper beats Rock";
  } else if (playerSelection == "Rock" && computerSelection == "Scissors") {
    return "You win! Rock beats Scissors";
  } else if (playerSelection == "Scissors" && computerSelection == "Rock") {
    return "You lose! Rock beats Scissors";
  } else if (playerSelection == "Paper" && computerSelection == "Scissors") {
    return "You lose! Scissors beats Paper";
  } else if (playerSelection == "Scissors" && computerSelection == "Paper") {
    return "You win! Scissors beats Paper";
  } else if (playerSelection == computerSelection) {
    return "Draw";
  }
}

//-----------Final Part-------------------------------------


// play the game 5 times for a round
function game() {
  for (let i = 0; i < 5; i++) {
    let playerSelection = prompt(
      "Enter Rock, Paper or Scissors - CaseSensetive"
    );
    let computerSelection = getComputerChoice();
    console.log(playRound(playerSelection, computerSelection));
  }
}
game();

Here, the code runs from top to bottom and executes accordingly.

Part 1

Let’s now break down the first part of the game.

function getComputerChoice() {
  let choice = ["Rock", "Paper", "Scissors"];
  let randomRPS = choice[Math.floor(Math.random() * choice.length)];
  return randomRPS;
}

The first part is simple enough. We’ve written a function called getComputerChoice(), inside of which we have an array called choice which takes in Rock, Paper and Scissors as the given options.

At this point we use the Math.random method to randomly generate the indexes of the array choice each time we call the function to randomly generate one of the three index values of the array. Here, .length generates the string length which is 3 and Math.floor rounds the decimal places down to the nearest integer. So, here if we console log the function name, it randomly generates either Rock, Paper or Scissors.

Part 2

function playRound(playerSelection, computerSelection) {
  if (playerSelection == "Rock" && computerSelection == "Paper") {
    return "You lose! Paper beats Rock";
  } else if (playerSelection == "Paper" && computerSelection == "Rock") {
    return "You win! Paper beats Rock";
  } else if (playerSelection == "Rock" && computerSelection == "Scissors") {
    return "You win! Rock beats Scissors";
  } else if (playerSelection == "Scissors" && computerSelection == "Rock") {
    return "You lose! Rock beats Scissors";
  } else if (playerSelection == "Paper" && computerSelection == "Scissors") {
    return "You lose! Scissors beats Paper";
  } else if (playerSelection == "Scissors" && computerSelection == "Paper") {
    return "You win! Scissors beats Paper";
  } else if (playerSelection == computerSelection) {
    return "Draw";
  }
}

So, the above code is the second part. This is the main game logic where we play a single round of the game. Here, we’ve written a function playRound() with two parameters – playerSelection & computerSelection. Inside the function we’ve used the if-else statement to write the whole game logic, like, when it’s a draw, when you win and when the computer wins.

Part 3

function game() {
  for (let i = 0; i < 5; i++) {
    let playerSelection = prompt("Enter Rock, Paper or Scissors - CaseSensetive");
    let computerSelection = getComputerChoice();
    console.log(playRound(playerSelection, computerSelection));
  }
}
game();

Now, above is the third and final part. Here, we have to logically make the game work. So, we create a function called game() and then create a for-loop to play the game five times in a set. Here, the playerSelection has to be entered manually (which is also Case-Sensitive to keep things simple), so we use the prompt() method. Also, computerSelection is called automatically by linking the getComputerChoice() function from the first part in the next line.

Then, we have to call the function at the very end and also console log playRound() with the parameters inside the for-loop to run the game.

Note that we need to call the game() function at the very end here in order for the whole logic to execute. Also, this code executes inside the console and since we’ve looped the code five times through, you have to refresh the browser window after five inputs to play another round.


Posted

in

by

Tags:

Leave a Reply