首页 > 解决方案 > 为什么这总是平局?

问题描述

我正在使用 javascript 构建一个记分的石头剪刀布游戏。为什么这总是平局?您还看到我的代码保持分数有什么问题吗?将分数变量保存到localStorage会很容易吗?我还在底部添加了我的 HTML。

var userChoice = document.getElementById("selection").value;
var totalScore = document.getElementById("score");
var yourScore = 0
var compScore = 0
var computerChoice = Math.random();

if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if (computerChoice <= 0.67) {
    computerChoice = "paper"; 
} else {
    computerChoice = "scissors";
}

var compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        alert( "It's a tie.");
    }
    if (choice1 === "scissors") {
        if (choice2 === "rock") {
            alert("You lose")
            compScore += 1
            totalScore.innerHTML = "The score is " + yourScore + " - " + compScore
        }
      if (choice2 === "paper") {
          alert("you win")
          yourScore += 1
          totalScore.innerHTML = "The score is " + yourScore + " - " + compScore

     }

 }
    if (choice1 === "paper") {
        if (choice2 === "rock") {
            alert("You Win.")
            yourScore += 1
            totalScore.innerHTML = "The score is " + yourScore + " - " + compScore
        }
        if (choice2 === "scissors") {
            alert("You lose.")
            compScore += 1
            totalScore.innerHTML = "The score is " + yourScore + " - " + compScore

        }
  }
    if (choice1 ==="rock") {
        if (choice2 === "scissors") {
            alert("You win.")
            yourScore += 1 
            totalScore.innerHTML = "The score is " + yourScore + " - " + compScore
        }
        if (choice2 === "paper") {
            alert("You lose.")
            compScore += 1
            totalScore.innerHTML = "The score is " + yourScore + " - " + compScore

        }
    }
  }
  compare(userChoice,computerChoice);
<!DOCTYPE html>
<html>
  <input type="text" id="selection">
  <button onclick="compare()">Play</button>
  <h1 id="score">The Score is 0-0</h1>
</html>

标签: javascript

解决方案


欢迎来到 SO。

这里有几个问题:

  1. 您在加载页面(加载脚本时)而不是按下按钮时检索用户输入。

  2. 按下按钮时,您没有将任何值传递给比较函数。您正在调用“compare()”而不是“compare(value1, value2)”

  3. computerChoice 不会在每场比赛后重置。这不是真正的错误,因为它只是游戏玩法的问题。

这是一个显示它工作的小提琴:

https://jsfiddle.net/Lrbh8n3j/

这是代码:

<!DOCTYPE html>
<html>
 <input type="text" id="selection">
 <button onclick="doCompare()">Play</button>
 <h1 id="score">The Score is 0-0</h1>

 <script type="text/javascript">
 var totalScore = document.getElementById("score");
var yourScore = 0
var compScore = 0

  var doCompare = function() {
     var userChoice = document.getElementById("selection").value;
      var computerChoice = Math.random();
      if (computerChoice < 0.34) {
          computerChoice = "rock";
      } else if (computerChoice <= 0.67) {
          computerChoice = "paper"; 
      } else {
          computerChoice = "scissors";
      }

         return compare(userChoice, computerChoice);
  }

  var compare = function(choice1, choice2) {
      if(choice1 === choice2) {
          alert( "It's a tie.");
      }
    if (choice1 === "scissors") {
        if (choice2 === "rock") {
            alert("You lose")
            compScore += 1
            totalScore.innerHTML = "The score is " + yourScore + " - " + compScore
        }
      if (choice2 === "paper") {
          alert("you win")
          yourScore += 1
          totalScore.innerHTML = "The score is " + yourScore + " - " + compScore

     }

 }
    if (choice1 === "paper") {
        if (choice2 === "rock") {
            alert("You Win.")
            yourScore += 1
            totalScore.innerHTML = "The score is " + yourScore + " - " + compScore
        }
        if (choice2 === "scissors") {
            alert("You lose.")
            compScore += 1
            totalScore.innerHTML = "The score is " + yourScore + " - " + compScore

        }
  }
    if (choice1 ==="rock") {
        if (choice2 === "scissors"){
            alert("You win.")
            yourScore += 1 
            totalScore.innerHTML = "The score is " + yourScore + " - " + compScore
        }
        if (choice2 === "paper") {
            alert("You lose.")
            compScore += 1
            totalScore.innerHTML = "The score is " + yourScore + " - " + compScore

        }
    }
  }
 </script>
 </html>

推荐阅读