For this lab you need to write a program that generates random mazes using the recursive algorithm described in class. The program will use a 2D array. You will need to use recursion but the recursive algorithm is provided for you. I'm assuming you attended the class when I went over how to do this assignment.
Here again, is the general description of the algorithm. You start with a matrix M filled with 0's (wall or hedgerow). Starting with some fixed starting position y,x (such as center or corner):
Use the the numbers 0, 1, 2, 3 to represent the four different directions. Then to choose a random initial direction, just use something like
An even better way to randomize the maze is to use a random permutation:
int[] P = {0,1,2,3};
for (int i=0;i < P.length;i++)
{ int r = (int)(Math.random()*P.length);
int temp = P[i];
P[i] = P[r];
P[r] = temp;
}
Then use the array to set the directions.
Figuring out when you're going out of the bounds of the array could be tricky. The variables "mw" and "mh" represent the dimensions (width and height) of the array, so M[mh-1][mw-1] is the largest legal coordinate. That is, the y coordinate must be between 0 and mh-1, inclusive and the x coordinate must be between 0 and mw-1, inclusive.
By default, the dimensions of my maze array is 41X51 and the graphical representation uses 10x10 rectangles.
To simplify matters so you can concentrate on the problem, you've been provided with a template: maze.java. You only need to write the "digout" function at the end. Currently the function contains only some code that demonstrates the mechanics of the program. You are also encouraged to look at the class to get a sense of what's being provided for you (e.g, NORTH, SOUTH, EAST, WEST constants.)
You need to understand that the following variables are already declared in the mazegen class:
Read this again to make sure you know what to do!