我正在开发一款RPS游戏,在设置它之前,让它在基本级别上工作时遇到了一些麻烦。这是我的HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="rpsfinal1.css">
<title>Shattered Template</title>
</head>
<body>
<div class="corners">
<h1 class="title">Rock Paper Scissors!</h1>
<p>Welcome to a Rock Paper Scissors web app!1
</p>
<script type="text/javascript" src="rps.js"></script>
<a href='linkhref.html' id='mylink'>Play Again</a>
<input type="button" value="Rock" onClick="rockChoice()">
<input type="button" value="Paper" onclick="paperChoice()">
<input type="button" value="Scissors" onClick="scissorsChoice()">
<p id="output"></p>
</div>
</body>
</html>
这是我的JS:
var compare = function(choice1,choice2) {
if (choice1 === "rock") {
if (choice2 === "rock") {
console.log("It is a tie! The computer chose rock!");
alert ("It is a tie!");
}
else if (choice2 === "paper") {
console.log ("Sorry, you loose. :(");
alert ("Sorry, you lose. :( The computer chose paper!");
}
else {
console.log ("You win!");
alert ("You win! The computer chose scissors!");
}
}
else if (choice1 === "paper") {
if (choice2 === "rock") {
console.log ("You win!");
alert ("You win! The computer chose rock!");
}
else if (choice2 === "paper") {
console.log ("It is a tie!");
alert ("It is a tie! The computer chose paper!");
}
else {
console.log("Sorry, you lose. :(");
alert ("Sorry, you lose. :( The computer chose scissors!");
}
}
else {
if (choice2 === "rock") {
console.log ("You win!");
alert ("You win! The computer chose rock!");
}
else if (choice2 === "paper") {
console.log ("Sorry, you lose. :(");
alert ("Sorry, you lose. :( The computer chose paper!");
}
else {
console.log ("It is a tie!");
alert ("It is a tie! The computer chose scissors!");
}
}
};
var rockChoice = function() {
var userChoice = "rock";
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice >= 0 && computerChoice <= 0.33) {
computerChoice = "rock";
console.log(computerChoice);
}
else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
computerChoice = "paper";
console.log(computerChoice);
}
else {
computerChoice = "scissors";
console.log(computerChoice);
}
compare(userChoice,computerChoice);
}
var paperChoice = function() {
var userChoice = "paper";
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice >= 0 && computerChoice <= 0.33) {
computerChoice = "rock";
console.log(computerChoice);
}
else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
computerChoice = "paper";
console.log(computerChoice);
}
else {
computerChoice = "scissors";
console.log(computerChoice);
}
compare(userChoice,computerChoice);
}
var scissorsChoice = function() {
var userChoice = "scissors";
var computerChoice = Math.random();
console.log(computerChoice);
if (computerChoice >= 0 && computerChoice <= 0.33) {
computerChoice = "rock";
console.log(computerChoice);
}
else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
computerChoice = "paper";
console.log(computerChoice);
}
else {
computerChoice = "scissors";
console.log(computerChoice);
}
compare(userChoice,computerChoice);
}
我认为代码不起作用的原因是我可能没有正确调用函数。请告诉我。 谢谢!
答案 0 :(得分:2)
我知道它有点晚了但对于那些正在寻找同样事物的人: - 您可能会收到#34;语法错误&#34;因为你在JS文件的脚本末尾有一个额外的结束括号(})。