我正在制作一个小石头剪刀游戏,我认为我完美地计划了一切,但出于某种原因,当你尝试游戏时没有任何反应。
基本上,有三个图像,一块石头,一张纸和一些剪刀,然后你点击一个。使用我键入的代码,计算机生成一个随机值,该值将转换为摇滚,纸张或剪刀。当玩家点击图像时,它会将玩家选择设置为摇滚,纸张或剪刀。然后javascript运行一些if else语句来比较两个选项,并决定胜利者。
代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Rock Paper Scissors</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style>
body {
font-family: Roboto, Arial;
}
.choose img {
width: 150px;
margin: 20px;
}
</style>
</head>
<body>
<div class="choose" align="center">
<h2 id="question">Rock, paper, or scissors?"</h2>
<img src="http://img2.wikia.nocookie.net/__cb20141120133208/epicrapbattlesofhistory/images/4/4b/A_Rock!.gif" id="rock"><img src="http://www.clipartpal.com/_thumbs/pd/education/paper_sheet.png" id="paper"><img src="http://www2.fiskars.com/var/fiskars_amer/storage/images/frontpage2/sewing-quilting/products/scissors-and-sharpeners/the-original-orange-handled-scissors-8/16933-6-eng-US/The-Original-Orange-Handled-Scissors-8_product_main.png" id="scissors">
</div>
<script>
var computerChoice = math.random();
var userChoice = null;
//Change randomly generated number to rock, paper, or scissors
if (computerChoice < .33) {
computerChoice == "rock";
} else if (computerChoice < .66) {
computerChoice == "paper";
} else {
computerChoice == "scissors";
};
//Set userChoice variable to rock, paper, or scissors
function convertUserChoice() {
$('#rock').click(function() {
userChoice == "rock";
});
$('#paper').click(function() {
userChoice == "paper";
});
$('#scissors').click(function() {
userChoice == "scissors";
});
};
//Compare computerChoice with userChoice, decide who wins
if (userChoice == computerChoice) {
alert ("Tie!");
} else if (userChoice == "rock") {
if (computerChoice == "scissors") {
alert ("You win!");
} else {
alert ("You lose.");
};
} else if (userChoice == "paper") {
if (computerChoice == "rock") {
alert ("You win!");
} else {
alert ("You lose.");
};
} else if (userChoice == "scissors") {
if (computerChoice == "paper") {
alert ("You win!");
} else {
alert ("You lose");
};
};
</script>
</body>
</html>
我认为问题在于,当用户点击图片时,标记为“将computerChoice与userChoice比较,决定谁获胜”的js代码未运行。如果是这样,我怎么能这样做?
答案 0 :(得分:1)
您的代码中存在一些问题:
convertUserChoice()
。只需删除该功能即可使用。=
代替==
。需要保留而不是比较。computerChoice
和checkChoise
创建功能,以便在需要时调用它们。Math
中所提到的,upercase是必需的。请在此jsFiddle及以下版本中找到您的代码。
<强>更新强>
您可以使用支票对象来改善您的获胜支票。该对象存储用户的每个获胜情况。例如如果用户选择摇滚,那么winningCombinations['rock']
将返回值scissors
,如果computerChoice
是剪刀,那将是一个获胜的情况,比较将返回true,字符串win
将是结果。 (参见下面的更新代码。)
这比你的if / else条件容易。
var computerChoice = null,
userChoice = null;
var newComputerChoice = function () {
computerChoice = Math.random();
//Change randomly generated number to rock, paper, or scissors
if (computerChoice < .33) {
computerChoice = "rock";
} else if (computerChoice < .66) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
};
console.log('pc choice: ', computerChoice);
};
//Set userChoice variable to rock, paper, or scissors
//function convertUserChoice() {
$('.choose>img').click(function () {
var id = $(this).attr('id');
userChoice = id.substring(0, id.length); //#rock --> remove #
console.log('user choice: ', userChoice);
newComputerChoice();
checkChoice();
});
var checkChoice = function () {
var message = function(msgType) {
var msgTypes = ['win', 'lose'],
index = msgTypes.indexOf(msgType),
format = ( index != -1 ) ? 'You %1$s!': 'Tie!';
alert(sprintf(format, msgTypes[index]));
};
var winningCombinations = {
// userchoice: computerchoice
rock: 'scissors', // you win
paper: 'rock', // you win
scissors: 'paper' // you win
};
var result;
//Compare computerChoice with userChoice, decide who wins
if (userChoice == computerChoice) {
message('tie');
}
else {
result = ( winningCombinations[userChoice] == computerChoice ) ? 'win': 'lose';
message(result);
}
};
body {
font-family: Roboto, Arial;
}
.choose img {
width: 150px;
margin: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="choose" align="center">
<h2 id="question">Rock, paper, or scissors?"</h2>
<img src="http://img2.wikia.nocookie.net/__cb20141120133208/epicrapbattlesofhistory/images/4/4b/A_Rock!.gif" id="rock">
<img src="http://www.clipartpal.com/_thumbs/pd/education/paper_sheet.png" id="paper">
<img src="http://www2.fiskars.com/var/fiskars_amer/storage/images/frontpage2/sewing-quilting/products/scissors-and-sharpeners/the-original-orange-handled-scissors-8/16933-6-eng-US/The-Original-Orange-Handled-Scissors-8_product_main.png" id="scissors">
</div>