RPS游戏应用程序不工作

RPS Game App Not Working

本文关键字:工作 应用程序 游戏 RPS      更新时间:2023-09-26

我正在开发一款RPS游戏,在设计它之前,我在让它在基本级别上运行时遇到了一些麻烦

<!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);
    }

我认为代码不起作用的原因是我可能没有正确调用函数。请告诉我。谢谢

我知道现在有点晚了,但对于那些正在寻找同样东西的人来说:-您可能会遇到"语法错误",因为在JS文件的脚本末尾有一个额外的括号(})。