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:
- empty(). This tests if a given square is empty.
Note that it takes two integer arguments, representing the row and
column that specifies the square.
The method empty() should be written so that row and
column can take on values greater than 2 or less than 0!
It should return false if that happens, and you can test this feature
of empty() easily enough. Finally, note that,
if the arguments are in the correct range (between 0 and 2 inclusive) and
the corresponding square is indeed empty, then before returning true,
empty() should have the following side effect: Set the instance variables emptyRow and
emptyColumn to the appropriate values. The side effect is
not included in the documentation because it's not something the
client needs to know. (You do, of course.)
- legalMove(). This tests to see if a given move is
"legal", meaning that the square being moved is not empty and is adjacent
to an empty square. This is simply done using 4 invocations of empty():
you look at the squares above, below, to the left and to the right of the
given square. Note that some of these squares might not even be on the
board. That's okay! If you wrote empty() as specified, it
will return false for those squares. You can test this method by having
your driver method print out "Legal move" or "illegal move" depending on
what move you make.
- addAMove(). This is straightforward once empty() and
legalMove() have been implemented. You must invoke legalMove()
before invoking addAMove(). Then the instance variables emptyRow
and emptyColumn will contain the row and column of the empty square.
You can just move the square specified in the arguments of addAMove
to the empty square, and set the moved square to empty. Also increment the
number of moves.
- gameWon(). After the above methods are working, most of the game
is functional. You have only to recognize when the puzzle is solved. Hints
for the code here are given in the documentation.
- getMoves(). Easy, once addAMove() does the right thing.
When you're done with this assignment, a sample run might look like
this.
Back to CS 124 Home Page