Java深度优先搜索迷宫生成算法陷入困境

Java深度优先搜索迷宫生成算法陷入困境,java,depth-first-search,maze,Java,Depth First Search,Maze,我有一个深度优先搜索的迷宫生成算法。 它首先生成一个原始迷宫,如下所示: 然后使用以下算法创建迷宫: 结果如下所示: 红色像素表示目标,绿色像素表示开始。 它被卡在球门上,来回跳跃,直到它认为它完成了。 迷宫生成器源: public Maze generate(int width, int height) { Maze maze = getRawMaze(width, height); List<Point> cellStack = new LinkedList

我有一个深度优先搜索的迷宫生成算法。
它首先生成一个原始迷宫,如下所示:

然后使用以下算法创建迷宫:
结果如下所示:

红色像素表示目标,绿色像素表示开始。
它被卡在球门上,来回跳跃,直到它认为它完成了。
迷宫生成器源:

public Maze generate(int width, int height) {
    Maze maze = getRawMaze(width, height);

    List<Point> cellStack = new LinkedList<Point>();
    int totalAirCells = maze.getTotalAirCells();
    Point currentCell = maze.getRandomAirCell(ran);
    maze.setCellTypeAt(currentCell, Maze.CellType.START);       
    int visitedCells = 1;

    while(visitedCells<totalAirCells) {
        Point[] unvisitedNeighbors = getUnvisitedNeighbors(maze, currentCell);
        if(unvisitedNeighbors.length!=0) {
            System.out.println("Go Foward");
            Point newCell = unvisitedNeighbors[ran.nextInt(unvisitedNeighbors.length)];
            maze.connect(newCell, currentCell);
            cellStack.add(currentCell);
            currentCell = newCell;
            visitedCells++;
        }else {
            System.out.println("Go Back");
            Point recentCell = cellStack.get(cellStack.size()-1);
            cellStack.remove(cellStack.size()-1);
            currentCell = recentCell;
        }
    }

    maze.setCellTypeAt(currentCell, Maze.CellType.GOAL);

    return maze;
}

public Point[] getUnvisitedNeighbors(Maze maze, Point p) {
    List<Point> unvisitedNeighbors = new ArrayList<Point>();
    Point[] neighbors = maze.getNeighbors(p);
    for(Point n : neighbors) {
        if(maze.getSurroundingWalls(n)==4)unvisitedNeighbors.add(n);
    }
    return unvisitedNeighbors.toArray(new Point[unvisitedNeighbors.size()]);
}
public int getSurroundingWalls(Point p) {
    if(getCellTypeAt(p)==CellType.AIR) {
        int walls = 0;
        if(getCellTypeAt(new Point(p.x+1, p.y))==CellType.WALL)walls++;
        if(getCellTypeAt(new Point(p.x-1, p.y))==CellType.WALL)walls++;
        if(getCellTypeAt(new Point(p.x, p.y+1))==CellType.WALL)walls++;
        if(getCellTypeAt(new Point(p.x+1, p.y-1))==CellType.WALL)walls++;
        return walls;
    }else return -1;
}

public Point[] getNeighbors(Point p) {
    Point[] neighbors = new Point[4];
    neighbors[0] = new Point(p.x+2, p.y);
    neighbors[1] = new Point(p.x-2, p.y);
    neighbors[2] = new Point(p.x, p.y+2);
    neighbors[3] = new Point(p.x, p.y-2);
    return neighbors;
}

public void connect(Point a, Point b) {
    if((a.x==b.x-2)&&(a.y==b.y))setCellTypeAt(new Point(a.x+1, a.y), CellType.AIR);
    else if((a.x==b.x+2)&&(a.y==b.y))setCellTypeAt(new Point(a.x-1, a.y), CellType.AIR);
    else if((a.x==b.x)&&(a.y==b.y-2))setCellTypeAt(new Point(a.x, a.y+1), CellType.AIR);
    else if((a.x==b.x)&&(a.y==b.y+2))setCellTypeAt(new Point(a.x, a.y-1), CellType.AIR);
}

public Point getRandomAirCell(Random ran) {
    List<Point> airCells = new LinkedList<Point>();
    for(int x = 0; x<getWidth(); x++) {
        for(int y = 0; y<getHeight(); y++){
            Point p = new Point(x, y);
            if(getCellTypeAt(p)==Maze.CellType.AIR)airCells.add(p);
        }
    }
    return airCells.get(ran.nextInt(airCells.size()));
}

public int getTotalAirCells() {
    int airCells = 0;
    for(int x = 0; x<getWidth(); x++) {
        for(int y = 0; y<getHeight(); y++){
            if(getCellTypeAt(new Point(x, y))==Maze.CellType.AIR)airCells++;
        }
    }
    return airCells;
}
公共迷宫生成(整数宽度、整数高度){
迷宫=getRawMaze(宽度、高度);
List cellStack=newlinkedlist();
int totalAirCells=maze.getTotalAirCells();
Point currentCell=maze.getRandomAirCell(ran);
maze.setCellTypeAt(currentCell,maze.CellType.START);
int visitedCells=1;

而(visitedCellsCheck
getSurroundingWalls()
,检查的第四点是
p.x+1,p.y-1
,但我想它应该是
p.x,p.y-1
,你的问题是什么?它是否抛出错误消息?如果不是,标准输出是什么样子的?让它打印出
visitedCells
cellStack.size()的值
getSurroundingWalls()的原因是什么
功能?据我所知,你使用它只是为了检查电池周围是否有四面墙,这样你就可以知道它是否已经连接到迷宫。如果是这样,我建议你简化代码:将所有电池的类型初始化为CellType.UNUSED而不是AIR-然后你只需测试电池类型,看看它是否可以连接到t他将迷宫(然后将其类型更改为“空气”)。