CS 340 - Software Design

Spring 2006


MP 2 - Fifteen Puzzle

Due: Thursday February 9, 2006 at 11:59 pm

The Fifteen Puzzle is one variation of the N-Puzzle. The Fifteen Puzzle was made famous by Sam Lloyd who in 1878 offered $1000 to anyone who could solve an unsolvable variation of the puzzle. The puzzle consists of a 4x4 grid with the numbers from 1 to 15 on the grid leaving one grid position empty. The numbers are mixed up the puzzle is solved when the number are put into the following order:

To solve the puzzle, a number that is next to the empty position is moved into the empty position. By "next to", the number can be above, below, to the left or to the right of the empty position. The empty position will now occupy the grid position were the number had been.

Your program is to implement the Fifteen Puzzle using the Java Swing GUI elements. One method to do this is to create a 4x4 grid of 16 buttons. Use the text of the buttons to specify which number (or the blank) is stored at that grid position. When the user clicks on a button that is next to the "blank" button, the text on those two buttons is exchanged. Clicking on any other button does nothing.

Your program will need a menu that will perform the following operations.

A discussion of what makes an ordering of the numbers in the puzzle solvable or not can be found at http://mathworld.wolfram.com/15Puzzle.html. The basic idea is whether there are an odd or even number of permutations inversions compared to the solved ordering of the numbers. Any arrangement that has an even number of permutation inversions is solvable, whil any arrangement that has an odd number of permutation inversions is unsolvable.

The easiest way to determine if an arrangement is solvable or not is to count the inversions made for each position. First think of the 4x4 grid as a one dimensional array were the position in the array is (row-1)*4 + col. Thus the positions of the grid as:
1234
5678
9101112
13141516
correspond to the array:
1234 5678 9101112 13141516
For each value in the array, count the number of values in a higher position with a smaller value. This will given the number of permutation inversions. If this number is even, the puzzle is solvable. If this number is odd, the puzzle is not solvable.

The following discussion is taken from http://mathworld.wolfram.com/15Puzzle.html. Consider the following puzzle.

The number 13 is at position 1. Since there are 12 values less than 13 at position higher than 1, the inversion count for the 13 is 12. The number 7 is at position 6. Since there are 4 values less than 7 at positions higher than 6 (the 4 at position 7, the 1 at position 9, the 3 at position 13 and the 2 at position 15), the inversion count for the 7 is 4. For the entire puzzle, the inversion counts are 12, 9, 9, 5, 4, 4, 3, 3, 0, 3, 3, 2, 1, 1, and 0, giving the total inversion count of 59. Since this number is odd, the above arrangement of the puzzle cannot be solved. Note that this example has the blank at position 16 and thus it is excluded from the inversion count. It may be simpliest to follow this idea and only randomize the numeric values on the grid.

You may earn 10 pts additional credit by having an image divided into 15 parts instead of using the numbers from 1 to 15. It is your responsibility to get and prepare the image for your program.

Submission of the Program

Your program is to be submitted electronically via the turnin command on the LINUX machines. The project name for this is mp2. All programs are expected to be written in good programming style.

Turnin your program electronically using the "turnin" command from your CS account as follows:

turnin -c cs340 -p mp2  [your project directory]
where the [your project directory] is the directory name under which you have all your files related to this programming problem. The turnin command will automatically compress the data under your directory, so there is no need to do the compression by yourself.

Notice you can only invoke turnin command on the Linux machines in the lab or after logging into the server machine oscar.cs.uic.edu.

If you want to verify that your project was turned in, look in the turnin directory for a file with your userid. For instance for this project, from your CS account you would type:

turnin -c cs340 -p mp2 -v

Note that you can execute turnin as many times as you would like, up until the program deadline when turnin will be disabled for this project. Each time you execute turnin for a project, you overwrite all of what you had turned in previously for that project. It does not work in an incremental way.

CS 340 - Fall 2005