我正在循环播放这款剪刀石头布游戏5次,如果用户获胜,则获得1;如果计算机获胜,则获得1,依此类推。如何更新用户和计算机分数?如果最终成为平局,我该怎么做,所以没人能说清楚吗?
// Computer makes a choice
function computerPlay() {
let compchoice = ['Rock', 'Paper', 'Scissors'];
return compchoice[Math.floor(Math.random() * compchoice.length)];
}
//Plays a single round Player vs Computer
function playRound(playerSelection, computerSelection) {
//Player
if (playerSelection === 'Rock' && computerSelection === 'Scissors')
{
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (playerSelection === 'Paper' && computerSelection ===
'Rock') {
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (playerSelection === 'Scissors' && computerSelection ===
'Paper') {
return 'You chose ' + playerSelection + ',' + ' You win!';
}
// Computer
else if (computerSelection === 'Rock' && playerSelection ===
'Scissors') {
return 'Computer chose ' + computerSelection + ',' + 'Computer
wins!';
} else if (computerSelection === 'Paper' && playerSelection ===
'Rock') {
return 'Computer chose ' + computerSelection + ',' + 'Computer
wins!';
} else if (computerSelection === 'Scissors' && playerSelection ===
'Paper') {
return 'Computer chose ' + computerSelection + ',' + ' Computer
wins!';
} else if (computerSelection === playerSelection) {
return 'Its a draw!';
} else {
return 'Please chose Rock, Paper, or Scissors';
}
}
//loops game 5 times to decide a winner.
function game() {
for(var i=0;i<5;i++){
let playerSelection = prompt("Rock, Paper, Scissors");
const computerSelection = computerPlay()
console.log(playRound(playerSelection, computerSelection))
console.log("your score = " + userScore);
console.log("Computer's score = " + computerScore);
}
}
let userScore =0;
let computerScore =0;
console.log(game());
答案 0 :(得分:1)
我认为这是您要实现的目标。您只需要在循环(即game
)迭代时跟踪得分。我修改了playRound
以返回一个数组-第一个元素表示玩家是否在一轮中击败了计算机,第二个元素表示您最初在功能中console.log
正在使用的消息:
// Computer makes a choice
function computerPlay() {
let compchoice = ['Rock', 'Paper', 'Scissors'];
return compchoice[Math.floor(Math.random() * compchoice.length)];
}
//Plays a single round Player vs Computer
function playRound(playerSelection, computerSelection) {
let playerWinsRound = false;
let text;
//Player
if (playerSelection === 'Rock' && computerSelection === 'Scissors') {
playerWinsRound = true;
text = 'You chose ' + playerSelection + ',' + ' You win!';
} else if (playerSelection === 'Paper' && computerSelection ===
'Rock') {
playerWinsRound = true;
text = 'You chose ' + playerSelection + ',' + ' You win!';
} else if (playerSelection === 'Scissors' && computerSelection ===
'Paper') {
playerWinsRound = true;
text = 'You chose ' + playerSelection + ',' + ' You win!';
}
// Computer
else if (computerSelection === 'Rock' && playerSelection ===
'Scissors') {
text = 'Computer chose ' + computerSelection + ', Computer wins!';
} else if (computerSelection === 'Paper' && playerSelection ===
'Rock') {
text = 'Computer chose ' + computerSelection + ', Computer wins!';
} else if (computerSelection === 'Scissors' && playerSelection ===
'Paper') {
text = 'Computer chose ' + computerSelection + ', Computer wins!';
} else if (computerSelection === playerSelection) {
playerWinsRound = null;
text = 'Its a draw!';
} else {
text = 'Please chose Rock, Paper, or Scissors';
}
return [playerWinsRound, text];
}
//loops game 5 times to decide a winner.
function game() {
//Score is part of the game - so move the score vars inside the game function
let userScore = 0;
let computerScore = 0;
//Update the scores on each iteration of the loop (i.e.: each round)
for (var i = 0; i < 5; i++) {
const playerSelection = prompt("Rock, Paper, Scissors");
const computerSelection = computerPlay();
const [playerWinsRound, text] = playRound(playerSelection, computerSelection)
if (playerWinsRound) {
userScore += 1;
} else {
if (playerWinsRound === false) {
computerScore += 1;
}
}
console.log(text);
console.log(`Your score = ${userScore}`);
console.log(`Computer score = ${computerScore}`);
}
}
game();