Lab 8: Maze Solver

In this lab you will design a Java applet to navigate a maze. We model the maze as a grid of squares. The grid has r rows and c columns. The maze in the picure below has r=10 rows and c=10 columns.

The white squares are squares you may occupy or move to. The black squares are walls or obstacles. The green square is the start of the maze, and the red square is the end of the maze.

In Java we represent the maze as a two-dimensional array of integers:

int[][] maze = new int[10][10];

The colored squares of the pictured maze correspond to the following integer values in the maze array:

maze[r][c] = -1 black square
maze[r][c] = 0 white square
maze[r][c] = 1 green square (START)
maze[r][c] = 2 red square (END)

The START square is at array position maze[1][1]. The END square is at position maze[8][8].

We can move in at most four possible directions from any occupied square -- north, south, east or west. If we are currently in position maze[r][c], then the following array positions correspond to the possible directions of movement:

maze[r-1][c] North
maze[r+1][c] South
maze[r][c-1] West
maze[r][c+1] East

Here is an example of a fully functional maze applet:

Clicking the Solve button will compute the path through the maze from start to finish. The blue squares represent this path. The gray squares represent "dead-end" paths that your algorithm computed. Depending on the order in which you choose directions, your implementation may visit different dead-end paths than the one above. The output area shows the squares visited by the algorithm and the final path through the maze.

You will be given an almost complete version of the Maze.java code for the above applet. You need to fill in the code for the subroutine to solve the maze:

public void SolveMaze( int r, int c, int depth ) { showPosition( r, c, depth ); /* ************************************ */ /* ************************************ */ /* Add your code here */ /* ************************************ */ /* ************************************ */ }

The first line of code (showPosition( r, c, depth );) will generate the recursion trace in the output text area so you can see what your subroutine is doing. You should leave that line of code where it is and put all your code below as indicated.

You don't need to change any other code in the program!

Preliminaries

Create a subdirectory of your public_html/cps001 directory to store your applet files for this lab. Access the P: drive from My Computer on your desktop. Open the folder for your public_html directory. Open the folder for the cps001 directory you just made. Create a new folder titled Lab8.

Create an HTML document for your applet. Start GNU Emacs. Go to File->Open and open a new file in your Lab8 directory named Maze.html. Type in the complete HTML page for your applet. In the applet tag set width = 350 and height = 600.

Save your Maze.html file.

Download or copy the file Maze.java to your Lab8 directory.

Open Maze.java in Emacs.

Implemetation

From your prelab you should see that navigating a maze from start to end can be solved recursively. Before writing any code, think about the following:

Details and hints

Compile and test

Compile your code and check out your applet:

Add a link

Add a link to your Maze applet on your CPS1 page (cps001.html).