开发者

How could I stop from printing both sides of a wall in my ascii maze?

开发者 https://www.devze.com 2022-12-08 02:02 出处:网络
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).

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)) ? "|" :开发者_StackOverflow社区 " ");
            }
            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.


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 ;-)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号