CS124, Fall 2010


Assignment #8

DUE: Thursday, December 2.


This and assignment 9, together, will be to create a Processing applet that allows a user to solve the "eight puzzle" with a mouse, as illustrated here.

In this first part, we will not be concerned with the GUI aspects of the project, but only the "textual" aspects. The new programming techniques this assignment deals with are: (a) the two-dimensional array and (b) encapsulating such an array in a class definition. There will also be some opportunities to use conversions between characters and ints, something that was referred to earlier on but never really used.

Write a class definition called EightPuzzle. It should have methods for initializing the board, adding a move, displaying the board, telling if the puzzle is solved, and reinitializing the game. Create a driver class to play the game at the terminal interactively. The board should be represented internally by a 3 X 3 array of char (indexed 0, 1, 2). You can use character digits to represent the actual numbers in the squares, and the space character to represent the empty square (this comes in quite handy for the graphical part). You will also need int instance variables to record the current empty row and column (let's call them emptyRow and emptyColumn).

For this assignment, I'm giving you a complete specification to start with, right here. All the method headers and documentation are included. It will be your job to put the correct code in the method bodies. All of them are short (at most 5 to 10 lines of code apiece).

It is absolutely vital that the interface be left intact! That is, do NOT change the method headers, nor what they are supposed to do. If you do so, the next assignment, which will be to create the GUI interface, will become more difficult. To eliminate difficulties as much as possible, if there is any question in your mind as to the purpose of a method, please ask me about it.

Remember to just work on one method at a time. That is, write a method, and test just the one method. BlueJ is a great aid in this, of course. You can create an EightPuzzle object and test every single unit individually. Things would be easiest if you write (and test!) the methods in the following order. Do not proceed to any method until all the previous methods on the following list are written and tested: When you're done with this assignment, a sample run might look like this.


Back to CS 124 Home Page