【问题标题】:How could I stop from printing both sides of a wall in my ascii maze?我怎么能停止在我的 ascii 迷宫中打印墙壁的两面?
【发布时间】:2009-10-15 22:44:31
【问题描述】:

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

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

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 方法,使其从第一个示例转到第二个示例?

如果有人感兴趣,我的课程的源代码是here

【问题讨论】:

    标签: java maze


    【解决方案1】:

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

    我把墙改成了 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。但这只是我 ;-)

    【讨论】:

      【解决方案2】:

      您需要执行类似“永远不要为 NORTH 或 WEST 打印墙,除非此单元位于迷宫边缘”这样的操作已经将其打印为自己的 EAST 墙。

      如果门/入口位于北墙或西墙,您可能也需要特殊情况。

      【讨论】:

      • 如果你使用这种方法,你应该只存储每个单元格的两堵墙(北墙和西墙);南墙和东墙只是相邻空间北墙和西墙的冗余副本,无论如何都会被忽略。迷宫的边界会自动被围起来。
      【解决方案3】:

      因为单元格共享墙壁,您可以忽略一半的值。如果你从最西北的牢房开始,只测试南面和东面的墙壁,你可以画出单壁迷宫。当然,迷宫的北墙和西墙必须完全封闭。

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

      【讨论】:

        猜你喜欢
        • 2012-08-02
        • 2022-10-17
        • 1970-01-01
        • 1970-01-01
        • 2020-01-22
        • 1970-01-01
        • 2020-06-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多