0

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

游戏应该可以运行,至少在我将其与模块分开之前它可以运行。这是一个简单的数学游戏,用户得到随机的+,问题并且必须正确回答

问题

我似乎无法向用户提供任何提示。我也无法在 chrome 开发工具中调试它,你能立即看到任何看起来不对的地方吗?非常感谢您的帮助:)

这是 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";
        }
    };
4

1 回答 1

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 演示

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

<script src="mathGame.js"></script>
<script src="mathGame.logic.js"></script>
<script src="mathGame.ui.js"></script>
<script src="mathGame.play.js"></script>
于 2012-09-29T12:40:19.100 回答