The objective of this assignment is to write a program for finding the shortest path through a maze using the breadth-first search algorithm described in class. It's a lot of fun, but do start early. There's also a very fun optional bonus problem at the end of the assignment.
First, a major sanity-preserving tip: Build this program incrementally. After each increment, test what you've done (perhaps by writing a simple testing program) to give you confidence that this part works. This is the SECRET TO ALL HAPPINESS! For example, you will need to implement a class defining a queue of MazeElements (individual cells in a maze). Write that class and then write your own testing program that creates a queue and then enqueues some data, dequeues some data, and prints the contents of the queue. Now that you have the MazeElementQueue written and tested, move on to writing the main program MazeBFS. In MazeBFS, write just a bit of code and then test it before moving on to the next logical part.
You may assume that a maze will always be 10 by 10 and that the entire perimeter of a maze will be entirely made of walls (except in the bonus problem, should you wish to do it).
You should assume that a single step is a move that is due north, south, east, or west. One cannot move from a node to one of its four diagonal neighbors in one step -- that's two steps away!
Here are the classes and files that you'll need to get this program working:
A note on elegance and design: In your breadth-first search algorithm you will need to examine all 4 neighbors of the "current" maze element. It may be tempting to have essentially the same (or very similar) code replicated for each of the 4 prospective neighbors. Replicating code like this is inelegant and you should avoid it. Why? For one thing, imagine that you had 8 prospective neighbors (you were allowing diagonally adjacent nodes to be considered neighbors) or, worse yet, you were operating in 3-dimensions and had perhaps 26 prospective neighbors! Then, replicating the code 8 or 26 times would be nasty! Moreover, even with just 4 prospective neighbors, the code is more readable and easily debugged and modified if the "common code" appears just once. Think about clean and elegant ways that you might do this and try to implement that in your code.
A note on debugging: For debugging purposes, it may be very helpful to you to print out the contents of the point queue during each iteration of the breadth-first search. It may be useful to add other print statements to aid in debugging. If your code is behaving funky, print out the contents of the important variables and data structures so that you can trace the behavior of your program by hand and compare it to what the program is doing. This is another SECRET TO ALL HAPPINESS! Do remove these debugging aids once your program works and you are ready to submit it.
In the directory /cs/cs60/assignments/assignment2 you will also find three sample 10 by 10 mazes called test1.maze, test2.maze, and test3.maze. You can run your program on these mazes by typing java MazeBFS test1.maze, for example. You can also create your own mazes by creating a text file with 10 lines of characters, each of which has 10 characters in it.
Please submit ALL of the java files that your program uses. You can use cs60submit multiple times, once for each file you wish to submit. Alternatively, you can submit multiple files at the same time. For example, typing cs60submit spam.java foo.java bar.java will submit the three files spam.java, foo.java and bar.java. Yet another alternative is to simply type the command cs60submitall with nothing else. This will submit every java file in the directory in which you're currently residing! Be sure to call the MazeBFS class MazeBFS.java. Our grading scripts look for exactly this file name.
Here's a fun bonus problem that extends what you just did. Please be sure to submit the regular part of the assignment before submitting the bonus problem.
Let's now assume that the maze is 3-dimensional. Each "floor" of the maze is still 10 by 10, but there can be an arbitrary number of floors.
Write a program called MazeBFS3D.java that takes in one or more files on the command line, each of which is a 10 by 10 maze. The first file is the first floor of the maze, the next file is the second floor of the maze, and so forth. For example, if there are just two floors, we might have files named floor0.maze and floor1.maze and we would run our program by typing
% java MazeBFS3D floor0.maze floor1.mazeHere are some of the specifics:
7 % java MazeBFS3D floor0.maze floor1.maze
Here's the maze:
Floor 0
****** **
S * * *
* * *O *
* * *
*
* * *
* * * *
* * *
* *
*** *****
Floor 1
* *******
* *
O *
* * * *
* * ******
* *
* ********
* *
* *D*
*** ***
-----------------------------------
And here's the solution:
Floor 0
****** **
So* * *
* o* *O *
* oooo*o *
ooo *
* * *
* * * *
* * *
* *
*** *****
Floor 1
* *******
* *
ooooooO *
*o * * *
*o* ******
*o *
*o********
oooo*ooo*
* ooo*D*
*** ***
Last modified January 2006 by Christine Alvarado or Ran Libeskind-Hadas