Comment puis-je stocker la valeur playerScore et l'accumuler à chaque fois que la fonction playRound est appelée ?
P粉460377540
P粉460377540 2024-02-03 23:46:33
0
1
387

Pourquoi mon code s'ajoute-t-il à playerScore à chaque fois qu'il appelle la fonction playRound, au lieu de s'ajouter à ComputerScore ?

Mon brief de projet suggérait d'appeler la fonction playRound cinq fois dans la fonction de jeu car je n'ai pas cherché comment "boucler" le code pour répéter les appels de fonction.

Mon profil : https://www.theodinproject.com/lessons/foundations-rock-paper-scissors

J'ai essayé d'ajouter 1 à playerScore ou ComputerScore (ils sont déclarés comme variables globales avec la valeur 0) lors de l'appel de la fonction playRound.

J'ai essayé d'utiliser l'opérateur d'incrémentation ++ et J'ai essayé d'utiliser l'opérateur d'affectation d'addition += 1

Je pensais que le score du joueur gagnant serait augmenté de 1.

Ce qui s'est réellement passé : Chaque fois que la fonction playRound est appelée, playerScore est incrémenté de 1, ce qui n'est pas cohérent avec le gagnant.

//write a program to play 'rock, paper, scissors' game against the computer

//COMPUTER CHOICE- generate random choice of weapon
let choice = ['rock', 'paper', 'scissors'];
//select random array element from weapon array
function getComputerChoice() {
  computerChoice = choice[(Math.floor(Math.random() * choice.length))];
  return computerChoice;
}


//USER CHOICE- assign user choice from prompt input
function getPlayerChoice() {
  playerChoice = prompt('Choose your weapon', 'rock, paper or scissors?');
  return playerChoice;
}


//assign values to player variables 
const playerSelection = getPlayerChoice().toLowerCase();
const computerSelection = getComputerChoice();


//message to return to player
let youWin = `You win, ${playerSelection} beats ${computerSelection}`;
let youLose = `You lose, ${computerSelection} beats ${playerSelection}`;
let youDraw = `It's a draw!`;
//put message options into an array
let message = [youWin, youLose, youDraw];


//make global player score variables
let playerScore = 0;
let computerScore = 0;


//function to play one round
function playRound() {

  if (playerSelection == computerSelection) {
    return youDraw;
  } else if (playerSelection == 'rock' && computerSelection == 'paper') {
    computerScore = computerScore++;
    return message[1]; //you lose
  } else if (playerSelection == 'rock' && computerSelection == 'scissors') {
    playerScore++;
    return message[0]; //you win
  } else if (playerSelection == 'paper' && computerSelection == 'rock') {
    playerScore++;
    return message[0]; //you win
  } else if (playerSelection == 'paper' && computerSelection == 'scissors') {
    computerScore++;
    return message[1]; //you lose
  } else if (playerSelection == 'scissors' && computerSelection == 'rock') {
    computerScore++;
    return message[1]; //you lose
  } else if (playerSelection == 'scissors' && computerSelection == 'paper') {
    playerScore++;
    return message[0]; //you win
  } else {
    return ('oops! Type rock, paper or scissors!')
  }
}


//function to play five rounds and report player as winner or loser at the end
function game() {

  //check code: what values are assigned to player selections?
  console.log('player ', playerSelection);
  console.log('computer ', computerSelection);

  playRound();
  playRound();
  playRound();
  playRound();
  playRound();

  return playRound();
}

console.log(game());
console.log(computerScore);
console.log(playerScore);

P粉460377540
P粉460377540

répondre à tous(1)
P粉949267121

Quelques petits changements résoudront votre problème. Le principal changement est d'offrir le choix au joueur et à l'ordinateur à chaque tour au lieu d'une seule fois. Nous générerons également des messages youWin, youLose et d'autres en même temps.

//write a program to play 'rock, paper, scissors' game against the computer

//COMPUTER CHOICE- generate random choice of weapon
let choice = ['rock', 'paper', 'scissors'];
//select random array element from weapon array
function getComputerChoice() {
  computerChoice = choice[(Math.floor(Math.random() * choice.length))];
  return computerChoice;
}

//USER CHOICE- assign user choice from prompt input
function getPlayerChoice() {
  playerChoice = prompt('Choose your weapon', 'rock, paper or scissors?');
  return playerChoice;
}

//make global player score variables
let playerScore = 0;
let computerScore = 0;

//function to play one round
function playRound() {

  //assign values to player variables 
  const playerSelection = getPlayerChoice().toLowerCase();
  const computerSelection = getComputerChoice();

  //check code: what values are assigned to player selections?
  console.log('player:', playerSelection);
  console.log('computer:', computerSelection);
  
  //message to return to player
  let youWin = `You win, ${playerSelection} beats ${computerSelection}`;
  let youLose = `You lose, ${computerSelection} beats ${playerSelection}`;
  let youDraw = `It's a draw!`;

  if (playerSelection == computerSelection) {
return youDraw;
  } else if (playerSelection == 'rock' && computerSelection == 'paper') {
computerScore++;
return youLose;
  } else if (playerSelection == 'rock' && computerSelection == 'scissors') {
playerScore++;
return youWin;
  } else if (playerSelection == 'paper' && computerSelection == 'rock') {
playerScore++;
return youWin;
  } else if (playerSelection == 'paper' && computerSelection == 'scissors') {
computerScore++;
return youLose;
  } else if (playerSelection == 'scissors' && computerSelection == 'rock') {
computerScore++;
return youLose;
  } else if (playerSelection == 'scissors' && computerSelection == 'paper') {      playerScore++;
return youWin;
  } else {
return ('oops! Type rock, paper or scissors!')
  }
}

//function to play five rounds and report player as winner or loser at the end

function game() {
  for(let round = 0; round 
.as-console-wrapper { max-height: 100% !important; }
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal