我正在为聊天机器人编写剪刀石头布游戏。我遇到的问题是switch (playerChoice)无法识别playerChoice变量中定义的字符串。

运行开关时,始终输出默认情况。似乎甚至没有在switch()中获取变量

这是我的代码,带注释:

var user = "user"
var message = "/rps rock" //this can be paper or scissors, based on the user's input.
//Here we find out what the player chose from the command '/rps [choice]'
var playerSaid = message.split(" ")
var playerChoice = playerSaid[1].toString()

//This determines the bot's choice of 1-3
var botChoice = (Math.floor(Math.random() * 3) + 1)

//This switch *should* find the user's choice
//The if statements inside each case should get the bot's choice and tell the verdict appropriately
switch (playerChoice) {
  case "rock":
    if (botChoice = 1) {
      var rpsVerdict = "It\'s a tie!"
    } else if (botChoice = 2) {
      var rpsVerdict = "Botticus wins!"
    } else if (botChoice = 3) {
      var rpsVerdict = user + " wins!"
    };
    break;
  case "paper":
    if (botChoice = 1) {
      var rpsVerdict = "It\'s a tie!"
    } else if (botChoice = 2) {
      var rpsVerdict = "Botticus wins!"
    } else if (botChoice = 3) {
      var rpsVerdict = user + " wins!"
    };
    break;
  case "scissors":
    if (botChoice = 1) {
      var rpsVerdict = "It\'s a tie!"
    } else if (botChoice = 2) {
      var rpsVerdict = "Botticus wins!"
    } else if (botChoice = 3) {
      var rpsVerdict = user + " wins!"
    };
    break;
  default:
    var rpsVerdict = "default"
    break
  }

//Here we output a simple sentence telling who won.
console.log('You chose ' + playerChoice + '. I chose ' + botChoice + '. ' + rpsVerdict)


(我意识到botChoice输出的是数字而不是字符串)

最佳答案

问题在于您的if语句中的比较:

if (botChoice = 1) {
// -----------^


=是赋值运算符。为了进行比较,您需要=====(在这种情况下无关紧要)。

分配作业后,您实际正在做的是:

botChoice = 1
if (1) {


...由于使用赋值运算符,它将值分配给目标(botChoice),操作的结果就是所分配的值。因此,无论if实际是什么1,您都始终输入第一个botChoice(因为Math.random是真实的)。



但是,您不需要switch,可以使用查找更简单,重复次数更少地确定获胜者。

// Set up data. `choices` is for using Math.random to get
// a choice for the bot. `winLookup` is for looking up the
// player's choice and seeing what it beats.
var choices = ["rock", "paper", "scissors"];
var winLookup = {
    rock: "scissors",
    paper: "rock",
    scissors: "paper"
};

// Bot chooses
var botChoice = choices[Math.floor(Math.random() * 3)];

// Outcome
var rpsVerdict;
if (playerChoice == botChoice) {
    rpsVerdict = "It's a tie!";
} else if (winLookup[playerChoice] == botChoice) {
    rpsVerdict = user + " wins!";
} else {
    rpsVerdict = "Botticus wins!";
}


现场示例:



document.getElementById("play").onclick = function() {
  var choices = ["rock", "paper", "scissors"];
  var winLookup = {
    rock: "scissors",
    paper: "rock",
    scissors: "paper"
  };
  var user = "user";
  var message = "/rps rock";
  var playerSaid = message.split(" ");
  var playerChoice = document.getElementById("playerChoice").value;
  //var playerChoice = playerSaid[1];
  //// No .toString() --------------^

  // botChoice => rock, paper, or scissors
  var botChoice = choices[Math.floor(Math.random() * 3)];

  // outcome
  var rpsVerdict;
  if (playerChoice == botChoice) {
    rpsVerdict = "It's a tie!";
  } else if (winLookup[playerChoice] == botChoice) {
    rpsVerdict = user + " wins!";
  } else {
    rpsVerdict = "Botticus wins!";
  }

  snippet.log(
    "You chose " + playerChoice + ". I chose " + botChoice +
    ". " + rpsVerdict
  );
};

<label>
  Your choice:
  <select size=3 id="playerChoice">
    <option>rock</option>
    <option>paper</option>
    <option>scissors</option>
  </select>
</label>
<input type="button" id="play" value="Play">
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

09-25 16:44