For the Java homework assignment, the sliding
puzzle, I'm giving myself a grade of A+. In order to explain this, I'm
going to go line-by-line through the guidelines and
answer with what I've done.
- Get a photograph and cut it into six pieces. Use only five for
the toy. -- I went one step further, setting it up so the program
only needs to read in the big image, and then cuts it up itself.
- Write an applet that reads in the five images and then sends
them to a canvas to be painted at certain x,y coordinates. -- Done;
again, it only read in the one image, and handled creating five on its own.
- Create an object that has an image, x and y coordinates, its cell
location (there are six cells), and a boolean flag indicating whether or not
it can move. -- Done; it's the "piece" class.
- Create a randomizing process that selects a random number from 0
to 5 (i.e., there are six cells) and assigns one of the six cells to each
image object. -- Done; this is handled in init_board() and
scrambler().
- Create a canvas that receives these five image objects, paints them at
certain locations and remembers where the blank is. -- Done.
- Add a MouseListener to the canvas that reports the location of a mouse
click and figures out which cell has been targeted. -- Done, although
with ActionListeners and the mousedown. It's basically the same thing.
- If the target cell "can move" then shift it to the blank cell (i.e.,
repaint it in the blank cell) -- Done in the mousedown() handling function.
- Add a button to the applet that "restarts" the process (i.e., generates
another random number, assigns cell locations and sends them to the canvas to
be repainted. -- Done.
The source code for the project is available here:
Return to the main site