如何在相同的选择后停止循环

How to stop the loop after same choices?

本文关键字:循环 选择      更新时间:2023-09-26

我的代码看起来像这样,但我有一个问题。只要选择相同,游戏就会重新开始。在完成一个游戏后,我得到了一个"未定义"的警报,因为startGame()函数使用第一轮的参数再次运行(其中的选择是相同的)。

我是JS这个词的新手,希望尽可能简单。有人能帮我提供一个如何停止"未定义"的解决方案吗?

// the game starts here
function startGame() {
  // the user
  var userChoice = prompt("Do you choose rock, paper or scissors?");
  // if invalid input
  while ((userChoice != "rock") && (userChoice != "paper") && (userChoice != "scissors")) {
    userChoice = prompt("Please select again, this time correctly!");
  }
  alert("You chose " + userChoice);
  // computer
  var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if (computerChoice <= 0.67) {
    computerChoice = "paper";
  } else {
    computerChoice = "scissors";
  }
  alert("Computer pick: " + computerChoice);
  // comparison between user and computer
  var compare = function(choice1, choice2) {
    // if both selections are the same
    if (choice1 == choice2) {
      alert("Tie, restart the game!");
      startGame();
    }
    // if the user selects rock
    else if (choice1 == "rock") {
      if (choice2 == "scissors") {
        return "You have won the game!";
      } else {
        return "LOOOOOSSEEER!";
      }
    }
    // if the user selects scissors
    else if (choice1 == "scissors") {
      if (choice2 == "rock") {
        return "You have won the game!";
      }
    } else {
      return "LOOOOOSSEEER";
    }
  };
  alert(compare(userChoice, computerChoice));
}
// startGame();

您在这里使用递归。startGame()函数再次启动,compare()的第一个调用需要一个return语句。但是在这个if子句中,您根本没有返回语句。

// if both selections are the same
if (choice1 == choice2) {
  alert("Tie, restart the game!");
  startGame();
}

您可以通过if语句中的alert而不是返回结果来解决此问题。这里有一个jsfiddle(函数调用注释):

http://jsfiddle.net/v5poL9cm/

编辑:

另一种方法是在所有情况下都返回消息,并让startGame()函数也有一个return语句。jsfiddle在这里(函数调用注释):

http://jsfiddle.net/65sg7mcL/

$(function () {
    startGame();
});
// the game starts here
function startGame() {
    // the user
    var userChoice = prompt("Do you choose rock, paper or scissors?");
    // if invalid input
    while ((userChoice != "rock") && (userChoice != "paper") && (userChoice != "scissors")) {
        userChoice = prompt("Please select again, this time correctly!");
    }
    alert("You chose " + userChoice);
    // computer
    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
        computerChoice = "rock";
    } else if (computerChoice <= 0.67) {
        computerChoice = "paper";
    } else {
        computerChoice = "scissors";
    }
    alert("Computer pick: " + computerChoice);
    // comparison between user and computer
    var result = compare(userChoice, computerChoice);
    if (result == "1") {
        alert("Tie, restart the game!");
        startGame();
    } else if (result == "2") {
        alert("You have won the game!");
    } else {
        alert("LOOOOOSSEEER");
    }
}
// return 1 -- > Tie
// return 2 -- > You have won
// return 3 -- > LOOOOOSSEEER
function compare(choice1, choice2) {
    // if both selections are the same
    if (choice1 == choice2) {
        return "1";
    }
    // if the user selects rock
    else if (choice1 == "rock") {
        if (choice2 == "scissors") {
            return "2";
        } else {
            return "3";
        }
    }
    // if the user selects scissors
    else if (choice1 == "scissors") {
        if (choice2 == "rock") {
            return "2";
        }
    } else {
        return "3";
    }
};

我同意@Escobear的观点!问题是,你没有从平局部分返回任何东西,而是重新开始比赛。在某个时刻,它会回到你的脑海中,并试图提醒一个未定义的人。

我想我会这样做:

// comparison between user and computer NOTE: This method is global, but it seemed pointless to redefine it for every game
function compare(choice1, choice2) {
  // if both selections are the same
  if (choice1 == choice2) {
    return -1;
  }
  // if the user selects rock
  else if (choice1 == "rock") {
    if (choice2 == "scissors") {
      return 1;
    }
    return 0;
  }
  // if the user selects scissors
  else if (choice1 == "scissors") {
    if (choice2 == "rock") {
      return 0;
    }
    return 1;
  }
  else if (choice1 == "paper") {
    if (choice2 == "rock") {
      return 1;
    }
    return 0
  }
};
// the game starts here
function startGame() {
  // the user
  var userChoice = prompt("Do you choose rock, paper or scissors?");
  // if invalid input
  while ((userChoice != "rock") && (userChoice != "paper") && (userChoice != "scissors")) {
    userChoice = prompt("Please select again, this time correctly!");
  }
  alert("You chose " + userChoice);
  // computer
  var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if (computerChoice <= 0.67) {
    computerChoice = "paper";
  } else {
    computerChoice = "scissors";
  }
  alert("Computer pick: " + computerChoice);
  var result = compare(userChoice, computerChoice);
  if (result === -1) {
    alert('Tie, restart the game!');
    startGame();
  } else {
    alert(result ? 'You have won the game!' : 'LOOOOOSSSEEER!');
  }
}
startGame();

(我还纠正了获胜的一些逻辑,并添加了用户输入=纸张)