有谁知道为什么这可能导致无限循环。我只是看不出原因!
我觉得问题可能出在我的while循环播放到五下。
while循环应该在5时停止,当玩家或计算机到达时,游戏应该停止。
这是Rock,Paper,Scissors。每场比赛的结果是球员获胜,电脑赢或抽奖。
一旦玩家达到5胜,游戏就应该结束。
问题是游戏没有按预期结束!
function getInput() {
console.log("Please choose either 'rock', 'paper', or 'scissors'.");
return prompt("Please choose either 'rock', 'paper', or 'scissors'");
}
function getPlayerMove() {
return getInput();
}
function randomPlay() {
var randomNumber = Math.random();
if (randomNumber < 0.33) {
return "rock";
}
else if (randomNumber < 0.66) {
return "paper";
}
else {
return "scissors";
}
}
function getComputerMove() {
return randomPlay();
}
function getWinner(playerMove,computerMove) {
var winner;
if (playerMove === 'rock' && computerMove === 'scissors') {
winner = 'Player';
}
else if (playerMove === 'scissors' && computerMove === 'paper') {
winner='Player';
}
else if (playerMove === 'paper' && computerMove === 'rock') {
winner='Player';
}
else if (playerMove === 'paper' && computerMove === 'scissors') {
winner='Computer';
}
else if (playerMove === 'rock' && computerMove === 'paper') {
winner='Computer';
}
else if (playerMove === 'scissors' && computerMove === 'rock') {
winner = 'Computer';
}
else {
winner = "tie";
}
return winner;
}
// A big limitation of this game is the user is only allowed to choose once! To allow more choices you'd need to design the program differently
function playToFive() {
var playerWins = 0;
var computerWins = 0;
var playerMove = getPlayerMove();
while ((playerWins <= 5) || (computerWins <= 5)) {
var computerMove = getComputerMove();
var winner = getWinner(getPlayerMove, getComputerMove);
console.log('The player has chosen ' + playerMove + '. The computer has chosen ' + computerMove);
if (winner === "player") {
playerWins += 1;
}
else if (winner === "computer") {
computerWins += 1;
}
if ((playerWins = 5) || (computerWins = 5)) {
console.log("The game is over! " + "The " + winner + " has taken out the game!");
console.log("The final score was. " + playerWins + " to " + computerWins);
}
else {
console.log("The " + winner + ' takes the round. It is now ' + playerWins + ' to ' + computerWins);
}
}
}
playToFive ();
答案 0 :(得分:1)
问题在于这一行
var winner = getWinner(getPlayerMove, getComputerMove);
您正在将函数引用传递给getWinner()方法
var winner = getWinner(playerMove , computerMove );
这意味着您需要稍后再次移动,因此请将方法更改为(代码中修复的多个问题)
function playToFive()
{
var playerWins = 0;
var computerWins = 0;
while ((playerWins <= 5) && (computerWins <= 5)) //this line has && instead of ||
{
var computerMove = getComputerMove();
var playerMove = getPlayerMove(); //this line is now inside while loop
var winner = getWinner( playerMove , computerMove );
console.log('The player has chosen ' + playerMove + '. The computer has chosen ' + computerMove);
if (winner === "Player") { //P caps
playerWins += 1;
}
else if (winner === "Computer") { //C caps
computerWins += 1;
}
if ((playerWins == 5) || (computerWins == 5)) { //= is replaced by ==
console.log("The game is over! " + "The " + winner + " has taken out the game!");
console.log("The final score was. " + playerWins + " to " + computerWins);
}
else {
console.log("The " + winner + ' takes the round. It is now ' + playerWins + ' to ' + computerWins);
}
}
}
答案 1 :(得分:1)
为了这个,如果相应地运行:
if ((playerWins = 5) || (computerWins = 5)) {}
您需要使用==
运算符,因为只有一个=
用于值分配。
if ((playerWins == 5) || (computerWins == 5)) {}
答案 2 :(得分:0)
首先在while循环中你需要更改它&amp;&amp;否则,如果计算机为8且播放器为3,则循环将继续,直到两个玩家都> 5。
答案 3 :(得分:0)
我首先想一想你应该理解(==)和(===)here之间的区别。
if ((playerWins == 5) || (computerWins == 5)) { //use logical Operator not assignment Oprtr.
console.log("The game is over! " + "The " + winner + " has taken out the game!");
console.log("The final score was. " + playerWins + " to " + computerWins);
}
if ((playerWins = 5) || (computerWins = 5)) { // Error Code.
console.log("The game is over! " + "The " + winner + " has taken out the game!");
console.log("The final score was. " + playerWins + " to " + computerWins);
}
问题在于行
您正在将函数引用传递给getWinner()方法
使用此
var winner = getWinner(playerMove , computerMove );
而不是
var winner = getWinner(getPlayerMove, getComputerMove);
对于此功能......
function getWinner(playerMove,computerMove) {
var winner;
if (playerMove === 'rock' && computerMove === 'scissors') {
winner = 'Player';
}
else if (playerMove === 'scissors' && computerMove === 'paper') {
winner='Player';
}
else if (playerMove === 'paper' && computerMove === 'rock') {
winner='Player';
}
else if (playerMove === 'paper' && computerMove === 'scissors') {
winner='Computer';
}
else if (playerMove === 'rock' && computerMove === 'paper') {
winner='Computer';
}
else if (playerMove === 'scissors' && computerMove === 'rock') {
winner = 'Computer';
}
else {
winner = "tie";
}
return winner;
}
每次此方法都将
tie
设置为获胜者。
但你应该知道(==) equalto
和(===) equal value and equal type
here
答案 4 :(得分:-1)
谢谢你们。有很多问题。
干杯队员。绝对需要&amp;&amp;而不是||。 ||导致游戏继续进行
还需要var winner = getWinner(getPlayerMove,getComputerMove)。这导致无限循环。