我怎样才能停止在我的 ascii 迷宫中打印墙壁的两面?

发布于 2024-08-08 03:25:40 字数 3203 浏览 9 评论 0原文

我已经编写了一些为我生成迷宫的代码。迷宫由 (nxn) 个单元格组成,每个单元格都有一个布尔值来代表一面墙(北、南、东、西)。

它工作正常,我编写了下面的函数来打印迷宫:

public static void printMaze(Cell[][] maze)
    {
        for(int i = 0; i < maze.length; i++)
        {
            for(int j = 0; j < maze[i].length; j++)
            {
                System.out.print((maze[i][j].walls.get(Dir.NORTH)) ? "+--+" : "+  +"); 
            }
            System.out.println();
            for(int j = 0; j < maze[i].length; j++)
            {
                System.out.print((maze[i][j].walls.get(Dir.WEST)) ? "|" : " ");
                System.out.print("  ");
                System.out.print((maze[i][j].walls.get(Dir.EAST)) ? "|" : " ");
            }
            System.out.println();
            for(int j = 0; j < maze[i].length; j++)
            {
                System.out.print((maze[i][j].walls.get(Dir.SOUTH)) ? "+--+" : "+  +");
            }
            System.out.println();
        }
    }

但是,因为单元格共享墙壁,所以我在打印函数中产生了一种双墙走廊的外观:

+--++--++--++--++--++--++--++--++--++--+
|      ||                  ||          |
+--++  ++--++--++  ++--++--++  ++  ++--+
+--++  ++--++--++  ++--++--++  ++  ++--+
|  ||          ||  ||          ||      |
+  ++--++--++  ++  ++  ++--++--++--++  +
+  ++--++--++  ++  ++  ++--++--++--++  +
|      ||      ||  ||  ||      ||  ||  |
+  ++  ++  ++--++  ++  ++  ++  ++  ++  +
+  ++  ++  ++--++  ++  ++  ++  ++  ++  +
|  ||  ||  ||  ||          ||      ||  |
+  ++  ++  ++  ++  ++--++--++--++--++  +
+  ++  ++  ++  ++  ++--++--++--++--++  +
|  ||      ||          ||          ||  |
+  ++--++--++--++--++--++  ++--++  ++  +
+  ++--++--++--++--++--++  ++--++  ++  +
|  ||          ||          ||      ||  |
+  ++--++  ++  ++  ++--++--++  ++--++  +
+  ++--++  ++  ++  ++--++--++  ++--++  +
|          ||  ||  ||      ||  ||      |
+--++--++--++  ++  ++  ++  ++  ++  ++  +
+--++--++--++  ++  ++  ++  ++  ++  ++  +
|          ||  ||  ||  ||  ||      ||  |
+  ++  ++--++  ++  ++  ++  ++--++--++  +
+  ++  ++--++  ++  ++  ++  ++--++--++  +
|  ||  ||      ||      ||  ||  ||      |
+  ++  ++  ++--++--++--++  ++  ++  ++--+
+  ++  ++  ++--++--++--++  ++  ++  ++--+
|  ||                      ||          |
+--++--++--++--++--++--++--++--++--++--+

我应该如何修改我的打印函数,使其看起来像:

+--+--+--+--+--+--+--+--+--+--+
|     |              |        |
+--+  +--+--+  +--+--+  +  +--+
|  |        |  |        |     |
+  +--+--+  +  +  +--+--+--+  +
|     |     |  |  |     |  |  |
+  +  +  +--+  +  +  +  +  +  +
|  |  |  |  |        |     |  |
+  +  +  +  +  +--+--+--+--+  +
|  |     |        |        |  |
+  +--+--+--+--+--+  +--+  +  +
|  |        |        |     |  |
+  +--+  +  +  +--+--+  +--+  +
|        |  |  |     |  |     |
+--+--+--+  +  +  +  +  +  +  +
|        |  |  |  |  |     |  |
+  +  +--+  +  +  +  +--+--+  +
|  |  |     |     |  |  |     |
+  +  +  +--+--+--+  +  +  +--+
|  |                 |        |
+--+--+--+--+--+--+--+--+--+--+

我担心当我最终开始使用实际图形而不是 ASCII 来绘制迷宫时,我会遇到类似的问题。

如何修改 printMaze 方法,使其从第一个示例转到第二个示例?

如果有人感兴趣,我的类用于生成这些的源代码位于此处

I've written some code that generates mazes for me. The maze consists of (n x n) cells, each cell has a boolean value to represent a wall (north, south, east west).

It is working fine, and I wrote the function below to print out the maze:

public static void printMaze(Cell[][] maze)
    {
        for(int i = 0; i < maze.length; i++)
        {
            for(int j = 0; j < maze[i].length; j++)
            {
                System.out.print((maze[i][j].walls.get(Dir.NORTH)) ? "+--+" : "+  +"); 
            }
            System.out.println();
            for(int j = 0; j < maze[i].length; j++)
            {
                System.out.print((maze[i][j].walls.get(Dir.WEST)) ? "|" : " ");
                System.out.print("  ");
                System.out.print((maze[i][j].walls.get(Dir.EAST)) ? "|" : " ");
            }
            System.out.println();
            for(int j = 0; j < maze[i].length; j++)
            {
                System.out.print((maze[i][j].walls.get(Dir.SOUTH)) ? "+--+" : "+  +");
            }
            System.out.println();
        }
    }

However, because cells share walls I produce a sort of a double-wall corridor look in my print function:

+--++--++--++--++--++--++--++--++--++--+
|      ||                  ||          |
+--++  ++--++--++  ++--++--++  ++  ++--+
+--++  ++--++--++  ++--++--++  ++  ++--+
|  ||          ||  ||          ||      |
+  ++--++--++  ++  ++  ++--++--++--++  +
+  ++--++--++  ++  ++  ++--++--++--++  +
|      ||      ||  ||  ||      ||  ||  |
+  ++  ++  ++--++  ++  ++  ++  ++  ++  +
+  ++  ++  ++--++  ++  ++  ++  ++  ++  +
|  ||  ||  ||  ||          ||      ||  |
+  ++  ++  ++  ++  ++--++--++--++--++  +
+  ++  ++  ++  ++  ++--++--++--++--++  +
|  ||      ||          ||          ||  |
+  ++--++--++--++--++--++  ++--++  ++  +
+  ++--++--++--++--++--++  ++--++  ++  +
|  ||          ||          ||      ||  |
+  ++--++  ++  ++  ++--++--++  ++--++  +
+  ++--++  ++  ++  ++--++--++  ++--++  +
|          ||  ||  ||      ||  ||      |
+--++--++--++  ++  ++  ++  ++  ++  ++  +
+--++--++--++  ++  ++  ++  ++  ++  ++  +
|          ||  ||  ||  ||  ||      ||  |
+  ++  ++--++  ++  ++  ++  ++--++--++  +
+  ++  ++--++  ++  ++  ++  ++--++--++  +
|  ||  ||      ||      ||  ||  ||      |
+  ++  ++  ++--++--++--++  ++  ++  ++--+
+  ++  ++  ++--++--++--++  ++  ++  ++--+
|  ||                      ||          |
+--++--++--++--++--++--++--++--++--++--+

How should I modify my print function so it looks like:

+--+--+--+--+--+--+--+--+--+--+
|     |              |        |
+--+  +--+--+  +--+--+  +  +--+
|  |        |  |        |     |
+  +--+--+  +  +  +--+--+--+  +
|     |     |  |  |     |  |  |
+  +  +  +--+  +  +  +  +  +  +
|  |  |  |  |        |     |  |
+  +  +  +  +  +--+--+--+--+  +
|  |     |        |        |  |
+  +--+--+--+--+--+  +--+  +  +
|  |        |        |     |  |
+  +--+  +  +  +--+--+  +--+  +
|        |  |  |     |  |     |
+--+--+--+  +  +  +  +  +  +  +
|        |  |  |  |  |     |  |
+  +  +--+  +  +  +  +--+--+  +
|  |  |     |     |  |  |     |
+  +  +  +--+--+--+  +  +  +--+
|  |                 |        |
+--+--+--+--+--+--+--+--+--+--+

I fear I'm going to face a similar problem when I eventually get to the point I start drawing my maze using actual graphics rather than ascii as well.

How do I modify my printMaze method so it goes from the first example to the second one?

In case anyone is interested the source code to my class for generating these is here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半窗疏影 2024-08-15 03:25:40

仅打印北墙和西墙。代码正在编写中...

我将墙壁更改为 EnumSet

public Set<Dir> walls = EnumSet.allOf(Dir.class);

因此,您不需要在构造函数中添加任何墙壁:

public Cell(final int x, final int y) {
    this.x = x;
    this.y = y;
    this.Visited = false;
}

要删除墙壁,请使用:

this.walls.remove(randDir);
randomNeighbor.walls.remove(randDir.opposite());

然后打印代码如下所示:(

public static void printMaze(final Cell[][] maze) {
    for (int r = 0; r < maze.length; r++) {
        final Cell[] row = maze[r];
        printTop(row);
        printMiddle(row);
        if (r == maze.length - 1) {
            printBottom(row);
        }
    }
}

private static void printBottom(final Cell[] row) {
    for (final Cell cell : row) {
        System.out.print(cell.walls.contains(Dir.SOUTH) ? "+--" : "+  ");
    }
    System.out.println("+");
}

private static void printMiddle(final Cell[] row) {
    for (int c = 0; c < row.length; c++) {
        final Cell cell = row[c];
        System.out.print(cell.walls.contains(Dir.WEST) ? "|  " : "   ");
        if (c == row.length - 1) {
            System.out.println(cell.walls.contains(Dir.EAST) ? "|" : " ");
        }
    }
}

private static void printTop(final Cell[] row) {
    for (final Cell cell : row) {
        System.out.print(cell.walls.contains(Dir.NORTH) ? "+--" : "+  ");
    }
    System.out.println("+");
}

注意:从美学角度来看,我更喜欢 Direction 和 randomDirection 但这只是我的想法;-)

Only print the NORTH and WEST walls. Code on its way...

I changed the walls to an EnumSet

public Set<Dir> walls = EnumSet.allOf(Dir.class);

So you don't need to add any walls in your constructor:

public Cell(final int x, final int y) {
    this.x = x;
    this.y = y;
    this.Visited = false;
}

And to remove your walls, use:

this.walls.remove(randDir);
randomNeighbor.walls.remove(randDir.opposite());

And then the print code looks like:

public static void printMaze(final Cell[][] maze) {
    for (int r = 0; r < maze.length; r++) {
        final Cell[] row = maze[r];
        printTop(row);
        printMiddle(row);
        if (r == maze.length - 1) {
            printBottom(row);
        }
    }
}

private static void printBottom(final Cell[] row) {
    for (final Cell cell : row) {
        System.out.print(cell.walls.contains(Dir.SOUTH) ? "+--" : "+  ");
    }
    System.out.println("+");
}

private static void printMiddle(final Cell[] row) {
    for (int c = 0; c < row.length; c++) {
        final Cell cell = row[c];
        System.out.print(cell.walls.contains(Dir.WEST) ? "|  " : "   ");
        if (c == row.length - 1) {
            System.out.println(cell.walls.contains(Dir.EAST) ? "|" : " ");
        }
    }
}

private static void printTop(final Cell[] row) {
    for (final Cell cell : row) {
        System.out.print(cell.walls.contains(Dir.NORTH) ? "+--" : "+  ");
    }
    System.out.println("+");
}

(Note: Aesthetically, I prefer Direction, and randomDirection. But that's just me ;-)

长途伴 2024-08-15 03:25:40

你需要做一些事情,比如“永远不要打印北或西的墙,除非这个单元格位于迷宫的边缘”这样,如果这个单元格的西边应该有一堵墙,那么西边的单元格就已经打印了它作为自己的东墙。

如果门/入口也位于北墙或西墙,您可能需要对其进行特殊处理。

You need to do something like "never print walls for NORTH or WEST unless this cell is on the edge of the maze" That way if there's supposed to be a wall on the WEST for this cell, the cell to the west will already have printed it as its own EAST wall.

You may have to special-case doors/entries if they're on the north or west walls, too.

撩动你心 2024-08-15 03:25:40

由于单元格共享墙壁,因此您可以忽略一半的值。如果您从最西北的单元开始,仅测试南部和东部的墙壁,您可以绘制单墙迷宫。当然,迷宫的北墙和西墙必须完全封闭。

免责声明:我并没有真正考虑过这一点,所以它可能根本不起作用,但对我来说听起来很合理。

Because the cells share walls, you could just ignore half of the values. If you start at the far north west cell and only test for walls to the south and east, you could draw the single-walled maze. The north and west walls of the maze would have to be entirely closed, of course.

Disclaimer: I didn't really think this through, so it might not work at all, but it sounds reasonable to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文