英文:
Rock Paper Scissors in console repeating issue
问题
尝试在控制台中玩3次RPS游戏,并更新得分和结果。在游戏函数之外运行它对于第一局游戏来说效果很好,但在游戏函数中多次运行函数会导致结果无法正确更新。
尝试从playRound函数中返回结果,然后通过得分函数处理结果。认为结果函数每次在游戏函数中调用时都会更新。但实际上,它不会更新,无论选择的结果如何,我都会得到与第一轮游戏相同的结果。希望能得到有关如何使结果更新的输入。
let computerSelection = getComputerChoice();
let randomNum = Math.random();
function getComputerChoice() {
let randomNum = Math.random()
let computerSelection = '';
if (randomNum >= 0 && randomNum < 1 / 3) {
computerSelection = 'rock';
} else if (randomNum >= 1 / 3 && randomNum < 2 / 3) {
computerSelection = 'paper';
} else if (randomNum >= 2 / 3 && randomNum < 1) {
computerSelection = 'scissors';
}
console.log(computerSelection);
return computerSelection
}
// 提示玩家进行选择
let playerSelection = getPlayerChoice()
function getPlayerChoice() {
let playerSelection = prompt('Rock, Paper, or Scissors?').toLowerCase();
if (playerSelection === 'rock') {
console.log('rock');
} else if (playerSelection === 'paper') {
console.log('paper');
} else if (playerSelection === 'scissors') {
console.log('scissors');
}
return playerSelection
}
// 比较玩家选择与计算机选择并更新分数
let result = playRound(playerSelection, computerSelection)
function playRound(playerSelection, computerSelection) {
let result = '';
if (playerSelection === 'rock' && computerSelection === 'rock') {
result = 'Tie Game!';
} else if (playerSelection === 'rock' && computerSelection === 'paper') {
result = `Computer wins! ${computerSelection} beats ${playerSelection}`;
} else if (playerSelection === 'rock' && computerSelection === 'scissors') {
result = 'You win! Computer sucks!';
} else if (playerSelection === 'paper' && computerSelection === 'rock') {
result = 'You win! Computer sucks!';
} else if (playerSelection === 'paper' && computerSelection === 'paper') {
result = 'Tie Game!';
} else if (playerSelection === 'paper' && computerSelection === 'scissors') {
result = `Computer wins! ${computerSelection} beats ${playerSelection} `;
} else if (playerSelection === 'scissors' && computerSelection === 'rock') {
result = `Computer wins! ${computerSelection} beats ${playerSelection}`;
} else if (playerSelection === 'scissors' && computerSelection === 'paper') {
result = 'You win! Computer sucks!';
} else if (playerSelection === 'scissors' && computerSelection === 'scissors') {
result = 'Tie Game!';
};
console.log(result);
return result;
}
let score = {
wins: 0,
losses: 0,
};
function updateScore(result) {
if (result === 'You win! Computer sucks!') {
score.wins += 1;
} else if (result === 'Tie Game!') {
score = score;
} else if (result === `Computer wins! ${computerSelection} beats ${playerSelection}`) {
score.losses += 1;
};
console.log(score)
return score;
};
updateScore(result);
function game() {
getPlayerChoice();
getComputerChoice();
playRound(playerSelection, computerSelection);
updateScore(result);
getPlayerChoice();
getComputerChoice();
playRound(playerSelection, computerSelection);
updateScore(result);
};
game();
请注意,您的代码中存在一些问题,例如,您在游戏函数中多次调用getPlayerChoice()
和getComputerChoice()
,但这不会更改playerSelection
和computerSelection
的值。要解决这个问题,您可以在game()
函数中存储这些值并传递给playRound()
和updateScore()
函数。此外,您可能需要考虑如何在游戏中进行3轮比赛,以确保适当的追踪和更新分数。
英文:
Trying to play a game of RPS 3 times in the console with an updating score and result. Running it outside of the game function works fine for the first game, but running the functions multiple times in the game function leaves my result to not update properly.
Tried returning result from the playRound function than putting the result through a score function. Thought that the result function would update every time I call it in the game function.
Instead, it doesn't update and I get the same result as the first round played no matter the difference in outcomes from the choices made. Would love input on how I can get the result to update.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let computerSelection = getComputerChoice();
let randomNum = Math.random();
function getComputerChoice() {
let randomNum = Math.random()
let computerSelection = '';
if (randomNum >= 0 && randomNum < 1 / 3) {
computerSelection = 'rock';
} else if (randomNum >= 1 / 3 && randomNum < 2 / 3) {
computerSelection = 'paper';
} else if (randomNum >= 2 / 3 && randomNum < 1) {
computerSelection = 'scissors';
}
console.log(computerSelection);
return computerSelection
}
// prompt the player to make a selection
let playerSelection = getPlayerChoice()
function getPlayerChoice() {
let playerSelection = prompt('Rock, Paper, or Scissors?').toLowerCase();
if (playerSelection === 'rock') {
console.log('rock');
} else if (playerSelection === 'paper') {
console.log('paper');
} else if (playerSelection === 'scissors') {
console.log('scissors');
}
return playerSelection
}
// compare the player selection to the comp selection and update a score
let result = playRound(playerSelection, computerSelection)
function playRound(playerSelection, computerSelection) {
let result = '';
if (playerSelection === 'rock' && computerSelection === 'rock') {
result = 'Tie Game!';
} else if (playerSelection === 'rock' && computerSelection === 'paper') {
result = `Computer wins! ${computerSelection} beats ${playerSelection}`;
} else if (playerSelection === 'rock' && computerSelection === 'scissors') {
result = 'You win! Computer sucks!';
} else if (playerSelection === 'paper' && computerSelection === 'rock') {
result = 'You win! Computer sucks!';
} else if (playerSelection === 'paper' && computerSelection === 'paper') {
result = 'Tie Game!';
} else if (playerSelection === 'paper' && computerSelection === 'scissors') {
result = `Computer wins! ${computerSelection} beats ${playerSelection} `;
} else if (playerSelection === 'scissors' && computerSelection === 'rock') {
result = `Computer wins! ${computerSelection} beats ${playerSelection}`;
} else if (playerSelection === 'scissors' && computerSelection === 'paper') {
result = 'You win! Computer sucks!';
} else if (playerSelection === 'scissors' && computerSelection === 'scissors') {
result = 'Tie Game!';
};
console.log(result);
return result;
}
let score = {
wins: 0,
losses: 0,
};
function updateScore(result) {
if (result === 'You win! Computer sucks!') {
score.wins += 1;
} else if (result === 'Tie Game!') {
score = score;
} else if (result === `Computer wins! ${computerSelection} beats ${playerSelection}`) {
score.losses += 1;
};
console.log(score)
return score;
};
updateScore(result);
function game() {
getPlayerChoice();
getComputerChoice();
playRound(playerSelection, computerSelection);
updateScore(result);
getPlayerChoice();
getComputerChoice();
playRound(playerSelection, computerSelection);
updateScore(result);
};
game();
<!-- end snippet -->
答案1
得分: 0
以下是翻译好的部分:
问题出在你的游戏函数中:
问题在于你的 getComputerChoice
和 playRound
返回了一个值,但你从未赋值给它。
应该是:
function game() {
getPlayerChoice();
computerSelection = getComputerChoice();
result = playRound(playerSelection, computerSelection);
updateScore(result);
getPlayerChoice();
computerSelection = getComputerChoice();
result = playRound(playerSelection, computerSelection);
updateScore(result);
};
演示
let computerSelection = getComputerChoice();
let randomNum = Math.random();
function getComputerChoice() {
let randomNum = Math.random()
let computerSelection = '';
if (randomNum >= 0 && randomNum < 1 / 3) {
computerSelection = 'rock';
} else if (randomNum >= 1 / 3 && randomNum < 2 / 3) {
computerSelection = 'paper';
} else if (randomNum >= 2 / 3 && randomNum < 1) {
computerSelection = 'scissors';
}
console.log(computerSelection);
return computerSelection
}
let playerSelection = getPlayerChoice();
function getPlayerChoice() {
let playerSelection = prompt('Rock, Paper, or Scissors?').toLowerCase();
if (playerSelection === 'rock') {
console.log('rock');
} else if (playerSelection === 'paper') {
console.log('paper');
} else if (playerSelection === 'scissors') {
console.log('scissors');
}
return playerSelection
}
let result = playRound(playerSelection, computerSelection);
function playRound(playerSelection, computerSelection) {
let result = '';
if (playerSelection === 'rock' && computerSelection === 'rock') {
result = 'Tie Game!';
} else if (playerSelection === 'rock' && computerSelection === 'paper') {
result = `Computer wins! ${computerSelection} beats ${playerSelection}`;
} else if (playerSelection === 'rock' && computerSelection === 'scissors') {
result = 'You win! Computer sucks!';
} else if (playerSelection === 'paper' && computerSelection === 'rock') {
result = 'You win! Computer sucks!';
} else if (playerSelection === 'paper' && computerSelection === 'paper') {
result = 'Tie Game!';
} else if (playerSelection === 'paper' && computerSelection === 'scissors') {
result = `Computer wins! ${computerSelection} beats ${playerSelection} `;
} else if (playerSelection === 'scissors' && computerSelection === 'rock') {
result = `Computer wins! ${computerSelection} beats ${playerSelection}`;
} else if (playerSelection === 'scissors' && computerSelection === 'paper') {
result = 'You win! Computer sucks!';
} else if (playerSelection === 'scissors' && computerSelection === 'scissors') {
result = 'Tie Game!';
};
console.log(result);
return result;
}
let score = {
wins: 0,
losses: 0,
};
function updateScore(result) {
if (result === 'You win! Computer sucks!') {
score.wins += 1;
} else if (result === 'Tie Game!') {
score = score;
} else if (result === `Computer wins! ${computerSelection} beats ${playerSelection}`) {
score.losses += 1;
};
console.log(score)
return score;
};
updateScore(result);
function game() {
getPlayerChoice();
computerSelection = getComputerChoice();
result = playRound(playerSelection, computerSelection);
updateScore(result);
getPlayerChoice();
computerSelection = getComputerChoice();
result = playRound(playerSelection, computerSelection);
updateScore(result);
};
game();
请注意,我只翻译了代码部分,没有包含问题描述或其他内容。
英文:
The problem is in your game function:
The problem is that your getComputerChoice
and playRound
returns a value, but you never assign that value.
Should be:
function game() {
getPlayerChoice();
computerSelection = getComputerChoice();
result = playRound(playerSelection, computerSelection);
updateScore(result);
getPlayerChoice();
computerSelection = getComputerChoice();
result = playRound(playerSelection, computerSelection);
updateScore(result);
};
Demo
<!-- begin snippet: js hide: true console: true babel: false -->
<!-- language: lang-js -->
let computerSelection = getComputerChoice();
let randomNum = Math.random();
function getComputerChoice() {
let randomNum = Math.random()
let computerSelection = '';
if (randomNum >= 0 && randomNum < 1 / 3) {
computerSelection = 'rock';
} else if (randomNum >= 1 / 3 && randomNum < 2 / 3) {
computerSelection = 'paper';
} else if (randomNum >= 2 / 3 && randomNum < 1) {
computerSelection = 'scissors';
}
console.log(computerSelection);
return computerSelection
}
// prompt the player to make a selection
let playerSelection = getPlayerChoice()
function getPlayerChoice() {
let playerSelection = prompt('Rock, Paper, or Scissors?').toLowerCase();
if (playerSelection === 'rock') {
console.log('rock');
} else if (playerSelection === 'paper') {
console.log('paper');
} else if (playerSelection === 'scissors') {
console.log('scissors');
}
return playerSelection
}
// compare the player selection to the comp selection and update a score
let result = playRound(playerSelection, computerSelection)
function playRound(playerSelection, computerSelection) {
let result = '';
if (playerSelection === 'rock' && computerSelection === 'rock') {
result = 'Tie Game!';
} else if (playerSelection === 'rock' && computerSelection === 'paper') {
result = `Computer wins! ${computerSelection} beats ${playerSelection}`;
} else if (playerSelection === 'rock' && computerSelection === 'scissors') {
result = 'You win! Computer sucks!';
} else if (playerSelection === 'paper' && computerSelection === 'rock') {
result = 'You win! Computer sucks!';
} else if (playerSelection === 'paper' && computerSelection === 'paper') {
result = 'Tie Game!';
} else if (playerSelection === 'paper' && computerSelection === 'scissors') {
result = `Computer wins! ${computerSelection} beats ${playerSelection} `;
} else if (playerSelection === 'scissors' && computerSelection === 'rock') {
result = `Computer wins! ${computerSelection} beats ${playerSelection}`;
} else if (playerSelection === 'scissors' && computerSelection === 'paper') {
result = 'You win! Computer sucks!';
} else if (playerSelection === 'scissors' && computerSelection === 'scissors') {
result = 'Tie Game!';
};
console.log(result);
return result;
}
let score = {
wins: 0,
losses: 0,
};
function updateScore(result) {
if (result === 'You win! Computer sucks!') {
score.wins += 1;
} else if (result === 'Tie Game!') {
score = score;
} else if (result === `Computer wins! ${computerSelection} beats ${playerSelection}`) {
score.losses += 1;
};
console.log(score)
return score;
};
updateScore(result);
function game() {
getPlayerChoice();
computerSelection = getComputerChoice();
result = playRound(playerSelection, computerSelection);
updateScore(result);
getPlayerChoice();
computerSelection = getComputerChoice();
result = playRound(playerSelection, computerSelection);
updateScore(result);
};
game();
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论