Course Links

Exercises

Resources

External

Class Notes

The Maze class you will write is more complex than most of the classes we have worked with so far. Spend some time thinking about what sort of methods it could have that would help you write your final program neatly and clearly. For example, you might have methods that ask about the condition of a particular location -- is it a wall? is it open? is it unexplored? Consider functionality that will help simplify the main program you are writing. Your class will also need to define the usual methods inherited from JComponent so that it can display itself in the application window. (For purposes of comparison, the class combines the roles of MapGrid and MapView from our earlier homeworks.)

To further flesh out class Maze, you will note that MazeSolver makes several calls to its methods. In order for this to work, you must make sure those methods are defined! The first point where this occurs is inside main(), where it calls either of two Maze constructors. One takes a String argument, to be construed as the name of an input file, while the other takes no input arguments, and is expected to read from the system input. Either way, they are supposed to read the maze from the indicated source and create an appropriate structure in memory. To avoid duplicating code, I recommend that you make both constructors create an instance of BufferedReader and call a single function on it to do the actual reading in. (A BufferedReader can be created either from System.in or from a FileReader, and can be used to read the input one line at a time via its readLine method.)

The other point where MazeSolver makes calls that you must define is inside the event handlers. Two methods are referred to there, Maze.solve() and Maze.reset(). The only tricky thing here is that Maze.solve takes no arguments, whereas the recursive procedure we discussed in class includes both the maze and the current location as part of its problem description. The solution: Maze.solve() must call a recursive worker method with a MazeCoordinate as its argument. (The other portion of the state is the Maze object itself, accessible through this.

Comment

The Maze class you are to write is doing double duty as both the model and the view -- it keeps track of its own data, and also draws it in the application window. Often these two roles are kept separate, as they were in the Map assignments. In fact, the Maze class has a lot of similarity to the MapGrid and MapViewer classes from that assignment. It would have been possible to design Maze as a subclass of MapGrid, which would have allowed the existing MapViewre to display your mazes without any changes. But this would have introduced other complications, because the MapGrid class only stores colors and not any other properties like traversibility. So it is probably best to design Maze from scratch, while referring to your previous work for inspiration and guidance. This assignment is simpler than the map because you will display the entire maze at a fixed scale, with no offset.