Rock Paper Scissors在控制台中重复的问题

huangapple go评论78阅读模式
英文:

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(),但这不会更改playerSelectioncomputerSelection的值。要解决这个问题,您可以在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 = &#39;&#39;;
if (randomNum &gt;= 0 &amp;&amp; randomNum &lt; 1 / 3) {
computerSelection = &#39;rock&#39;;
} else if (randomNum &gt;= 1 / 3 &amp;&amp; randomNum &lt; 2 / 3) {
computerSelection = &#39;paper&#39;;
} else if (randomNum &gt;= 2 / 3 &amp;&amp; randomNum &lt; 1) {
computerSelection = &#39;scissors&#39;;
}
console.log(computerSelection);
return computerSelection
}
// prompt the player to make a selection
let playerSelection = getPlayerChoice()
function getPlayerChoice() {
let playerSelection = prompt(&#39;Rock, Paper, or Scissors?&#39;).toLowerCase();
if (playerSelection === &#39;rock&#39;) {
console.log(&#39;rock&#39;);
} else if (playerSelection === &#39;paper&#39;) {
console.log(&#39;paper&#39;);
} else if (playerSelection === &#39;scissors&#39;) {
console.log(&#39;scissors&#39;);
}
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 = &#39;&#39;;
if (playerSelection === &#39;rock&#39; &amp;&amp; computerSelection === &#39;rock&#39;) {
result = &#39;Tie Game!&#39;;
} else if (playerSelection === &#39;rock&#39; &amp;&amp; computerSelection === &#39;paper&#39;) {
result = `Computer wins! ${computerSelection} beats ${playerSelection}`;
} else if (playerSelection === &#39;rock&#39; &amp;&amp; computerSelection === &#39;scissors&#39;) {
result = &#39;You win! Computer sucks!&#39;;
} else if (playerSelection === &#39;paper&#39; &amp;&amp; computerSelection === &#39;rock&#39;) {
result = &#39;You win! Computer sucks!&#39;;
} else if (playerSelection === &#39;paper&#39; &amp;&amp; computerSelection === &#39;paper&#39;) {
result = &#39;Tie Game!&#39;;
} else if (playerSelection === &#39;paper&#39; &amp;&amp; computerSelection === &#39;scissors&#39;) {
result = `Computer wins! ${computerSelection} beats ${playerSelection} `;
} else if (playerSelection === &#39;scissors&#39; &amp;&amp; computerSelection === &#39;rock&#39;) {
result = `Computer wins! ${computerSelection} beats ${playerSelection}`;
} else if (playerSelection === &#39;scissors&#39; &amp;&amp; computerSelection === &#39;paper&#39;) {
result = &#39;You win! Computer sucks!&#39;;
} else if (playerSelection === &#39;scissors&#39; &amp;&amp; computerSelection === &#39;scissors&#39;) {
result = &#39;Tie Game!&#39;;
};
console.log(result);
return result;
}
let score = {
wins: 0,
losses: 0,
};
function updateScore(result) {
if (result === &#39;You win! Computer sucks!&#39;) {
score.wins += 1;
} else if (result === &#39;Tie Game!&#39;) {
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

以下是翻译好的部分:

问题出在你的游戏函数中:

问题在于你的 getComputerChoiceplayRound 返回了一个值,但你从未赋值给它。

应该是:

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 = &#39;&#39;;
if (randomNum &gt;= 0 &amp;&amp; randomNum &lt; 1 / 3) {
computerSelection = &#39;rock&#39;;
} else if (randomNum &gt;= 1 / 3 &amp;&amp; randomNum &lt; 2 / 3) {
computerSelection = &#39;paper&#39;;
} else if (randomNum &gt;= 2 / 3 &amp;&amp; randomNum &lt; 1) {
computerSelection = &#39;scissors&#39;;
}
console.log(computerSelection);
return computerSelection
}
// prompt the player to make a selection
let playerSelection = getPlayerChoice()
function getPlayerChoice() {
let playerSelection = prompt(&#39;Rock, Paper, or Scissors?&#39;).toLowerCase();
if (playerSelection === &#39;rock&#39;) {
console.log(&#39;rock&#39;);
} else if (playerSelection === &#39;paper&#39;) {
console.log(&#39;paper&#39;);
} else if (playerSelection === &#39;scissors&#39;) {
console.log(&#39;scissors&#39;);
}
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 = &#39;&#39;;
if (playerSelection === &#39;rock&#39; &amp;&amp; computerSelection === &#39;rock&#39;) {
result = &#39;Tie Game!&#39;;
} else if (playerSelection === &#39;rock&#39; &amp;&amp; computerSelection === &#39;paper&#39;) {
result = `Computer wins! ${computerSelection} beats ${playerSelection}`;
} else if (playerSelection === &#39;rock&#39; &amp;&amp; computerSelection === &#39;scissors&#39;) {
result = &#39;You win! Computer sucks!&#39;;
} else if (playerSelection === &#39;paper&#39; &amp;&amp; computerSelection === &#39;rock&#39;) {
result = &#39;You win! Computer sucks!&#39;;
} else if (playerSelection === &#39;paper&#39; &amp;&amp; computerSelection === &#39;paper&#39;) {
result = &#39;Tie Game!&#39;;
} else if (playerSelection === &#39;paper&#39; &amp;&amp; computerSelection === &#39;scissors&#39;) {
result = `Computer wins! ${computerSelection} beats ${playerSelection} `;
} else if (playerSelection === &#39;scissors&#39; &amp;&amp; computerSelection === &#39;rock&#39;) {
result = `Computer wins! ${computerSelection} beats ${playerSelection}`;
} else if (playerSelection === &#39;scissors&#39; &amp;&amp; computerSelection === &#39;paper&#39;) {
result = &#39;You win! Computer sucks!&#39;;
} else if (playerSelection === &#39;scissors&#39; &amp;&amp; computerSelection === &#39;scissors&#39;) {
result = &#39;Tie Game!&#39;;
};
console.log(result);
return result;
}
let score = {
wins: 0,
losses: 0,
};
function updateScore(result) {
if (result === &#39;You win! Computer sucks!&#39;) {
score.wins += 1;
} else if (result === &#39;Tie Game!&#39;) {
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 -->

huangapple
  • 本文由 发表于 2023年8月10日 16:06:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定