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, non recursive, can be found in Greek
mythology).
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 location in the maze, see for example this:
The class
'Maze.java' (source below, at the bottom of this page) provides all
functionalities you need to generate and display the maze. It can even display
a path in the maze, if provided (that's what you have to do!), so you can
entirely concentrate on the path generation. You don't have to deal with the
graphical output or the input of the start location, everything's built into
the class 'Maze.java'. Once again, OOP shows its advantages (another advantage
is that I obviously enjoy programming such interfaces).
Maze.java API, how to use the Maze class
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 a location (cell), coordinates (row, col) (NOT x,y !). The
value encodes the presence of walls of the cell 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 (which is also always 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, that's the exit bit as well as N, W and E walls (see
explanations below how to check these bits).
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 !). If you want to display your solution, put all cells that are
part of your path as Point objects into a JAVA LinkedList (named e.g.
"myPath"), and pass its iterator ( myPath.iterator() ) to this
method. The maze will show all participating cells in red.
Method void addMazeListener(MazeListener ml)
The maze window is clickable, i.e. if you click on a cell in the maze, you can
get its coordinates (as row, column, NOT x,y). 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 cell.
Note: although the implementation of Maze is not of specific interest to this
assignment, it 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.
Steps:
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, e.g. a LinkedList.
5. call Maze's showPath(Iterator)
with an iterator to your path data-structure.
How to
check if a bit is set in a byte-type variable: the JAVA bit-AND-operator '&'
can be used if a bit is set or not. To check bit 0 in a byte-variable 'b', test
'if (b & 1){..}',
bit 1: 'if (b&2){..}', bit 2: 'if (b&4){..}',
and so on...
Good
luck. The Netbeans project shows a very simple example of how to use the Maze
and implement the MazeListener. Please have a look at the Main class.
And here's
the source:
Maze Java Code