javascript - RPS 游戏应用程序无法运行

标签 javascript html

我正在开发一款 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);
    }

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

最佳答案

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

关于javascript - RPS 游戏应用程序无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20124258/

相关文章:

javascript - 如何告诉 Vue 应用使用 Firebase 模拟器?

html - css 网格创建具有自动填充功能的空列

javascript - 将数组中的一项附加到一个 div

java - 在 GWT 中遍历动态无序列表

Javascript 表单提交 - anchor 与按钮

javascript - 重新绘制 AmStockChart

javascript - Jquery 事件监听器不会从本地转移到服务器

javascript - 一次循环 2 个元素

javascript - 具有动态镜像字段的 Bootstrap Datetimepicker

html - 水平菜单下拉