简体   繁体   English

Javascript游戏问题

[英]Problems with Javascript Game

I'm working on what should be an extremely simple "guess my number" game in Javascript, however, I can't get it to work correctly. 我正在使用Javascript开发一款非常简单的“猜我的数字”游戏,但是,我无法使其正常运行。

The program is supposed to choose a random integer between 1 and 10 and allow the user to try and guess that number as many times as it takes to get it right. 该程序应该选择一个介于1到10之间的随机整数,并允许用户尝试并猜测该数字以使其正确无误。 If the user guesses a number that's lower than the correct one, the program is supposed to display a "your guess is too low" message, this is the same for when the guess is too high, or correct. 如果用户猜测的数字小于正确的数字,则该程序应该显示“您的猜测太低”的消息,这对于猜测太大或正确的情况是相同的。 The program must display the message on the webpage, not in an alert or any type of pop-up. 该程序必须在网页上显示该消息,而不是在警报或任何类型的弹出窗口中显示。 Also, the program is supposed to keep a running list of the numbers that have been guessed by the user and display them on the page each time the user makes a guess (I want this list to display below my heading "Here are the numbers you've guessed so far:"). 另外,该程序应该保留用户猜测的数字的运行列表,并在用户每次进行猜测时将其显示在页面上(我希望此列表显示在标题“下面是您到目前为止,我已经猜到了:”)。 And finally, the program is supposed to display a "you already tried using that number" message if the user tries to input a number that they've already used. 最后,如果用户尝试输入他们已经使用过的数字,则该程序应该显示“您已尝试使用该数字”消息。

Here is my code, along with a JSfiddle I've created and am still playing with. 这是我的代码,以及我创建并仍在使用的JSfiddle。

<html>
    <head>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
        <title>Guess My Number</title>

        <script type = "text/javascript">
            var tries = [];
            game = {
                 num : 0,
                 turns : 1,
                 reset : function() {
                        this.turns = 1;
                        this.newNum();
                 },
                 newNum : function() {
                    this.num = parseInt(Math.random(), 10) + 1;
                 },
                 guessNumber : function(guess) {
                        try {
                            guess = parseInt(guess, 10);
                        }
                        catch (e) {
                            document.getElementById("guess").value = "Enter a guess.";
                            this.turns++;
                            return false;
                        }
                        if (guess == this.num) {
                            document.getElementById("result").value = "Correct! It took you " + this.turns + " tries to guess my number.";
                            tries.push(document.getElementById("guess").value);
                            display();
                            document.getElementById("guess").value = " ";
                            return true;
                        }
                        else if(guess > this.num) {
                            document.getElementById("result").value = "Your guess is too high. Try again.";
                            tries.push(document.getElementById("guess").value;
                            display();
                            document.getElementById("guess").value = " ";
                            return false;
                        }
                        else if(tries.indexOf(guess) != -1 {   
                            document.getElementById("result").value = "You have already guessed that number. Try again.";
                            document.getElementById("guess").value = " ";
                            return false;
                        }
                        else {
                            document.getElementById("result").value = "Your guess is too low. Try again.";
                            tries.push(document.getElementById("guess").value;
                            display();
                            document.getElementById("guess").value = " ";
                            return false;  
                        }
                    }
                    function display() {
                        var number = ";
                            for(i=0; i<tries.length; i++) {
                                    number += tries[i] + "<br >";
                            }
                            document.getElementById("tries").innerHTML = number;
                    }
                    function guessNumber() {
                        var guess = document.getElementById("guess").value;
                        game.guessNumber(guess);
                        document.getElementById("guess").value = " ";
                    }
                    function resetGame() {
                        game.reset();
                        document.getElementById("guess").value = " ";
                    }
                    resetGame();
            }
        </script>
    </head>

    <body>
        <h1>Would You Like To Play A Game?</h1>
        <h3>Directions:</h3>
                <p>
                The game is very simple. I am thinking of a non-decimal number between 1 and 10, it is your job to guess what that number is. If you guess incorrectly on your first attempt, don't worry. You can keep guessing until you guess my number.
            </p>
        <h3>Thanks for playing and good luck!</h3>
        <h3>Created by Beth Tanner</h3>
        <p>Your Guess:
            <input type = "text" id = "guess" size = "10" />
            <br />
            <input type = "button" value = "Submit Guess" onclick = "guessNumber()" onclick = "display()" />
            <input type = "reset" value = "Reset Game" onclick = "resetGame()" />
        </p>
        <h3>How'd you do?</h3>
            <div id = "result">
                <input type = "text" id = "result" size = "20" />
            </div>
        <h3>Here are the numbers you've guessed so far:</h3>
    </body>
</html>

http://jsfiddle.net/v09ttL74/ http://jsfiddle.net/v09ttL74/

As you should be able to see from the fiddle, nothing is happening when I click either one of the buttons, so I'm not really sure where the problem is. 正如您应该从小提琴中看到的那样,当我单击其中一个按钮时,什么也没有发生,因此我不确定问题出在哪里。 I am getting a few errors when I select the JShints option on the fiddle, but most are things like "Missing semicolon" in places where there's not supposed to be a semicolon, like an one line where all I have written is 在小提琴上选择JShints选项时,我会遇到一些错误,但是大多数情况是在不应该使用分号的地方,例如“我想写分号”,就像写了一行

else {

Any suggestions would be greatly appreciated. 任何建议将不胜感激。 I feel like this is just a really simple project that I am over-thinking since I'm stressed about it not working. 我觉得这只是一个非常简单的项目,我过分考虑,因为我被迫无法正常工作。

There's a lot of issues in your code. 您的代码中有很多问题。 Just to mention a few... 仅举几例...

1- Unclosed parenthesis here 1-此处未加括号

tries.push(document.getElementById("guess").value;

it should be... 它应该是...

tries.push(document.getElementById("guess").value);

2- Expression not terminated with a close parenthesis here 2-此处的表达式不以右括号结尾

else if(tries.indexOf(guess) != -1 { 

it should be... 它应该是...

else if(tries.indexOf(guess) != -1) { 

3- Wrong declaration of functions inside a function 3-函数内部的函数声明错误

function display() {
                    var number = ";
                        for(i=0; i<tries.length; i++) {
                                number += tries[i] + "<br >";
                        }
                        document.getElementById("tries").innerHTML = number;
                }
                function guessNumber() {
                    var guess = document.getElementById("guess").value;
                    game.guessNumber(guess);
                    document.getElementById("guess").value = " ";
                }
                function resetGame() {
                    game.reset();
                    document.getElementById("guess").value = " ";
                }

I could be going forever but, really, the errors are a bit obvious 我可能会永远走下去,但实际上,错误有点明显

Alright as some of the answers must have already given you an idea of how many mistakes your Script as well as HTML code had.. from missing parenthesis to same ids to missing elements. 好的,因为某些答案必须已经使您了解了脚本以及HTML代码有多少错误。从缺少括号到相同的id到缺少的元素。 Well i have corrected them and it's working now: 好吧,我已经纠正了它们,并且现在可以正常工作:

<h1>Would You Like To Play A Game?</h1>
<h3>Directions:</h3>
    <p>
        The game is very simple. I am thinking of a non-decimal number between 1 and 10, it is your job to guess what that number is. If you guess incorrectly on your first attempt, don't worry. You can keep guessing until you guess my number.
</p>
<h3>Thanks for playing and good luck!</h3>
<h3>Created by Beth Tanner</h3>
<p>Your Guess:
    <input type = "text" id = "guess" size = "10" />
    <br />
    <input type = "button" value = "Submit Guess" onclick = "guessNumber()" onclick = "display()" />
    <input type = "reset" value = "Reset Game" onclick = "resetGame()" />
</p>
<h3>How'd you do?</h3>

    <input type = "text" id = "result" size = "20" />

<h3>Here are the numbers you've guessed so far:</h3>
<p id='tries'></p>

Script: 脚本:

var tries = [];
game = {
    num : 0,
    turns : 1,
    reset : function() {
        this.turns = 1;
        this.newNum();
    },
    newNum : function() {
        this.num = parseInt(Math.random() * 10) + 1;
    },
    guessNumber : function(guess) {
        try {        
            guess = parseInt(guess, 10);
        }
        catch (e) {
            document.getElementById("guess").value = "Enter a guess.";
            this.turns++;
            return false;
        }
        alert(guess+ ' '+this.num);
        if (guess == this.num) {
            document.getElementById("result").value = "Correct! It took you " + this.turns + " tries to guess my number.";
            tries.push(document.getElementById("guess").value);
            display();
            document.getElementById("guess").value = " ";
            return true;
        }
        else if(tries.indexOf(guess) != -1) {   
            document.getElementById("result").value = "You have already guessed that number. Try again.";
            document.getElementById("guess").value = " ";
            return false;
        }
        else if(guess > this.num) {
            document.getElementById("result").value = "Your guess is too high. Try again.";
            tries.push(document.getElementById("guess").value);
            display();
            document.getElementById("guess").value = " ";
            return false;
        }
        else if(guess < this.num){          
            document.getElementById("result").value = "Your guess is too low. Try again.";
            tries.push(document.getElementById("guess").value);
            display();
            document.getElementById("guess").value = " ";
            return false;  
        }


    }
}
    function display() {
        var number = "";
        for(i=0; i<tries.length; i++) {
            number += tries[i] + "<br >";
        }
        document.getElementById("tries").innerHTML = number;
    }
    function guessNumber() {
        var guess = document.getElementById("guess").value;
        game.guessNumber(guess);
        document.getElementById("guess").value = " ";
    }
    function resetGame() {
        game.reset();
        document.getElementById("guess").value = " ";
    }
    resetGame();

See the DEMO here 在这里查看演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM