Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript递归意外中断-警告长代码_Javascript_Oop_Recursion_Depth First Search - Fatal编程技术网

Javascript递归意外中断-警告长代码

Javascript递归意外中断-警告长代码,javascript,oop,recursion,depth-first-search,Javascript,Oop,Recursion,Depth First Search,我对以下代码有问题。在NewMaze中的maze函数的递归过程中,它似乎正在中断。我通过firebug运行了它,得到以下错误: parent is undefined: line 97 newmaze_generator.js [Break On This Error] self.visited = function() {return parent.visited();}; 我不知道为什么它会是未定义的,如果这是真正的问题的话 我知道这要求很多,所以请不要浪费你的时间,除非你真的想

我对以下代码有问题。在NewMaze中的maze函数的递归过程中,它似乎正在中断。我通过firebug运行了它,得到以下错误:

parent is undefined: line 97 newmaze_generator.js
    [Break On This Error] self.visited = function() {return parent.visited();}; 
我不知道为什么它会是未定义的,如果这是真正的问题的话

我知道这要求很多,所以请不要浪费你的时间,除非你真的想帮助一个新手javascript程序员。为了让事情变得更简单,我在我的网站上有完整的代码,这样你就可以实际运行这个程序,而这个程序目前什么也做不了

另一方面,这只是我第二次涉足javascript编程,所以我很容易犯下愚蠢的错误

我的MazeGenerator

function NewMaze(_grid) {
    var self = this;
    var stack = new Array();
    var grid = _grid;

    /**
     * A recursive depth-first-search style maze generator
     */
    self.maze = function() {
        var randx = Math.floor(Math.random()*(grid.getWidth()));
        var randy = Math.floor(Math.random()*(grid.getHeight()));
        var startOfMaze = new MyCell(grid.getCell(randx, randy));
        stack.push(startOfMaze);
        mazeHelper(startOfMaze);
    };

    /**
     * Internal recursive method used by maze.
     * A depth-first-search style algorithm.
     * @param mycell current cell
     * @returns {Boolean} true once finished
     */
    function mazeHelper(myCell) {
        var neighbors = myCell.getOpenNeighbors();
        myCell.markVisited();
        if (neighbors.length > 0) {
            var next = Math.floor(Math.random()*(neighbors.length));
            var nextCell = neighbors[next];
            stack.push(nextCell);
            removeWall(myCell, nextCell);
//          alert(myCell.getParent().getArrayLocation().getX()+" , "+myCell.getParent().getArrayLocation().getY());
            mazeHelper(nextCell);
        } else {
            alert("else");
            if (stack.length == 0) return true;
            else mazeHelper(stack.pop());
        };
    };

    /**
     * Removes the wall between the given cells
     * @param fromCell
     * @param toCell
     */
    function removeWall(fromCell, toCell) {
        var xDif = toCell.getParent().getArrayLocation().getX() - fromCell.getParent().getArrayLocation().getX();
        var yDif = toCell.getParent().getArrayLocation().getY() - fromCell.getParent().getArrayLocation().getY();

        if (xDif == -1) toCell.seWall = false;  // if moving up x axis
        if (xDif == 1) fromCell.seWall = false; // if moving down x axis
        if (yDif == -1) toCell.swWall = false;  // if moving up y axis
        if (yDif == 1) fromCell.swWall = false; // if moving down y axis

    };

    /**
     * @returns an instance of 'this' named self.
     */
    self.getSelf = function() {return self;};

    /**
     * Inner class that is an extension of Cell class
     * @param cell the parent cell
     * @returns {MyCell} instance of 'this' child
     */
    function MyCell(cell) {
        var self = this;
        var parent = cell;

        /**
         * Marks the referenced cell as visited using the super class's visited method.
         */
        self.markVisited = function() {parent.markVisited();};

        self.visited = function() {return parent.visited();};

        /**
         * Creates and returns an array of neighbors that have not been visited.
         * @returns {<MyCell>Array} an array of adjacent cells that have not been visited. 
         */
        self.getOpenNeighbors = function() {
            var neighbors = new Array();

            var tc0, tc1, tc3, tc4;
            var parentX = parent.getArrayLocation().getX();
            var parentY = parent.getArrayLocation().getY();

            tc0 = new MyCell(grid.getCell(parentX-1, parentY));
            tc1 = new MyCell(grid.getCell(parentX+1, parentY));
            tc2 = new MyCell(grid.getCell(parentX, parentY-1));
            tc3 = new MyCell(grid.getCell(parentX, parentY+1));

            if (tc0 != undefined && !tc0.visited()) neighbors.push(tc0);
            if (tc1 != undefined && !tc1.visited()) neighbors.push(tc1);
            if (tc2 != undefined && !tc2.visited()) neighbors.push(tc2);
            if (tc3 != undefined && !tc3.visited()) neighbors.push(tc3);

            return neighbors;
        };

        /**
         * @returns an instance of 'this' named self.
         */
        self.getSelf = function() {return self;};

        /**
         * @returns an instance of the 'super' class named parent.
         */
        self.getParent = function() {return parent;};
    };
};

好吧,我知道了!我犯了一个愚蠢的错误,我不确定这是否是件好事

问题是:

getOpenNeights函数中,我正在检查单元格是否未定义,如果未定义,请不要将其添加到可能要移动到的邻居数组中。嗯,未定义的支票写得不正确。我将这些单元格定义为:

tc0 = new MyCell(grid.getCell(parentX-1, parentY));
如果给定位置不存在单元格,则grid.getCell(x,y)方法将返回undefined,这正是我希望它执行的操作

但是,在检查中,我确保没有将其添加到我编写的数组中:

if (tc0 != undefined && !tc0.visited()) neighbors.push(tc0);
很明显,我以这种方式创建的tc0或任何tc0-x对象都不会是未定义的。我需要做的是检查作为参数传递的对象是否未定义。我将这个对象视为父类或超类,所以我所要做的就是使用我的方法getParent(),一切都会很顺利

if (tc0.getParent() != undefined && !tc0.visited()) neighbors.push(tc0);

现在一切都好了!不幸的是,迷宫看起来不像它应该的样子,所以我要在那里解决一些问题,但至少我现在可以前进了

据我所知,您的“单元格”没有与之关联的已访问方法。由于它们是myCell对象的父对象,因此未定义parent.visited()。cell类具有以下方法:self.visited=function(){return visited;};此后,我在迷宫生成器的内部类MyCell的已访问函数中添加了一个try-catch,我正在捕获没有父对象的对象,我只是不明白为什么会发生这种情况。
if (tc0 != undefined && !tc0.visited()) neighbors.push(tc0);
if (tc0.getParent() != undefined && !tc0.visited()) neighbors.push(tc0);