为什么我的随机迷宫生成算法在迷宫的底部创建了一个列模式?

时间:2015-12-02 05:29:39

标签: java algorithm recursion maze

所以我正在使用深度优先搜索制作迷宫生成算法。我在2D字符数组中这样做。墙壁表示为'#' &安培;路径是'。'每当我打电话给#34; Maze"我就会创造一个新的迷宫。类。

我使用三种方法做到这一点:

1 - hasUnvisited:检查一个单元格周围是否有任何未被算法访问过的单元格。仅向上或向下或向左或向右检查单元+2个单元。 (UDLR)

2 - pather:通过仅由墙壁制成的网格划出一条路径。检查路径是否位于网格边缘。然后将该单元格转换为路径。然后检查单元格是否已访问。如果是,它会选择随机方向(UDLR)。如果该方向+ 2' s(例如:左+ 2,右+ 2 ......等)单元格已清除,则它将方向的+1改变为墙,然后调用方向+2 。 (这将在随机选择的方向上清除+2并递归重复,直到路径没有未访问。

3 - mazer:此方法仅用于美学目的,所以我已经注释了很多内容,专注于潜在的问题。它基本上是一个网格,用所有#'初始化它。然后用起始行(sr)和起始列(sc)1,1调用pather。然后返回字符网格。

然而,出于某种原因,我的代码使这些奇怪的路径成为了#39;列。在我每次跑步时都在迷宫的底部。我99%肯定它是在"剪辑" “' pather”中的代码部分方法,但我不知道如何结束该方法并阻止它在那时超出界限。

正如您所看到的,这是Java,但我已经尝试了C ++中的代码并且它做了同样的事情,因此我非常确定这是与语言无关的。

以下是代码:

import java.util.Random;

public class Maze {

private char[][] grid;
private final int WIDTH;
private final int HEIGHT;
Random randomGen = new Random();

//Checks to see if any of the surrounding cells are un
private boolean hasUnvisited (char[][] grid, int sr, int sc) {
    if (sc+2 > HEIGHT-1) {
    } else if (grid[sr][sc+2]=='#') {
        return true;
    }
    if (sc-2 < 0) {
    } else if (grid[sr][sc-2]=='#') {
        return true;
    }
    if (sr+2 > WIDTH-1) {
    } else if (grid[sr+2][sc]=='#') {
        return true;
    }
    if (sr-2 < 0) {
    } else if (grid[sr-2][sc]=='#') {
        return true;
    }
    return false;
}

//Visits each cell, turns it to '.'
private void pather (char[][] grid, int sr, int sc) {
    //Sets current cell to '.' to mark as visited
    grid[sr][sc] = '.';

    //"Clipping": if it is at edge of grid, don't carve any more, just return.
    if (sr>WIDTH-2||sr<1||sc>HEIGHT-2||sc<1) {
        return;
    }

    //Gets a number between 0-3
    switch (randomGen.nextInt(4)) {
    case 0:
        if(hasUnvisited(grid,sr,sc)) {
            if(sc+2>HEIGHT-1) {
            }else if(grid[sr][sc+2]!='.'){
                grid[sr][sc+1]='.';
                pather(grid,sr,sc+2);
            }
            pather(grid,sr,sc);
        }
        break;
    case 1:
        if(hasUnvisited(grid,sr,sc)) {
            if(sc-2<0) {
            } else if(grid[sr][sc-2]!='.'){
                grid[sr][sc-1]='.';
                pather(grid,sr,sc-2);
            }
            pather(grid,sr,sc);
        }
        break;
    case 2:
        if(hasUnvisited(grid,sr,sc)) {
            if(sr+2>WIDTH-1) {
            }else if(grid[sr+2][sc]!='.'){
                grid[sr+1][sc]='.';
                pather(grid,sr+2,sc);
            }
            pather(grid,sr,sc);
        }
        break;

    case 3:
        if(hasUnvisited(grid,sr,sc)) {
            if(sr-2<0) {
            } else if(grid[sr-2][sc]!='.') {
                grid[sr-1][sc]='.';
                pather(grid,sr-2,sc);
            }
            pather(grid,sr,sc);
        }
        break;
    }
}

//Returns a complete maze, gets the carved out paths from the pather function,
//then 'cleans it up' to return a useable maze format for the game.

private char[][] mazer() {
    grid = new char[WIDTH][HEIGHT];
    //Initialize Grid with all walls
    for (int i=0;i<WIDTH;i++)
    {
        for (int j=0;j<HEIGHT;j++)
        {
            grid[i][j]= '#';
        }
    }
    //Starting from row and column 1 and 1, respectively.
    int sr=1,sc=1;
    //Carve Out the Grid
    pather(grid,sr,sc);

/*
    //Draw Vertical Surrounding Walls
    for (int j=0;j<HEIGHT;j++)
    {
        grid [0][j]= '#';
        grid [WIDTH-1][j]= '#';
    }
    //Draw Horizontal Surrounding Walls
    for (int j=0;j<WIDTH;j++)
    {
        grid [j][0]= '#';
        grid [j][HEIGHT-1]= '#';
    }
*/

    //JUST FOR DEBUGGING:
    for (int i=0;i<HEIGHT;i++)
    {
        for (int j=0;j<WIDTH;j++)
        {
            System.out.print(grid[j][i]);
        }
        System.out.println("");
    }
    //JUST FOR DEBUGGING ERASE IMMEDIATELY AFTER DONE WITH
    return grid;
}

public Maze (int wIn, int hIn) {
    WIDTH = wIn;
    HEIGHT = hIn;
    grid = mazer();

}

//After Debugging the maze: 
public static void main(String[] args) {
    Maze maze = new Maze(15,10);
   }
}

1 个答案:

答案 0 :(得分:2)

我修复了你的解决方案,问题在于检查角落情况。

主要变化:

sc+2 > HEIGHT-1 => sc+2 > HEIGHT-2
sr+2 > WIDTH-1 => sr+2 > WIDTH-2

更新的代码:

import java.util.Random;

public class Maze {

    private char[][] grid;
    private final int WIDTH;
    private final int HEIGHT;
    Random randomGen = new Random();

    //Checks to see if any of the surrounding cells are un
    private boolean hasUnvisited (char[][] grid, int sr, int sc) {
        if (sc+2 > HEIGHT-2) {
        } else if (grid[sr][sc+2]=='#') {
            return true;
        }
        if (sc-2 < 0) {
        } else if (grid[sr][sc-2]=='#') {
            return true;
        }
        if (sr+2 > WIDTH-2) {
        } else if (grid[sr+2][sc]=='#') {
            return true;
        }
        if (sr-2 < 0) {
        } else if (grid[sr-2][sc]=='#') {
            return true;
        }
        return false;
    }

    //Visits each cell, turns it to '.'
    private void pather (char[][] grid, int sr, int sc) {
        //Sets current cell to '.' to mark as visited
        grid[sr][sc] = '.';

        //"Clipping": if it is at edge of grid, don't carve any more, just return.
        if (sr>WIDTH-2||sr<1||sc>HEIGHT-2||sc<1) {
            return;
        }

        //Gets a number between 0-3
        switch (randomGen.nextInt(4)) {
            case 0:
                if(hasUnvisited(grid,sr,sc)) {
                    if(sc+2>HEIGHT-2) {
                    }else if(grid[sr][sc+2]!='.'){
                        grid[sr][sc+1]='.';
                        pather(grid,sr,sc+2);
                    }
                    pather(grid,sr,sc);
                }
                break;
            case 1:
                if(hasUnvisited(grid,sr,sc)) {
                    if(sc-2<0) {
                    } else if(grid[sr][sc-2]!='.'){
                        grid[sr][sc-1]='.';
                        pather(grid,sr,sc-2);
                    }
                    pather(grid,sr,sc);
                }
                break;
            case 2:
                if(hasUnvisited(grid,sr,sc)) {
                    if(sr+2>WIDTH-2) {
                    }else if(grid[sr+2][sc]!='.'){
                        grid[sr+1][sc]='.';
                        pather(grid,sr+2,sc);
                    }
                    pather(grid,sr,sc);
                }
                break;

            case 3:
                if(hasUnvisited(grid,sr,sc)) {
                    if(sr-2<0) {
                    } else if(grid[sr-2][sc]!='.') {
                        grid[sr-1][sc]='.';
                        pather(grid,sr-2,sc);
                    }
                    pather(grid,sr,sc);
                }
                break;
        }
    }

//Returns a complete maze, gets the carved out paths from the pather function,
//then 'cleans it up' to return a useable maze format for the game.

    private char[][] mazer() {
        grid = new char[WIDTH][HEIGHT];
        //Initialize Grid with all walls
        for (int i=0;i<WIDTH;i++)
        {
            for (int j=0;j<HEIGHT;j++)
            {
                grid[i][j]= '#';
            }
        }
        //Starting from row and column 1 and 1, respectively.
        int sr=1,sc=1;
        //Carve Out the Grid
        pather(grid,sr,sc);

/*
    //Draw Vertical Surrounding Walls
    for (int j=0;j<HEIGHT;j++)
    {
        grid [0][j]= '#';
        grid [WIDTH-1][j]= '#';
    }
    //Draw Horizontal Surrounding Walls
    for (int j=0;j<WIDTH;j++)
    {
        grid [j][0]= '#';
        grid [j][HEIGHT-1]= '#';
    }
*/

        //JUST FOR DEBUGGING:
        for (int i=0;i<HEIGHT;i++)
        {
            for (int j=0;j<WIDTH;j++)
            {
                System.out.print(grid[j][i]);
            }
            System.out.println("");
        }
        //JUST FOR DEBUGGING ERASE IMMEDIATELY AFTER DONE WITH
        return grid;
    }

    public Maze (int wIn, int hIn) {
        WIDTH = wIn;
        HEIGHT = hIn;
        grid = mazer();

    }

    //After Debugging the maze:
    public static void main(String[] args) {
        Maze maze = new Maze(17,17);
    }
}

OUTPUT(17 * 17):

#################
#.#.............#
#.#######.#####.#
#.......#...#...#
#######.#.#.#####
#.#.....#.#.#...#
#.#.#####.#.#.#.#
#...#.....#...#.#
#.#######.#####.#
#.......#.....#.#
#######.#######.#
#.......#.....#.#
#.#######.###.#.#
#.#.......#.#.#.#
#.#######.#.#.#.#
#.........#.....#
#################

但是当涉及到宽度和高度均匀时,它输出有点奇怪(但无论如何都是正确的)因为移动是+2

OUTPUT(18 * 18):

################
#.......#.....##
#######.###.#.##
#.#.....#...#.##
#.#.#####.###.##
#.#.#.......#.##
#.#.#######.#.##
#...#.....#.#.##
#.###.###.#.#.##
#...#.#.#.#.#.##
###.#.#.#.#.#.##
#.#.#.#...#.#.##
#.#.#.#.###.#.##
#.....#.....#.##
################
################

我的建议是尝试重构您的解决方案,通过一步移动生成路径。您的代码永远不会生成类似

的路径
#####
#.#.#
#...#
#####

因为它通过两步移动来走地图。