Javascript bot未定义的变量

时间:2016-07-24 14:19:18

标签: javascript bots

我正在使用Kik API创建机器人。应该发生什么,就是当我输入!hangman时,游戏初始化。 hangman布尔值执行此操作,然后变为非活动状态。然后,人们可以使用!hangman LETTER来猜测这个词。很简单。然而,有一个奇怪的错误。第一次!使用hangman,它正常工作。然而,第二次,代码崩溃,声称状态和不正确的未定义,即使我确实定义了它们。任何帮助表示赞赏!

var hangmanIsActive = false;
var hangmanBootup = true;

bot.onTextMessage((message) => {
    if (message.body.startsWith("!help")) {
        message.reply("I know the following commands:\n- !Hangman");
    }

    if (message.body.startsWith("!hangman")) {
        if (hangmanBootup == true){
            hangmanBootup = false;
            message.reply("Welcome to hangman!");

            var triesleft = "Tries left: "
            var inttriesleft = 10

            var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
                            "t", "u", "v", "w", "x", "y", "z"];

            var stage0 = triesleft + inttriesleft;

            var stage1 = "\n"
                        "\n"
                        "\n"
                        "              " + triesleft + {} + "\n"
                        "\n"
                        "\n"
                        "\n"
                        "_|___ \n";

            var stage2 = "\n"
                        " |\n"
                        " |\n"
                        " |            " + triesleft + {} + "\n"
                        " |\n"
                        " | \n"
                        " |\n"
                        "_|___ \n";

            var stage3 = "  _______\n"
                        " |/\n"
                        " |\n"
                        " |            " + triesleft + {} + "\n"
                        " |\n"
                        " |\n"
                        " |\n"
                        "_|___ \n";

            var stage4 = "  _______\n"
                        " |/    |\n"
                        " |\n"
                        " |            " + triesleft + {} + "\n"
                        " |\n"
                        " | \n"
                        " |\n"
                        "_|___ \n";

            var stage5 = "  _______\n"
                        " |/    |\n"
                        " |    (_)\n"
                        " |            " + triesleft + {} + "\n"
                        " |\n"
                        " |\n"
                        " |\n"
                        "_|___ \n";

            var stage6 = "  _______\n"
                        " |/    |\n"
                        " |    (_)\n"
                        " |     |      " + triesleft + {} + "\n"
                        " |     | \n"
                        " |\n"
                        " |\n"
                        "_|___ \n";

            var stage7 = "  _______\n"
                        " |/    |\n"
                        " |    (_)\n"
                        " |    \|      " + triesleft + {} + "\n"
                        " |     | \n"
                        " |\n"
                        " |\n"
                        "_|___ \n";

            var stage8 = "  _______\n"
                        " |/    |\n"
                        " |    (_)\n"
                        " |    \|/     " + triesleft + {} + "\n"
                        " |     | \n"
                        " |\n"
                        " |\n"
                        "_|___ \n";

            var stage9 = "  _______\n"
                        " |/    |\n"
                        " |    (_)\n"
                        " |    \|/     " + triesleft + {} + "\n"
                        " |     | \n"
                        " |    / \n"
                        " |\n"
                        "_|___ \n";

            var stage10 = "  _______\n"
                        " |/    |\n"
                        " |    (_)\n"
                        " |    \|/     " + triesleft + {} + "\n"
                        " |     | \n"
                        " |    / \ \n"
                        " |\n"
                        "_|___ \n";


            var words = ["testword"];

            var keyword = "testword";

            var currentstage = stage0;

            var status = keyword.replace(/a/g, "-").replace(/b/g, "-").replace(/c/g, "-").replace(/d/g, "-").replace(/e/g, "-")
                .replace(/f/g, "-").replace(/g/g, "-").replace(/h/g, "-").replace(/i/g, "-").replace(/j/g, "-")
                .replace(/k/g, "-").replace(/l/g, "-").replace(/m/g, "-").replace(/n/g, "-").replace(/o/g, "-")
                .replace(/p/g, "-").replace(/q/g, "-").replace(/r/g, "-").replace(/s/g, "-").replace(/t/g, "-")
                .replace(/u/g, "-").replace(/v/g, "-").replace(/w/g, "-").replace(/x/g, "-").replace(/y/g, "-").replace(/z/g, "-");

            var inttriesleft = 10

            var incorrectletters = []
        };

        message.reply("Word: " + status + "\nIncorrect letters: " + incorrectletters);
        message.reply(currentstage);

        if (message.body.split(" ").length != 2){
            message.reply("To suggest a letter, use \"!hangman a\" for example.");
            return;
        }

        var theinput = message.body.split(" ")[1];

        if (theinput.length > 1 || theinput.length == 0){
            message.reply("To suggest a letter, use \"!hangman a\" for example.");
            return;
        }

        if (alphabet.indexOf(theinput) > -1 == false){ // if the input is NOT in the array
            console.log("Not a letter of the alphabet: " + theinput);
            message.reply("You're supposed to use a letter of the alphabet!");
            return;
        }

        if (status.indexOf(theinput) > -1 || incorrectletters.indexOf(theinput) > -1){ // if input is already used, either correct or incorrectly
            console.log(status)
            console.log(incorrectletters)
            message.reply("You already used this letter!");
            return;
        }

        if (keyword.includes(theinput)){
            var statuslist = status.split("");
            for (var l in keyword){
                if (keyword[l] == theinput){
                    statuslist[l] = theinput;
                    }
                }
            status = statuslist.join().replace(",", "");

            if (status.indexOf("-") > -1 == false){
                message.reply("You won!");
                }
            return;
        }

        if (keyword.includes(theinput) == false){
            var inttriesleft = inttriesleft - 1;
            incorrectletters.push(theinput);

            if (inttriesleft == 9){
                var currentstage = stage1.format(inttriesleft);
                return;}

            if (inttriesleft == 8){
                var currentstage = stage2.format(inttriesleft);
                return;}

            if (inttriesleft == 7){
                var currentstage = stage3.format(inttriesleft);
                return;}

            if (inttriesleft == 6){
                var currentstage = stage4.format(inttriesleft);
                return;}

            if (inttriesleft == 5){
                var currentstage = stage5.format(inttriesleft);
                return;}

            if (inttriesleft == 4){
                var currentstage = stage6.format(inttriesleft);
                return;}

            if (inttriesleft == 3){
                var currentstage = stage7.format(inttriesleft);
                return;}

            if (inttriesleft == 2){
                var currentstage = stage8.format(inttriesleft);
                return;}

            if (inttriesleft == 1){
                var currentstage = stage9.format(inttriesleft);
                return;}

            if (inttriesleft == 0){
                var currentstage = stage10.format(inttriesleft)
                message.reply(currentstage);
                message.reply("You lost! Thanks for playing.");
                message.reply("The word was: " + keyword);
                }
        }

    };
});

1 个答案:

答案 0 :(得分:0)

首先,无需在;之后将}放在if语句之后。

其次,它们第二次没有被定义的原因是因为你在bot.onMessage函数中定义它们使它们成为局部函数,因此它们在函数完成时被销毁。相反,你应该在开头全局定义它们,如下所示:

var hangmanIsActive = false;
var hangmanBootup = true;
// Define them up here instead
var triesleft = "Tries left: "
var inttriesleft = 10
var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
                    "t", "u", "v", "w", "x", "y", "z"];

var stage0 = triesleft + inttriesleft;

var stage1 = "\n"
                "\n"
                "\n"
                "              " + triesleft + {} + "\n"
                "\n"
                "\n"
                "\n"
                "_|___ \n";

var stage2 = "\n"
                " |\n"
                " |\n"
                " |            " + triesleft + {} + "\n"
                " |\n"
                " | \n"
                " |\n"
                "_|___ \n";

var stage3 = "  _______\n"
                " |/\n"
                " |\n"
                " |            " + triesleft + {} + "\n"
                " |\n"
                " |\n"
                " |\n"
                "_|___ \n";

var stage4 = "  _______\n"
                " |/    |\n"
                " |\n"
                " |            " + triesleft + {} + "\n"
                " |\n"
                " | \n"
                " |\n"
                "_|___ \n";

var stage5 = "  _______\n"
                " |/    |\n"
                " |    (_)\n"
                " |            " + triesleft + {} + "\n"
                " |\n"
                " |\n"
                " |\n"
                "_|___ \n";

var stage6 = "  _______\n"
                " |/    |\n"
                " |    (_)\n"
                " |     |      " + triesleft + {} + "\n"
                " |     | \n"
                " |\n"
                " |\n"
                "_|___ \n";

var stage7 = "  _______\n"
                " |/    |\n"
                " |    (_)\n"
                " |    \|      " + triesleft + {} + "\n"
                " |     | \n"
                " |\n"
                " |\n"
                "_|___ \n";

var stage8 = "  _______\n"
                " |/    |\n"
                " |    (_)\n"
                " |    \|/     " + triesleft + {} + "\n"
                " |     | \n"
                " |\n"
                " |\n"
                "_|___ \n";

var stage9 = "  _______\n"
                " |/    |\n"
                " |    (_)\n"
                " |    \|/     " + triesleft + {} + "\n"
                " |     | \n"
                " |    / \n"
                " |\n"
                "_|___ \n";

var stage10 = "  _______\n"
                " |/    |\n"
                " |    (_)\n"
                " |    \|/     " + triesleft + {} + "\n"
                " |     | \n"
                " |    / \ \n"
                " |\n"
                "_|___ \n";


var words = ["testword"];

var keyword = "testword";

var currentstage = stage0;

var status = keyword.replace(/a/g, "-").replace(/b/g, "-").replace(/c/g, "-").replace(/d/g, "-").replace(/e/g, "-")
        .replace(/f/g, "-").replace(/g/g, "-").replace(/h/g, "-").replace(/i/g, "-").replace(/j/g, "-")
        .replace(/k/g, "-").replace(/l/g, "-").replace(/m/g, "-").replace(/n/g, "-").replace(/o/g, "-")
        .replace(/p/g, "-").replace(/q/g, "-").replace(/r/g, "-").replace(/s/g, "-").replace(/t/g, "-")
        .replace(/u/g, "-").replace(/v/g, "-").replace(/w/g, "-").replace(/x/g, "-").replace(/y/g, "-").replace(/z/g, "-");

var inttriesleft = 10

var incorrectletters = []

bot.onTextMessage((message) => {
    if (message.body.startsWith("!help")) {
        message.reply("I know the following commands:\n- !Hangman");
    }

    if (message.body.startsWith("!hangman")) {
        if (hangmanBootup == true){
            hangmanBootup = false;
            message.reply("Welcome to hangman!");
        }

        message.reply("Word: " + status + "\nIncorrect letters: " + incorrectletters);
        message.reply(currentstage);

        if (message.body.split(" ").length != 2){
            message.reply("To suggest a letter, use \"!hangman a\" for example.");
            return;
        }

        var theinput = message.body.split(" ")[1];

        if (theinput.length > 1 || theinput.length == 0){
            message.reply("To suggest a letter, use \"!hangman a\" for example.");
            return;
        }

        if (alphabet.indexOf(theinput) > -1 == false){ // if the input is NOT in the array
            console.log("Not a letter of the alphabet: " + theinput);
            message.reply("You're supposed to use a letter of the alphabet!");
            return;
        }

        if (status.indexOf(theinput) > -1 || incorrectletters.indexOf(theinput) > -1){ // if input is already used, either correct or incorrectly
            console.log(status)
            console.log(incorrectletters)
            message.reply("You already used this letter!");
            return;
        }

        if (keyword.includes(theinput)){
            var statuslist = status.split("");
            for (var l in keyword){
                if (keyword[l] == theinput){
                    statuslist[l] = theinput;
                }
            }
            status = statuslist.join().replace(",", "");

            if (status.indexOf("-") > -1 == false){
                message.reply("You won!");
            }
            return;
        }

        if (keyword.includes(theinput) == false){
            inttriesleft = inttriesleft - 1;
            incorrectletters.push(theinput);

            if (inttriesleft == 9){
                currentstage = stage1.format(inttriesleft);
                return;
            }

            if (inttriesleft == 8){
                currentstage = stage2.format(inttriesleft);
                return;
            }

            if (inttriesleft == 7){
                currentstage = stage3.format(inttriesleft);
                return;
            }

            if (inttriesleft == 6){
                currentstage = stage4.format(inttriesleft);
                return;
            }

            if (inttriesleft == 5){
                currentstage = stage5.format(inttriesleft);
                return;
            }

            if (inttriesleft == 4){
                currentstage = stage6.format(inttriesleft);
                return;
            }

            if (inttriesleft == 3){
                currentstage = stage7.format(inttriesleft);
                return;
            }

            if (inttriesleft == 2){
                currentstage = stage8.format(inttriesleft);
                return;
            }

            if (inttriesleft == 1){
                currentstage = stage9.format(inttriesleft);
                return;
            }

            if (inttriesleft == 0){
                currentstage = stage10.format(inttriesleft)
                message.reply(currentstage);
                message.reply("You lost! Thanks for playing.");
                message.reply("The word was: " + keyword);
            }
        }

    }
});

每次更改变量时,您似乎都在使用var,但只有在定义它们时才应该使用它。无需始终使用var:)