First, enter the cs190m directory:
$ cd cs190m $ mkdir lab12 $ cd lab12
Then, download EightPuzzle.java into the lab12 directory.
The GUI for an 8-puzzle is rather simple. If you read the article linked above, you know that the main part of the interface will simply be a 3-by-3 grid; at any given time, 8 of those cells will have to display an image that is a piece of the complete image, while one of them will be blank. Initially, the puzzle will start off solved. To start the actual puzzle, the player should click a "Shuffle" button, possibly several times, which tells the program to randomly move tiles around for awhile before returning control back to the user. The programming can be iteratively programmed in three steps:
Since you need to make a 3-by-3 grid to hold pieces of the image, a GridLayout seems like a logical choice for this. To represent tiles, you should use JButton objects. Even though there are only 8 image tiles, you should create 9 buttons. Rather than moving buttons around, it's easier to simply switch the properties (the images) of two buttons. In our case, our 8-puzzle will start with its missing piece at the bottom right. All of the other buttons should be created with an ImageIcon object as the background. The images are loaded for you and stored in the icons array.
The first three TODOs in the code document what you need to do with respect to the grid. First, you create a JPanel that holds the grid of buttons; then, you create the first 8 buttons, which have corresponding images to use; lastly, you create the empty button, which has no image.
The shuffle button should be added at the bottom of the GUI below the grid. When it executes, it should perform any number of legal moves and then stop, allowing the player to start moving pieces around.
TODO 4 asks you to create a shuffle button.
TODO 5 is the implementation of event handling. There are 10 distinct methods that you must handle (9 buttons on the grid, and 1 shuffle button). The logic for moving tiles is already implemented for you; your task is simply to call the proper method when a button is pressed. The task is further documented in your skeleton code.
The EightPuzzle class also has a main method that will create the GUI and can be used for testing your program. You can test your program by compiling and running the command java EightPuzzle.
For essentially all of your classes that have programming assignments, you'll be using the turnin command to submit your code for grading. This is one command you'll want to know how to use (there's always the man pages!), since if you don't turn in your code properly, you will likely end up getting a zero on all of your hard-earned work. Typically, to turn in a lab, you'll go into the lab directory and then turnin all of your .java files, like so:
$ cd ~/cs190m/lab12 $ turnin -v -c cs190m -p lab12 *.java
Many of you will probably run across problems while programming at some point during a lab. If that's the case, here are the resources you should use, in order:
Lab created by: Daniel Tang