我循环这个石头剪刀游戏 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());
我认为这就是您要实现的目标。您只需要在循环(即: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();