奇怪的JS可变错误

时间:2019-03-18 14:44:18

标签: javascript jquery

我一直在JSfiddle上进行简单的石头,剪刀,剪刀游戏,但是遇到了一个奇怪的问题。不管用户输入是什么,以及游戏的实际结果是什么,两个损失都将添加到相应的计数器上。我找不到问题的根源,任何帮助将不胜感激。 Counter1跟踪平局,counter2跟踪获胜,counter3(破局)跟踪损失,counter4跟踪比赛。

 $('#begin').click(function() {
  playRPS();
  return false;
});

let counter1 = 0;
let counter2 = 0;
let counter3 = 0;
let counter4 = 0;

var playRPS = function() {
  var userChoice = prompt("Do you choose rock, paper or scissors?");
  var compChoice = computerChoice();
  var result = compare(userChoice, compChoice);
  track(userChoice, compChoice);
  $('body').append('<p>You chose ' + userChoice + '</p>')
    .append('<p>The computer chose ' + compChoice + '</p>')
    .append('<p>' + result + '</p>');
    document.getElementById("wins").innerHTML = counter2
    document.getElementById("losses").innerHTML = counter3
    document.getElementById("ties").innerHTML = counter1
    document.getElementById("gamesPlayed").innerHTML = counter4
};

var computerChoice = function() {
  var randomNum = Math.random();
  if (randomNum < 0.0001) {
    randomNum = "bomb";
  } else if (randomNum < 0.34) {
    randomNum = "rock";
  } else if (randomNum <= 0.67) {
    randomNum = "paper";
  } else if (randomNum < 0.99) {
    randomNum = "scissors";
  } else {
    randomNum = "gun";
  }
  return randomNum;
}

var compare = function(choice1, choice2) {
  if (choice1 === choice2) {
    return "The result is a tie!";
  }
  if (choice1 === "rock") {
    if (choice2 === "scissors") {
      return "You win";
    } else {
      return "The Computer wins";
    }
  }
  if (choice1 === "paper") {
    if (choice2 === "rock") {
      return "You win";
    } else {
      return "The Computer wins";
    }
  }
  if (choice1 === "scissors") {
    if (choice2 === "paper") {
      return "You win";
    } else {
      return "The Computer wins";
    }
  }
  if (choice1 === "bomb") {
    return "You win";
  }
  if (choice2 === "bomb") {
    return "The Computer wins"
  }
  if (choice1 === "gun") {
    return "You win"
  }
  if (choice2 === "gun") {
    return "The Computer wins"
  }
};

var track = function(uChoice, cChoice) {
  if (uChoice === cChoice) {
    counter1 += 1;
  }
  if (uChoice === "rock") {
    if (cChoice === "scissors")
      counter2 += 1;
  } else {
    counter3 += 1;
  }
  if (uChoice === "paper") {
    if (cChoice === "rock")
      counter2 += 1;
  } else {
    counter3 += 1;
  }
  if (uChoice === "scissors") {
    if (cChoice === "paper")
      counter2 += 1;
  } else {
    counter3 += 1;
  }
  if (uChoice === "bomb") {
    counter2 += 1;
  }
  if (uChoice === "gun") {
    counter2 += 1;
  }
  if (cChoice === "bomb") {
    counter3 += 1;
  }
  if (cChoice === "gun") {
    counter3 += 1;
  }
  counter4 += 1;
};

1 个答案:

答案 0 :(得分:0)

您可以采用两种方法。

  • 修复track函数以使用正确的if-else-if语句(为此我进行了快速更新here)。考虑更改对计数器无意义的变量名。

  • 请考虑更改compare方法以返回整数而不是字符串(0是平局,1是玩家赢,-1是比赛赢)。这样,您就可以在以后构建字符串,并重复使用比较结果来更新计数器。您当前正在执行两次比较逻辑(在comparetrack中)

接下来,您应该使用调试器或日志语句来跟踪问题的出处,您会很快发现此问题,因为它很容易重现;)