Home > Mobile >  I'm making a rock-paper-scissors game with multiples rounds, but I'm having problems stori
I'm making a rock-paper-scissors game with multiples rounds, but I'm having problems stori

Time:11-22

` I'm making a rock-paper-scissors game with multiples rounds, but I'm having problems storing the number of player's victory. What am I doing wrong?

// THIS FUNCTION DECIDES IF THE COMPUTER CHOOSES ROCK, PAPER OR SCISSOS 
function getComputerChoice () {
    let rand = Math.floor(Math.random() * 10);
    if (rand <= 3) {
        return "Rock"
    } else if (rand <= 6) {
        return "Paper"
    } else {
        return "Scissors"
}}

// TESTING PLAYROUND FUNCTION 
function playRound(playerSelection, computerSelection) { 
     const loseMessage = "You lose! Paper beats Rock";
    const winMessage = "You win! Rock beats Scissors";
    const drawMessage = "Draw. You and the computer chose Rock"
        if (computerSelection === "Paper" && playerSelection === "Rock") {
        alert (loseMessage);
        return loseMessage 
    } else if (computerSelection === "Rock" && playerSelection === "Rock") {
        alert(drawMessage);
        return drawMessage 
    } else if (computerSelection === "Scissors" && playerSelection === "Rock") {
        alert(winMessage);
        return winMessage 
    } else {
        alert("Something went wrong")
    }
  }

let playerScore = 0;
 
function updatePlayerScore1() { 
    let playRoundResults = playRound();
    if (playRoundResults === "You win! Rock beats Scissors") {
      playerScore  = 1;
    }
    else {
      playerScore  = 0  
    }
   return playerScore;
 }

playRound(prompt("Rock, Paper or Scissors?", "Rock"), getComputerChoice());
alert (updatePlayerScore1());

`

I was expecting the updatePlayerScore1 function would store the number of player victories and alert it.

CodePudding user response:

I don't think you run your updatePlayerScore(). It should run outside your alert. Instead put the player score inside the alert(playerScore)

CodePudding user response:

When u declared function playRound(playerSelection, computerSelection) here that you were going to pass two arguments but in the

function updatePlayerScore1() { 
   let playRoundResults = playRound();
   ***
}

u use didn't pass either one. You can change your code to this and try

  function updatePlayerScore1() {
            let playRoundResults = playRound('Rock', getComputerChoice());
***
}

CodePudding user response:

I think u should use var instead of let

Using let will not update your score, it still changes in the updatePlayerScore1 function but not in the global scope

And you should run playRound function inside your updatePlayerScore1 function, like this

function updatePlayerScore1() { 
   let playRoundResults = playRound(prompt("Rock, Paper or Scissors?", "Rock"), getComputerChoice());
   if (playRoundResults === "You win! Rock beats Scissors") {
    playerScore  = 1;
   }
   else {
    playerScore  = 0  
   }
return playerScore;

}

CodePudding user response:

let playRoundResults = playRound(); This line is producing the error, the function playRound() requires two arguments, but you are not providing any.

Resolve this line you will get the answer. If you are not able to find solution ,compare your code with below.

function updatePlayerScore1(message) { 
    let playRoundResults = message;
    if (playRoundResults === "You win! Rock beats Scissors") {
      playerScore  = 1;
    }
    else {
      playerScore  = 0  ;
    }
   return playerScore;
 }

let message = playRound(prompt("Rock, Paper or Scissors?", "Rock"), getComputerChoice());
alert (updatePlayerScore1());

CodePudding user response:

There multiple combinations of draws, wins and losses. (For example, you can Win by picking Stone against scissors, but also by picking paper against rock). You'd have to consider all those possibilities in order for your code to work. (9 in total to be exact). I would suggest you create the message the player gets dynamically, especially nice to know how to do this if you go on to create games with even more possible outcomes.

The first function I wrote does exactly that ... it takes 2 picks as arguments. And bases on those it figures out if it is a draw, win or loss. And then inserts the picks in the appropriate message (way less work, especially if you add possible outcomes)

For your updatePlayerScore you only care if it was a win or not. So you split() the result message so that you only are left with either "you win" "you loose" or "draw". Then you can just check if it says "win" if it does add 1 to your score, if not just do nothing.

function results (yourPick, pcPick) {
  let winningCombinations = {rock: "scissors", paper: "rock", scissors: "paper"}
  if (yourPick === pcPick) return `Draw! bot of you picked ${yourPick}`
  if (pcPick === winningCombinations[yourPick]) return `You win! ${yourPick} beats ${pcPick}`
  return `You lose! ${yourPick} loses to ${pcPick}`
}

let playerScore = 0

function updatePlayerScore (result) {
  let basicResult = result.split("!")[0]
  if (basicResult === "You win") playerScore  
}

  • Related