CPSC 170 Spring 2003
Program 3: Traversing a Maze
Due Friday, April 4

Overview

Imagine a maze represented by a two dimensional array as follows:

For example, consider the maze below:

   1 1 1 1 1 0
   0 1 0 0 1 1
   1 1 0 0 0 0
   0 1 1 1 1 1
In this maze, there is only one route to the end, marked by stars below:
   * * 1 1 1 0
   0 * 0 0 1 1
   1 * 0 0 0 0
   0 * * * * *
In general, there might be more than one route to the end, and of course there could be an arbitrary number of dead ends.

Your assignment is to write a program to traverse such a maze.

Program Structure

Most of the work will be in a Maze class, which should contain the following public methods:

You will also need a TestMaze class that will do the following:

Approach

One way to traverse a maze is using a stack. We will discuss this in more detail in class, but the strategy is simple. The program keeps a stack of cells to try next. It processes these with a loop that basically does two things:

You must write your own stack class that implements the Stack interface using a linked representation. Do this first, and test it thoroughly before you try to use it in the Maze program.

Input and Output

The first line of the input file will contain the number of rows in the maze; the second line will contain the number of columns in the maze. Subsequent lines will store the maze itself, one row per line, with no spaces between elements. You will need to set up a FileReader to read from the file, and you will probably want to create a BufferedReader from the FileReader so that you can use its readLine() method. Since readLine() returns a string, you will then need to transfer the characters in the string to the array representing the maze. Remember that both the readMaze method and the main method in the TestMaze class will need to include throws IOException in their headers. You will also need to import java.io.* in both files.

For the maze given above (which would be preceded by the values 4 and 6 on separate lines in the input file), your program might produce the following output:

Original maze
-------------
1 1 1 1 1 0 
0 1 0 0 1 1 
1 1 0 0 0 0 
0 1 1 1 1 1 

Traversed maze
-------------
* * * * * 0 
0 * 0 0 * * 
1 * 0 0 0 0 
0 * * * * * 
Note that in this case only one dead end -- the one still marked with a 1 -- was not tried before the solution was found. The actual routes that your program tries will depend on the order in which you push the options onto the stack, so your output may look different.

What to Turn In

Turn in hardcopy of Maze.java, TestMaze.java, Stack.java, and LinkedStack.java. Tar your prog3 direction and e-mail it to me with cpsc170 prog3 in the Subject line.