Labsheet 7 (for week 10)
This lab provides practice using 2D arrays, on-screen display, and exceptions.
The original version was written by Gordon Royle.
It is suggested that you do this lab in pairs; if you need help finding a partner,
please try help1001.
The 15 puzzle is a sliding piece puzzle
consisting of a 4x4 grid containing fifteen tiles numbered 1..15 plus a blank space.
Originally, the tiles are arranged in a disorderly fashion, as shown in the left-hand image below.
The player makes a series of moves,
each of which consists of sliding one tile into the blank space.
The aim is to attain the goal configuration, as shown in the right-hand image.
You can try out the 15 puzzle online.
In this lab, you will construct a Java program to allow a person to play the 15 puzzle.
Create a folder 15 puzzle, and download these two files.
Complete the walking skeleton of FifteenPuzzle.java.
Search for //TODO comments and use the comments to determine what each method must do.
Do not change SimpleCanvas.java.
There are five methods and two constructors to write.
I suggest that you approach the task as follows.
- Write a simple version of the first constructor that
initialises the four instance variables and then calls drawGrid.
- Write a simple version of drawGrid that calls drawTile for each tile,
in an appropriate colour. Remember that it needs to call repaint.
- Write a simple version of drawTile. Now you can display the original grid set by the user.
- Write legalClick.
- Write a simple version of moveTile that checks its arguments,
updates the instance variables as appropriate, and re-draws the grid.
Now you can play the game by repeatedly invoking this method.
- Write finished. Now you can detect if the player has completed the puzzle.
Now there are several ways you can enhance your program.
Tackle the following in any order you like.
- Write the second constructor, which sets up a random initial position.
You can't just place the fifteen tiles randomly - if you do this,
there is a 50% chance that the puzzle will be
impossible to solve.
So this constructor should work by initialising the grid to goal,
then making a large number of random legal moves to scramble it.
- Update the first constructor to check that its argument is a legal puzzle position,
i.e. a 4x4 grid containing the numbers 0..15 inclusive.
Update the constructor (and moveTile) to throw exceptions if their arguments are illegal.
- Improve your display. This is what mine looks like. (It's not complicated to write...)
- Update your program to use the mouse for input.
Again, this is less complicated than it may sound - the required changes are described below.
Using the mouse for input
This takes four simple changes to your program.
- Update the class header to be
public class FifteenPuzzle implements MouseListener
This tells Java that this class will implement the MouseListener class.
- Add the line
sc.addMouseListener(this);
in your constructor(s), after you initialise the variable sc.
This tells Java that objects belonging to this class want to be notified of mouse events (e.g. movements, clicks, etc.)
in the relevant window.
- Add these five methods to your class.
public void mouseClicked (MouseEvent e) {}
public void mousePressed (MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
These methods tell the system what code to run when a particular type of mouse event happens
in the relevant window.
- Probably all you want to do is to detect and act upon mouse clicks?
if so, add the statement
moveTile(e.getX() / tilesize, e.getY() / tilesize);
to the body of mouseClicked.
e.getX and e.getY return the coordinates where the click happens,
relative to the top-left corner of the window.
- If you want to implement more mouse functionality, go for it!
You can read about the class MouseListener at the
Java 1.7 API.
Animate your puzzle in interesting ways, using the mouse for input.
If you create an interesting display, email your updated version of FifteenPuzzle.java
to [email protected].
There will be a prize** for the best submission.
**All prizes are awarded or not solely at Lyndon's discretion.