CISC 3115
Introduction to Modern Programming Techniques
Lab #5
Recursion — Maze Solving

How to Develop and Submit your Labs

This Lab is purely optional (though I may ask extra-credit questions about it on the final). In addition, it is quite easy to see when you got it right. As such, there is no CodeLab component … just you and your IDE.

Lab 5 — Maze Solving

Overview

The ultimate objective of this lab is to solve a maze:
..........
.S****** .
.   *  * . 
.  **  * . 
.   *  F . 
. ***    .   
..........
i.e., state whether the maze has a solution (true in the above case), and furthermore, display the solution path through the maze:
..........
.SOOOOOO .
.   *  O .
.  **  O .
.   *  F .
. ***    .
..........
We would also like — for debugging purposes as well as general curiosity — like to follow the progress of the maze-solving algorithm:
..........	..........	..........	..........	..........	..........	..........
.S****** .	.O****** .	.SO***** .	.S*O**** .	.S**O*** .	.S****** .	.S****** .
.   *  * .	.   *  * .	.   *  * .	.   *  * .	.   *  * .	.   O  * .	.   *  * .
.  **  * . 	.  **  * .	.  **  * .	.  **  * .	.  **  * .	.  **  * .	.  *O  * .
.   *  F .	.   *  F .	.   *  F .	.   *  F .	.   *  F .	.   *  F .	.   *  F .
. ***    .	. ***    .	. ***    .	. ***    .	. ***    .	. ***    .	. ***    .
..........	..........	..........	..........	..........	..........	..........

..........	..........	..........	..........	..........	..........	..........
.S****** .	.S****** .	.S****** .	.S****** .	.S****** .	.S***O** .	.S****O* .
.   *  * .	.   *  * .	.   *  * .	.   *  * .	.   *  * .	.   *  * .	.   *  * .
.  **  * .	.  **  * .	.  **  * .	.  **  * .	.  O*  * .	.  **  * .	.  **  * .
.   O  F .	.   *  F .	.   *  F .	.   *  F .	.   *  F .	.   *  F .	.   *  F .
. ***    .	. **O    .	. *O*    .	. O**    .	. ***    .	. ***    .	. ***    .
..........	..........	..........	..........	..........	..........	..........

..........	..........	..........	..........	
.S*****O .	.S****** .	.S****** .	.S****** .	
.   *  * .	.   *  O .	.   *  * .	.   *  * .	
.  **  * .	.  **  * .	.  **  O .	.  **  * .	
.   *  F .	.   *  F .	.   *  F .	.   *  O .	
. ***    .	. ***    .	. ***    .	. ***    .	
..........	..........	..........	..........	
We will break down this task into several preliminary subtasks leading up to the actual solving of the maze and then presenting the solution path. But first we will introduce two-dimensional arrays, which we will use to represent the maze itself.

Two-Dimensional Arrays

The most straightforward way for us to represent the maze (especially within the context of 3115 and recursion) is to use a two-dimensional array. While you may or may not have covered this in 1115, here is a brief presentation if the relevant material:

Declaring and Creating

Accessing Individual Elements

Traversing

And that's about all you need for this lab. I've also provided App classes for the various labs containing code that works with the maze array. Finally, you can get some practice with 2-D arrays in CodeLab at Optional/CISC 115 Topics Review/ARRAYS/Two-Dim

Lab 5.1 — Basic Maze Class: Representing, Reading, toString

The Maze representation

Here is a visual representation of a maze, both as it would appear in the input file as well as when output:
..........
.S****** .
.   *  * .
.  **  * .
.   *  F .
. ***    .
..........
Here is a table of the various characters used in the representation:
. border
(blank) wall
* path
S start
F finish

The Maze Class

Create a class named Maze that has the following instance variables:
Notes

Developing and Testing

Lab 5.2 — Solving the Maze

In this step we will simply test and report whether the maze has a solution, i.e., there is a path from the start location to the finish location.

An Explanation of the Algorithm

We will refer to locations as (row, col). Here is an overview of the recursive definition of how to solve a maze:

Some more details

Lab 5.3 — Constructing and Displaying the Solution Path

Here is a solution path for the above example (the O's represents the path of the solution): ............ .S** * . .O * . .O * OOOF. .OO** O . . O O * . . OOOOOO * . . * *** . . * * . . *** *** . . * * . ............ Recall that when we reach the finish location, we return true. That location is the last one on the path. We then unwind the recursion passing true up the chain of recursive calls. That means that each of those recursive calls is on the path that leads from Start to Finish.