Java迷宫DFS Nullpointer-Exception

时间:2015-07-29 10:05:42

标签: java maze

我尝试使用DFS-Algorithm在Java中编写迷宫生成器。 但是我遇到了一个问题。每当我运行这个程序时,我得到Nullpointerexception,因为Stack.pop调用在某个时刻返回null(因为它变空)。我无法找到我的错误,但也许我太累了:P

代码:

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import java.util.Stack;

    public class FieldGenerator {

    public static Cell[][] getField(int width, int height){
        Random rand = new Random(System.currentTimeMillis());
        int total = width * height;
        Cell[][] retField = new Cell[height][width];
        for(int y = 0; y < height; y++){
            for(int x = 0; x < width; x++){
                retField[y][x] = new Cell(cellstate.UNWALKABLE, x, y);
            }
        }
        int xStart = rand.nextInt(width);
        int yStart = rand.nextInt(height);
        Cell currentCell = retField[yStart][xStart];
        currentCell.setCell_state(cellstate.STARTINGPOINT);
        Stack<Cell> stack = new Stack<Cell>();
        stack.setSize(total);
        int visited = 1;
        while(visited < total){
            List<Cell> neighbourList = getNeighbours(currentCell, retField, width, height);
            if(neighbourList.size() > 0){ //1 or more neighbours with walls intact
                Cell randNeighbour = neighbourList.get(rand.nextInt(neighbourList.size()));
                clearWallBetween(currentCell, randNeighbour, retField);
                stack.push(currentCell);
                currentCell = randNeighbour;
                visited++;
                if(visited >= total)
                    currentCell.setCell_state(cellstate.ENDINGPOINT);
            }else{
                currentCell = stack.pop();
            }
        }
        return retField;
    }

    private static List<Cell> getNeighbours(Cell searchCell, Cell[][] searchField, int width, int height){
        List<Cell> retList = new ArrayList<Cell>();
        int x = searchCell.getx();
        int y = searchCell.gety();

        if(findCellByCoordinates(x, y-2, width, height, searchField) != null && findCellByCoordinates(x, y-2, width, height, searchField).getCell_state() == cellstate.UNWALKABLE){ //Top
            retList.add(findCellByCoordinates(x, y-2, width, height, searchField));
        }
        if(findCellByCoordinates(x-2, y, width, height, searchField) != null && findCellByCoordinates(x-2, y, width, height, searchField).getCell_state() == cellstate.UNWALKABLE) { //Left
            retList.add(findCellByCoordinates(x-2, y, width, height, searchField));
        }
        if(findCellByCoordinates(x+2, y, width, height, searchField) != null && findCellByCoordinates(x+2, y, width, height, searchField).getCell_state() == cellstate.UNWALKABLE) { //Right
            retList.add(findCellByCoordinates(x+2, y, width, height, searchField));
        }
        if(findCellByCoordinates(x, y+2, width, height, searchField) != null && findCellByCoordinates(x, y+2, width, height, searchField).getCell_state() == cellstate.UNWALKABLE) { //Bottom
            retList.add(findCellByCoordinates(x, y+2, width, height, searchField));
        }

        return retList;
    }

    private static Cell findCellByCoordinates(int xcoord, int ycoord, int width, int height, Cell[][] field){
        if(xcoord >= width || xcoord < 0 || ycoord >= height || ycoord < 0) {
            return null;
        }else{
            return field[ycoord][xcoord];
        }
    }

    private static void clearWallBetween(Cell cell1, Cell cell2, Cell[][] field){
        cell2.setCell_state(cellstate.WALKABLE);
        int x = ((cell1.getx() + cell2.getx()) / 2);
        int y = ((cell1.gety() + cell2.gety()) / 2);
        field[y][x].setCell_state(cellstate.WALKABLE);
    }

}

1 个答案:

答案 0 :(得分:0)

确定。感谢Kenney的帮助。 我添加了一个“死胡同”的条件,现在它就像一个魅力! 这是工人阶级的完整来源:

{{1}}
相关问题