javascript - 为什么这总是平局?
问题描述
我正在使用 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>
解决方案
欢迎来到 SO。
这里有几个问题:
您在加载页面(加载脚本时)而不是按下按钮时检索用户输入。
按下按钮时,您没有将任何值传递给比较函数。您正在调用“compare()”而不是“compare(value1, value2)”
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>
推荐阅读
- kubernetes - 了解 Kubernetes 集群中的子网划分
- swift - 如何在点后用 2 位数字打印 Double 并在它们为零时隐藏?
- python - 正则表达式匹配可选点
- c# - 如何使用衍生 api 或 Autodesk.forge 库 c# 从 dwg 文件中检索图层列表
- c# - RabbitMQ:System.Net.Sockets.SocketException:现有连接被远程主机强行关闭
- ansible - 将 curl PUT 命令转换为 ansible uri
- java - 如何删除 XML 标记中的属性前缀?
- java - 功能
. TOP 不存在 - node.js - mongoose nodejs如何从数据库和用户项目数组中删除项目
- python - 如何获得卡方值作为 scipy.optimize.curve_fit 的输出?