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. A recursive algorithm is provided for you. Study the algorithm first. I'm assuming you attended the class when I went over how to do this assignment, but briefly, you will inherit a class that represents the maze with a 2D array int[][] M, of dimensions mheight rows by mwidth columns. You will override the digout function of the superclass using a recursive algorithm. The values inside each M[y][x] is initially zero, which graphically is represented by a green square. "Digging out" a maze location y,x means setting M[y][x]=1, AND calling drawblock(y,x); which changes the graphical representation to a black square. Because we tend to think of array coordinates in "row-major order", it is helpful to give the y (row) coordinate before the x (column) coordinate.
Here is the general description of the algorithm. Starting with some fixed starting position y,x (determined outside of digout)
A better way to randomize the maze is to use a random permutation:
int[] P = {0,1,2,3}; // initial identity permutation for (int i=0;i < P.length-1;i++) { int r = i+(int)(Math.random()*(P.length-i));//r is between i and P.length-1 int temp = P[i]; // swap each element with some random element. 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 "mwidth" and "mheight" represent the dimensions (width and height) of the array, so M[mheight-1][mwidth-1] is the largest legal coordinate. That is, the y coordinate must be between 0 and mheight-1, inclusive and the x coordinate must be between 0 and mwidth-1, inclusive.
By default, the dimensions of my maze array are 41X41 (mheight,mwidth) and the graphical representation uses 20x20 rectangles (bh,bw). But your program should work even if these defaults change (they can be changed in the @Override customize() function). Choosing odd values for mheight,mwidth will result in a maze with a green border.
To simplify matters so you can concentrate on the problem, you've been provided with a superclass that already contains a lot of code: mazebase.java. Javadoc documentation for this class is available at mazebase.html. You only need to write a subclass that *overrides* (redefines) the "digout" function. Use this template. You don't need to change the name of the file or public class. Currently the function contains only some code that demonstrates the mechanics of the program. You are also encouraged to look at documentation of the base class to see what's provided for you (though this is more important for the followup lab).
The following variables are already declared in the mazebase superclass:
@Override public void customize() { bh*=2; // 4x size of graphical squares bw*=2; mwidth = 31; mheight = 31; // change dimensions of maze (keep'em odd) wallcolor = java.awt.Color.yellow; // change maze color to yellow }
Challenge: Rewrite the digout function without using recursion. You will need to use a stack data structure. Refer to the triangles program as an example. However, this program is harder to do without recursion because the recursive calls can interfere with eachother. That is, making three consecutive recursive calls is not exactly the same as pushing three frames on a stack. Before making a recursive call, you need to check that the y,x coordinate to dig out has not been dug out already. With recursion, the second (and third) recursive call is not made until the first one (and second) completely returns, so the check is made before each recursive call. If you just push three frames on the stack, the check is not made for each simulated recursion: by the time the second (and third) frame is popped the maze may have already been changed. Therefore when you pop a frame off of the stack you must check again if the coordinate has already been dug out. Furthermore, you can only digout the intermediate square once you have rechecked the coordinate.