Home > Net >  Rock Paper Scissors assigning points each round
Rock Paper Scissors assigning points each round

Time:08-05

newbie here! Just starting learning week and a half ago. Creating the rock paper scissors game. The goal is to play 5 rounds against the computer to determine the overall winner. I'm a little stumped when it comes to how I can award the player a point if they win or the computer a point if it wins. Also all critiques of code are welcome as well.

Thanks a ton!

code so far...

let array = ['rock', 'paper', 'scissor'];

const getComputerChoice = () => {
  let random = Math.floor((Math.random() * 3));
  let randomNames = array[random];
  return randomNames;
}

const game = () => {
 
  for (let i = 0; i < 5; i  ) {
    let input = prompt('Choose rock, paper or scissor');
    const playerSelection = input.toLowerCase();
    const computerChoice = getComputerChoice();
    
    const computerWins = `Computer Wins! ${computerChoice} beats ${playerSelection}.`;
    const playerWins = `Player wins! ${playerSelection} beats ${computerChoice}.`;
    const tie = `It's a tie, ${playerSelection} is equal to ${computerChoice}.`;

    if (playerSelection !== 'rock' && playerSelection !== 'paper' && playerSelection !== 'scissor') {
      i = i - 1;
      console.log("Nice try! Please enter a valid input.");
    }
    if (playerSelection === 'rock' && computerChoice === 'paper' || playerSelection === 'paper' && computerChoice === 'scissor' || playerSelection === 'scissor' && computerChoice === 'rock') {
        console.log(computerWins);
    } 
    if (playerSelection === 'paper' && computerChoice === 'rock' || playerSelection === 'scissor' && computerChoice === 'paper' || playerSelection === 'rock' && computerChoice === 'scissor') {
        console.log(playerWins);
    } else {
        if (playerSelection === computerChoice) {
          console.log(tie);
          }
      }
  }
}

game();

CodePudding user response:

Like Wiktor said in his comment, you can store the victories in variables, that you increment every time someone wins.

Before your for loop, you you'd initialize two variables, one for Computer wins, and one for Human wins:

var compWins = 0;
var humanWins = 0;

Then, when someone wins, you would increment the appropriate variable:

compWins ;

or

humanWins ;

CodePudding user response:

just add variable of score and increment after each win

let array = ['rock', 'paper', 'scissor'];
    let player_score=0;
    let computer_score=0;
    const getComputerChoice = () => {
      let random = Math.floor((Math.random() * 3));
      let randomNames = array[random];
      return randomNames;
    }
    
    const game = () => {
     
      for (let i = 0; i < 5; i  ) {
        let input = prompt('Choose rock, paper or scissor');
        const playerSelection = input.toLowerCase();
        const computerChoice = getComputerChoice();
        
        const computerWins = `Computer Wins! ${computerChoice} beats ${playerSelection}.`;
        const playerWins = `Player wins! ${playerSelection} beats ${computerChoice}.`;
        const tie = `It's a tie, ${playerSelection} is equal to ${computerChoice}.`;
    
        if (playerSelection !== 'rock' && playerSelection !== 'paper' && playerSelection !== 'scissor') {
          i = i - 1;
          console.log("Nice try! Please enter a valid input.");
        }
        if (playerSelection === 'rock' && computerChoice === 'paper' || playerSelection === 'paper' && computerChoice === 'scissor' || playerSelection === 'scissor' && computerChoice === 'rock') {
                computer_score =1;
            console.log(computerWins);
        } 
        if (playerSelection === 'paper' && computerChoice === 'rock' || playerSelection === 'scissor' && computerChoice === 'paper' || playerSelection === 'rock' && computerChoice === 'scissor') {
            player_score =1;
            console.log(playerWins);
        } else {
            if (playerSelection === computerChoice) {
              console.log(tie);
              }
          }
      }
      console.log("Computer:" computer_score ' vs ' 'Player:' player_score);
    }
    
    game();

CodePudding user response:

You can add 2 const like.

  let playerPoints = 0;
  let computerPoints = 0;

then, where you console.log the winner of a round you can add 1 at their counter.

    if (playerSelection === 'rock' && computerChoice === 'paper' || playerSelection === 'paper' && computerChoice === 'scissor' || playerSelection === 'scissor' && computerChoice === 'rock') {
    computerPoints  = 1
    console.log(computerWins);
} 
    if (playerSelection === 'paper' && computerChoice === 'rock' || playerSelection === 'scissor' && computerChoice === 'paper' || playerSelection === 'rock' && computerChoice === 'scissor') {
    playerPoints  = 1
    console.log(playerWins)

Then you have the points. With them you can add another if to control if someone has won (when reach 3 points for example).

  • Related