CS367 Programming Assignment 3
| ||||
AnnouncementsCheck here periodically.
| ||||
OverviewWhy are we doing this program? DescriptionIn this assignment you will be creating a program that takes a maze (specified in a file) and solves it by either finding a path from the start of the maze to the finish or determining that no such path exists. Your program will use a recursive algorithm that involves backtracking to solve the maze and a stack to keep track of the path from start to finish. GoalsThe goals of this assignment are to:
| ||||
SpecificationsWhat are the program requirements? The Stack classYou must create your own Stack class, in a file called Stack.java, rather than using Java's Stack class. Your class must implement the StackADT interface (this interface is provided for you in the p3.jar file). Your class must have a public constructor that takes no arguments. All fields in your class should be private and you should not implement any additional public methods or constructors. You are free to choose how to implement your Stack so long as it is efficient. You may use either an explicit linked list/array, or you may use Java's (or your own) LinkedList/ArrayList as a field in your Stack class. Note that some of the methods listed in the StackADT interface throw EmptyStackException. You must write this exception, in a file called EmptyStackException.java, and the exception must be a checked exception. Testing the Stack classTo test your Stack class, you must write a file named TestStack.java that defines the TestStack class, which should contain only a main method. The sole purpose of that main method is to thoroughly test the Stack class. The main method does not use any command-line arguments nor user input. It should call all of the Stack methods, making sure that they work as expected. For example, your code might call push(ob) then peek and pop. If the calls to peek and pop return something other than ob, your code would print an error message. Don't forget to test calls that should cause exceptions to be thrown (and print an error message if the exception is not thrown). Your grade on this part of the assignment will depend on
The MazeSolver classFor the purposes of this assignment, a maze may be thought of as a rectangular grid. One square in the maze is designated the start position and one the finish position. Each square in the maze is either a wall (which may not be moved through) or clear (i.e., empty). A player begins at the starting position, must stay within the maze at all times, and can only move to squares that are clear. The allowable moves are north, south, east, and west (i.e., no diagonal moves allowed). The goal is to solve the maze by either finding a path from the start position to the finish position or determining that no such path exists. You must write a MazeSolver class, in a file called MazeSolver.java, that must follow these specifications:
The maze packageA maze package has been provided that contains all the classes necessary to represent a maze:
Solving a mazeIf you are at the start of the maze, you can systematically find your way to the finish using the following algorithm. This algorithm uses back-tracking, i.e., retracing your steps when you reach a "dead-end".
The goNorth method will examine all paths that start at the square immediately to the north of the present square as follows:
The goWest method will examine all the paths that start at the square immediately to the west of the present square as follows:
The methods goSouth and goEast are analgous to the methods just described. Note that in each case, the order in which the directions are considered in goDir is the current direction Dir, then the other directions counter-clockwise starting with Dir (i.e., after taking a step south, goSouth will try south, then east, then west; after taking a step east, goEast will try east, then north, then south) . Keeping track of your path using a stackThe preceding algorithm will find a path from the start to the finish, if one exists, and mark the path in the maze (as well as the other squares that were visited). But, what is this path, that is, what is the sequence of squares that will lead one from start to finish? In order to be able to provide this information, we will need to augment our algorithm to keep track of the squares in our path. We can do this easily by maintaining a stack of these positions. The first item on our stack will be the start position. As we add squares to our path, we will need to push those positions onto our stack. When we decide that a square is a dead-end, we will need to pop that position from the stack. When we have solved the maze by finding a path from the start to the finish, the stack will contain the squares in the path in order with start at the bottom of the stack and finish at the top (i.e., in backwards order). Your MazeSolver class must create and maintain a stack of positions (as described in the preceding paragraph). When the printPath method is called (after the maze has been solved), your program must print out the path in order from start to finish. You should implement your printPath method so that it functions correctly even if it is called more than once. How to proceedAfter you have read this program page and given thought to the problem we suggest the following steps:
| ||||
Extra Credit: Maze DisplayFor extra credit, you can use the MazeDisplay component to graphically display the progress of your maze solver. To implement this, you will need to work with an instance of the MazeDisplay class. You should:
You will have to use the following classes:
| ||||
Handing inWhat should be handed in? Make sure your code follows the style and commenting standards used in CS 302 and CS 367. New procedure for students working in pairs: Students working in pairs will only submit one copy of all program files (i.e., only one partner submits the program files), except the README file described below. Electronically submit the following file to your In "handin" directory by the due date and time:
Pair Programming students must also submit a README file: All students working in pairs must read the Pair Programming Guidelines and submit a README file. Each student working in a pair must hand in this file to his/her own handin directory. Copy, complete, and hand in the file that is linked here: Please turn in only the file named above. Extra files clutter up the "handin" directories. | ||||
This program description is based in part on Programming Problem #5 from Chapter 5 of
Data Abstraction and Problem Solving with Java: Walls and Mirrors, Frank M. Carrano
and Janet J. Prichard, Addison Wesley Longman (2001).
Last Updated: 10/31/2008 © 2008 Rebecca Hasti and Jim Skrentny, CS367 Instructors, hasti@cs.wisc.edu |