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.
Only print the NORTH and WEST walls. Code on its way…
I changed the walls to an EnumSet
So you don’t need to add any walls in your constructor:
And to remove your walls, use:
And then the print code looks like:
(Note: Aesthetically, I prefer Direction, and randomDirection. But that’s just me 😉