Students will be able to (1) explain and use backtracking in Java applications, (2) read data from files using standard input redirection, (3) read data from files using a file name supplied as a command line argument, (4) gain experience in using listeners in Java.
Read the Wikipedia article on Backtracking.
Read my old course notes on Backtracking.
As a lover of rats, you’ve adopted a few as pets from the shelter. You take great care of your little friends, making sure they have lots of time to forage and participate in enrichment activities. You notice they’re really good at mazes; in particular, you’ve discovered that once they backtrack after a dead end, they’re smart enough not to needlessly revisit those paths they know aren’t right. How do they do that, you wonder? Because you are also interested in science, neuroscience, artificial intelligence, and (most importantly) computer science, you set out to build a maze-solving program. Can you do better than the rats?
As in the previous labs, go to GitHub, select Import Repository. Then:
The import might take a few minutes. When done, clone the repo and get ready to work.
Complete the Maze
class which is stubbed out in the starter code. The starter code has extensive comments telling you what is expected.
Make sure the maze tests pass before moving on.
$ javac MazeTest.java && java MazeTest
Complete the BacktrackingMazeSolver
class which is stubbed out in the starter code. The starter code has extensive comments telling you what is expected.
The backtracking algorithm you should employ is:
Initialize path to an empty stack Set current to the initial rat position while True: Place the rat at current Notify the listener If the rat has reached the cheese: Return True // If you can, move to an adjacent cell and leave a breadcrumb If the cell above the current cell can be moved to: Push the current cell on path Mark the current cell as being on the path Move to the cell above (by updating the value of current) Else if the cell to the right of the current cell can be moved to: Push the current cell on path Mark the current cell as being on the path Move to the cell to the right (by updating the value of current) Else if the cell below the current cell can be moved to: Push the current cell on path Mark the current cell as being on the path Move to the cell below (by updating the value of current) Else if the cell to the left of the current cell can be moved to: Push the current cell on path Mark the current cell as being on the path Move to the cell to the left (by updating the value of current)) Else: // There was no place to go from here! This is a dead end! Mark the current cell as a dead end “Back up” by popping from the path stack into current, but: if the stack is empty (i.e., nowhere to backup to): return False
The ConsoleBacktrackingMazeSolver
class has a main
method so the application can be run from the command line. If the program is given no command line arguments, it reads a maze description form standard input (we’ll cover standard input redirection in class). If given a single argument, the argument should be the description of maze. Any errors in the number or form of arguments should cause a message to be written to standard error.
If a valid maze is obtained, the application will animate the backtracking solution in the terminal window. All the “graphics” are done in the starter code, so you won’t have to write any graphics code. However, you will need to read and understand the way the animation is unobtrusively wired into the maze solver. (One of the lab report questions below asks you to explain the architecture.)
The starter code contains a couple of sample maze files, called testmaze-small and testmaze-medium. Try them out!
Replace the textual rendering of the maze to something pretty and colorful. As usual, there is no expectation to you work on challenge problems, and no extra credit is awarded for doing them.
Online:
On hardcopy or email:
BacktrackingMazeSolver
) that knows nothing at all about graphics, and yet, was able to be animated while it ran.Maze
class?testmaze-5x5-maximal
.