如何在每次呼叫playRound函數時儲存playerScore值並累積它?
P粉460377540
P粉460377540 2024-02-03 23:46:33
0
1
335

為什麼我的程式碼每次呼叫 playRound 函數都會加入到playerScore,而不是加入到computerScore?

我的專案簡介建議在遊戲函數中呼叫 playRound 函數五次,因為我還沒有研究如何「循環」程式碼來重複函數呼叫。

我的簡介:https://www.theodinproject.com/lessons/foundations-rock-paper-scissors

我嘗試在呼叫 playRound 函數時將 1 加到playerScore 或computerScore(它們被宣告為值為0 的全域變數)。

我嘗試使用增量運算子 和 我嘗試過使用加法賦值運算子 = 1

我以為獲勝玩家的分數會增加 1。

實際發生了什麼: 每次呼叫 playRound 函數時,playerScore 都會增加 1,這與贏家的結果不一致。

//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

全部回覆(1)
P粉949267121

一些小的更改將解決您的問題。主要的變化是在每一輪中獲得玩家選擇和電腦選擇,而不是只一次。我們也會同時產生 youWin、youLose 等訊息。

//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; }
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!