Assignment #5: The Maze


This time we deal with an everyday problem: you have to find your way out of a maze. Luckily you learned how to think recursively, so the task is relatively simple (a slightly different solution can be found in greek mythology, see http://www.odysseyadventures.ca/articles/knossos/knossos_text.htm).
The program to generate the maze is given to you as a java class. It randomly creates a maze like this one:

Your task is to find the path to the upper left corner, the green grid, from any possible point in the maze, like this:


The class 'Maze.java' (source below, at the bottom of this page) provides all functionalities you need, so you can totally concentrate on the path generation. You don't have to deal with the graphical output or the input of the start-chamber, everything's built into the class 'Maze.java'.
Once again, OOP shows its advantages...

Maze.java API:

Constructor: Maze(int rows, int columns)
Generates a maze of size rows x columns. A typical size would be 60x80. Bigger values tend to be critical due to stack overflow. The constructor also DISPLAYS the maze in its own window. A button is implemented to change the maze's topology.

Method Byte getMazeData(int row, int col)
returns the value of the chamber, coordinates (row, col) (NOT x,y !). The value encodes the presence of walls of the chamber in the lower 4 bits:
bit 0 set: north wall present
bit 1 set: west wall present
bit 2 set: south wall present
bit 3 set: east wall present

bit 5 (not 4) is used to mark the exit, the upper left corner. if you want to check if you found the exit, you can either check for this bit or for row/column 0/0.
Example: a chamber having walls north and west would return the value 3, for '000000011'. The top left chamber (the exit chamber) in image 2 has the value '00101011' = 43.

public void showPath(Iterator path)
used to display the computed path, shown as red rectangles, see image 2. A path is passed to the method by an iterator. The elements in the iterated structure must be of type java.awt.Point. This is a class that contains only two fields, x and y. Please put the row into X, the column into Y (this is unconventional. read it again !).

Method void addMazeListener(MazeListener ml)
The maze window is clickable, i.e. if you click on a chamber in the maze, you can get the coordinates of the clicked chamber. In order to do so, you have to implement the Interface 'MazeListener' in your program (the interface is explained below). As soon as you register your class to the Maze, using addMazeListener, it is able to receive the coordinates of the clicked chamber.
Note: although the implementation of Maze is not of specific interest to this assignment, i should be mentioned that the graphical output of the maze does not consist of a grid of JButtons. This would be a GUI overkill.

Interface MazeListener
The interface has only a single method:
public void MazeClicked(int row, int col)
This method is called as soon as a chamber in the maze is clicked. row and col are the coordinates of the chamber.


With all this information given, here's your TASK:
Write a java class MazeGenerator, that displays a maze and shows the path from each clicked chamber to the exit.
How to:
  1. create your class, with main etc...
  2. your class must instantiate an object of class Maze.
  3. implement the interface MazeListener, the call to your iterative path finder will be put into its method MazeClicked, using the arguments row, col as the starting chamber.
  4. find the path recursively. store each chamber of the path in an appropriate structure.
  5. call Maze's showPath(Iterator) with an iterator to your path data-structure.

Good luck. I will explain more in class !
And here's the source:
Maze Java Code