Computer Science 210 Lab 8: Stacks and Backtracking
Due: April 12

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 (though fairly buggy) project called "Rats" 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 very dumb. It just moves through the maze randomly. Your job will be to make the mouse move with a purpose and to have it use backtracking to find good paths.

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 maze is implemented as a class with all of the functions you should need for this assignment.

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 vast majority of the code you will need to add/modify is in the FindPath routine.

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 a (stupid) algorithm to get 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 maze, 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 (you may collaborate with as many people on this as you want). 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. 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 (commenting them into the code is fine).