Bagaimanakah saya boleh menyimpan nilai playerScore dan mengumpulnya setiap kali fungsi playRound dipanggil?
P粉460377540
P粉460377540 2024-02-03 23:46:33
0
1
352

Mengapa kod saya menambah pada playerScore setiap kali ia memanggil fungsi playRound, bukannya menambah pada computerScore?

Ringkas projek saya mencadangkan untuk memanggil fungsi playRound lima kali dalam fungsi permainan kerana saya tidak melihat cara untuk "menggulung" kod untuk mengulangi panggilan fungsi.

Profil saya: https://www.theodinproject.com/lessons/foundations-rock-paper-scissors

Saya cuba menambah 1 pada playerScore atau computerScore (ia diisytiharkan sebagai pembolehubah global dengan nilai 0) apabila memanggil fungsi playRound.

Saya cuba menggunakan increment operator ++ dan Saya telah mencuba menggunakan operator penugasan tambahan += 1

Saya fikir markah pemain yang menang akan dinaikkan sebanyak 1.

Apa yang sebenarnya berlaku: Setiap kali fungsi playRound dipanggil, playerScore ditambah dengan 1, yang tidak konsisten dengan pemenang.

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

membalas semua(1)
P粉949267121

Beberapa perubahan kecil akan menyelesaikan masalah anda. Perubahan utama ialah mendapatkan pilihan pemain dan pilihan komputer setiap pusingan dan bukannya sekali sahaja. Kami juga akan menjana andaWin, youLose dan mesej lain pada masa yang sama.

//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; }
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!