Computer Science 210 Lab 7: Stacks and Backtracking
Due: Nov. 9

Objectives and Overview: For better or worse, one of the most common tasks for computer scientists is to modify someone else's code (this is essentially the only thing anyone at Microsoft does). In this lab you will get a fairly large chunk of running code, and your job will be to make a substantial improvement in that code. In order to do so, it will be necessary for you to understand. The main improvment to the code will be to make use of a stack in order to enhance performance.

The task: The task in this case is running a maze. A running project called "Rat" has been put into the course material folder. In this project you can create or use existing mazes to see if a mouse can successfully navigate them. As it stands now the mouse is pretty dumb. If it takes a path and runs into a dead end it is essentially stuck. Your job will be to enable the mouse to backtrack over where it has been in order to let it find a new potential path.

Backtracking: A common use for a stack is backtracking. The idea is simple: the stack keeps track of everything that you have done. If you get stuck somewhere you can pop things you have done off of the stack. E.g. if a mouse makes six moves but ends up in a corner, the mouse may be able to go back three moves and find a different choice of a move. The only tricky part of backtracking is figuring out how not to make the same move twice. In a real maze you might do this by leaving a trail of bread crumbs, or some string. In our program you can do this by directly altering the maze itself.

The assignment: You need to upgrade the program to include backtracking. Among other things, the animation should reflect the fact that some squares are dead ends (they should appear in red). The appropriate place for backtracking is commented in the code.

How to proceed: Your first goal must be to understand the program as it currently is implemented. The program does two important things: 1) It contains an algorithm to ge the mouse through the maze. 2) It contains an animation suite in order to visualize the algorithm. The bulk of the maze running algorithm can be found in the FindPath method, while the important animation information can be found in the Paint method.

Animation: The animation routines use a series of images read in from files. Essentially all they do is paint those images in the appropriate places based upon some of the variables set by the path finding routines. The critical variables for animation are board[][], mouseDir[][], and mouseGifPos. Your first job should be to figure out where these get set and what they represent. Along the way I encourage you to try and understand the individual parts of the animation. Probably the best way to do this is just to play around with the code. For example, you might try switching some images around, or moving the location of where text is displayed.

Path Finding: Your next job will be to understand the path finding algorithm. To do this you will need to figure out what the following variables do: animationCnt, option, here, LastOption, offset, maze, currLabel, pathDone, and pathFound. Once you have understood how the program works, it is time to figure out how to add backtracking.

What to hand in: I want detailed descriptions of what all of the variables mentioned above do. "animationCnt is a counter" is not a detailed description. For this part of the assignment I encourage lots of discussion and collaboration. Try and figure out the program together. However, just asking someone for the answer is not collaboration and won't necessarily help you on this assignment. I highly reccomend you complete this part of the assignment before trying to write any of your own code. Once you have done that go on to adding backtracking to the code. Your book has a description of using backtracking on a maze task starting on page 149. The code is different enough that it will only be useful as a guideline, but it might help you to understand the task better. When you have completed the lab put your running program (the entire folder renamed according to your name) in the drop box and give me hardcopy of the program and of all of your variable descriptions.