简单的数学游戏问题(初学者的东西)

时间:2012-09-29 11:48:00

标签: javascript html math project

我有一个学校项目,我们正在制作一个没有UI的小型JavaScript游戏;这意味着我们只能使用提示,警报或其他弹出脚本。

游戏应该可以工作,至少在我用模块将它拆开之前就已经完成了。这是一个简单的数学游戏,用户得到随机+,问题并且必须正确回答它们

问题

我似乎无法向用户提示任何提示。我也无法在chrome dev工具中调试它,你能看到任何看似错误的东西吗?感谢任何帮助:)

继承人JSfiddle

http://jsfiddle.net/vuTGa/1/

这是我们的代码,我只发布了重要的部分 - 我省略了index.html和Mathgame.js,因为它们似乎工作得很完美,而且它们不包含很多代码。

MathGame.logic.js

mathGame.logic = (function() {
    "use strict";
    var createQuestion, getQuestion;
    createQuestion = function() {
        var tal1, tal2;
        tal1 = Math.ceil(Math.random() * 10);
        tal2 = Math.ceil(Math.random() * 10);
        return {
            tal1: tal1,
            tal2: tal2,
            result: function() {
                return tal1 + tal2;
            }
        };
    };
    getQuestion = function() {
        return createQuestion();
    };
    return {
        getQuestion: getQuestion
    };
}());

MathGame.play.js

mathGame.play = function() {
    "use strict";
    var question, guess, answer, correct, questionGuess;
    // Starts game for user
    mathGame.ui.startCountDown();
    // Starts the timer in .logic
    // mathGame.logic.startCountDown();
    // Get random math
    question = mathGame.logic.getQuestion();
    // Send random math to User
    questionGuess = mathGame.ui.askMathQuestion(question.tal1, question.tal2);
    // The users guess
    guess = mathGame.ui.returnMathGuess;
    // See if the question is the same as the guess
    correct = (question() === guess);
    // Show the user how it went
    mathGame.ui.showResult(correct, guess, question);



    ##Mathgame.ui.js##
    mathGame.ui = {

        startCountDown: function() {
            "use strict";
            // Visa ready set go
            alert("READY");
            alert("SET");
            alert("GO");
        },
        askMathQuestion: function() {
            "use strict";
            prompt("askMathQuestion");
            //shows a math question to user
            // return Number(prompt(value1 + symbol +  value2));
            // e.g. value1 = 12
            //      value2 = 13
            //        symbol = "+"
            // 12 + 13  
            // return user guess
        },
        returnMathGuess: function() {
            "use strict";
        },
        showResult: function() {
            "use strict";
        }
    };

1 个答案:

答案 0 :(得分:1)

好吧,到目前为止,我只能查明代码中的小问题。由于您使用的是严格模式,因此无法全局访问窗口对象的属性。因此,您需要使用window.alert或设置变量:

var alert = this.alert; // "this" being the global, window object

我注意到的第一件事是你的math.play函数声明没有结束括号。我修好了。但是你遇到的真正问题是你在创建mathGame之前引用了它们的属性。例如,在mathGame.play()的定义中,您运行了函数mathGame.ui.startCountDown();,但在调用下方的函数中定义了mathGame.ui。所以我把它取出来,以便它可以访问它。这是你的脚本的一般问题。

还有一个部分你将一个对象称为一个函数:

correct = (question() === guess);

question已经被定义为函数mathGame.logic.getQuestion();的返回值,它是一个字符串。我认为你对此感到困惑:

question = mathGame.logic.getQuestion;

correct = (question() === guess); // now this works

我还找到了一些我认为多余的东西。如果希望整个脚本处于严格模式,则在严格模式下为其创建一个闭包:

(function() {
    "using strict";
    // everything below is in strict mode
})();

以下是整个代码:

(function() {
    "using strict";
    var mathGame = {},
        alert = this.alert,
        prompt = this.prompt;

    mathGame.play = function() {
        var question, guess, answer, correct, questionGuess;
        // Starts game for user
        mathGame.ui.startCountDown();
        // Starts the timer in .logic
        // mathGame.logic.startCountDown();
        // Get random math
        mathGame.logic = (function() {
            var createQuestion, getQuestion;
            createQuestion = function() {
                var tal1, tal2;
                tal1 = Math.ceil(Math.random() * 10);
                tal2 = Math.ceil(Math.random() * 10);
                return {
                    tal1: tal1,
                    tal2: tal2,
                    result: function() {
                        return tal1 + tal2;
                    }
                };
            };
            getQuestion = function() {
                return createQuestion();
            };
            return {
                getQuestion: getQuestion
            };
        }());

        question = mathGame.logic.getQuestion();
        // Send random math to User
        questionGuess = mathGame.ui.askMathQuestion(question.tal1, question.tal2);
        // The users guess
        guess = mathGame.ui.returnMathGuess;
        // See if the question is the same as the guess
        correct = (question === guess);
        // Show the user how it went
        mathGame.ui.showResult(correct, guess, question);
    };

    mathGame.ui = {

        startCountDown: function() {
            // Visa ready set go
            alert("READY");
            alert("SET");
            alert("GO");
        },
        askMathQuestion: function() {
            prompt("askMathQuestion");
            //shows a math question to user
            // return Number(prompt(value1 + symbol +  value2));
            // e.g. value1 = 12
            //      value2 = 13
            //        symbol = "+"
            // 12 + 13  
            // return user guess
        },
        returnMathGuess: function() {},
        showResult: function() {}

    };
    mathGame.play();
}).call(this); // global object

JSFiddle Demo

请注意,在代码的HTML部分中,我删除了一些脚本文件,因为它们在网站中不存在。如果你再次需要它们,那么它们就是:

<script src="mathGame.js"></script>
<script src="mathGame.logic.js"></script>
<script src="mathGame.ui.js"></script>
<script src="mathGame.play.js"></script>