Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 26th, 2012, 11:08 PM
 Seahawk9892
Registered User

Join Date: Nov 2012
Posts: 14
Time spent in forums: 3 h 56 m 3 sec
Reputation Power: 0
[Homework] Sliding Puzzle

Hello all, I am having trouble with a homework problem. I have posted a general description of it below:

----------------------------------------------------------------------

The 15-Tile Puzzle is a game where a player moves tiles on a 4 x 4 board to try to arrange them into a predetermined order. You are to do a simulation of this game.

+---+---+---+---+
| 1 | 2 | 3 | 4 |
+---+---+---+---+
| 5 | 6 | 7 | 8 |
+---+---+---+---+
| 9 |10 | 11 | 12 |
+---+---+---+---+
| 13 | 14 | 15 | |
+---+---+---+---+

The 4 x 4 board consists of 15 tiles, where the intial position is shown above. There is always one blank space where there is no tile (It is supposed to be symmetrical - I could not accurately draw it in text).

In your simulation, allow the user to control the movement of the tiles using the following commands:

'R': Moves a tile to the right.
'L': Moves a tile to the left.
'U': Moves a tile up.
'D': Moves a tile down.
'Q': Quit the program.
'S': Save the current board.

The save function will save the current board information into a text file. The file can be of any name in any location of your choice. You also need to decide the save format (e.g., one number per line).

The load function will load the saved board information and allows a user to continue from last saved game.

Suppose the current board is as shown in the initial position above. The command:

'R' will move the tile 15 to the right.
'L' is an invalid move since it is not possible to move any tile to the left.
'D' will move the tile 12 down.
'U' is an invalid move since it is not possible to move any tile up.

Assume the user will always enter one character. Both upper case and lower case characters are allowed. If an invalid command is entered, display "Wrong command!". If an invalid move occurs, display "Invalid move!".

Your program should generate a random puzzle (i.e, 15 numbers placed on the 4 x 4 board) for the user to solve. The goal of the user is to move the tiles from the position in the random puzzle until the initial position is reached.

----------------------------------------------------------------------

I honestly have no idea how to even begin this problem. I know I should use a 16-integer array to store the information in the tiles, however I am not sure how to initialize this.

Any help would be greatly appreciated.

#2
November 27th, 2012, 08:46 AM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 4 m 22 sec
Reputation Power: 347
Is this a console program, no GUI?
You should work on the design for the program before trying to code it.
Make a list of the steps the program needs to do to solve the problem. When the logic makes sense, then work on coding it.

#3
November 27th, 2012, 01:39 PM
 Seahawk9892
Registered User

Join Date: Nov 2012
Posts: 14
Time spent in forums: 3 h 56 m 3 sec
Reputation Power: 0
Quote:
 Originally Posted by NormR Is this a console program, no GUI? You should work on the design for the program before trying to code it. Make a list of the steps the program needs to do to solve the problem. When the logic makes sense, then work on coding it.

Yes, this is a console program. Sorry I should have mentioned that.

Step 1: I suppose the first thing that needs to be done is to initialize the board in the form of a four by four array.

Code:
``` public class Puzzle
{

public static void main(String[] args)
{
int [][] board = new int [4][4];

}
} ```

Step 2: Now, I need to assign values to the 16 spaces, ranging from 1-15 and one blank space. I guess I should use a loop to assign these values, but I am not sure how to do this?

#4
November 27th, 2012, 01:42 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 4 m 22 sec
Reputation Power: 347

#5
November 27th, 2012, 02:34 PM
 Seahawk9892
Registered User

Join Date: Nov 2012
Posts: 14
Time spent in forums: 3 h 56 m 3 sec
Reputation Power: 0
I checked out the tutorials, however I am still confused as how to initialize the board. Should I use a constructor or a method that returns 15 integers and a blank space?

I also need to figure out how to make sure that the board is randomized, so should I import java.util.Random?

#6
November 27th, 2012, 03:02 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 4 m 22 sec
Reputation Power: 347
I assume you want 15 numbers in random order. One way to get that is to put the numbers 1 to 15 in a collection and use the shuffle() method to randomize them.
Then get them out one at a time from the start of the collection.

For testing, I'd leave that step for later and just hardcode an array manually with the 15 numbers. Later you can work out how to do the above.

#7
November 27th, 2012, 04:50 PM
 Seahawk9892
Registered User

Join Date: Nov 2012
Posts: 14
Time spent in forums: 3 h 56 m 3 sec
Reputation Power: 0
I have updated my code below:

Code:
``` public class Puzzle
{

public static void main(String[] args)
{
int [][] board = new int [4][4];

board [1][1] = 1;
board [2][1] = 2;
board [3][1] = 3;
board [4][1] = 4;
board [1][2] = 5;
board [2][2] = 6;
board [3][2] = 7;
board [4][2] = 8;
board [1][3] = 9;
board [2][3] = 10;
board [3][3] = 11;
board [4][3] = 12;
board [1][4] = 13;
board [2][4] = 14;
board [3][4] = 15;
board [4][4] = -1; // -1 is just a temporary placeholder for the blank space.
}
} ```

In order to shuffle the values, I was thinking of creating a loop that randomly swapped the row and column number of two values using a swap method. For instance, consider two values:

board [1][1] = 1;
board [2][3] = 10;

I want to create a method that makes these values become:

board [2][3] = 1;
board [1][1] = 10;

How could I do this, or is this even possible?

#8
November 27th, 2012, 05:00 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 4 m 22 sec
Reputation Power: 347
The Collections class has a shuffle() method, you don't have to write the code to shuffle the 15 numbers. Create a collection, fill it with the 15 numbers, shuffle it, extract the numbers one at a time and add them to the 2Dim array inside of a nested loop.

BTW you must not have tried to compile and execute the posted code. The array's indexes go from 0 to the length-1

#9
November 27th, 2012, 05:15 PM
 Seahawk9892
Registered User

Join Date: Nov 2012
Posts: 14
Time spent in forums: 3 h 56 m 3 sec
Reputation Power: 0
Quote:
 Originally Posted by NormR The Collections class has a shuffle() method, you don't have to write the code to shuffle the 15 numbers. Create a collection, fill it with the 15 numbers, shuffle it, extract the numbers one at a time and add them to the 2Dim array inside of a nested loop. BTW you must not have tried to compile and execute the posted code. The array's indexes go from 0 to the length-1

Yeah, I haven't compiled it yet, I am just trying to make sense of things as I am a little lost. How do I create a collection? I have tried to find examples on the internet but I can't find any.

#10
November 27th, 2012, 05:22 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 4 m 22 sec
Reputation Power: 347
As I said before, postpone creating a random fill for now. Do it manually and move on to get the rest of the code working. When you get more experienced come back to it.

#11
November 27th, 2012, 06:44 PM
 Seahawk9892
Registered User

Join Date: Nov 2012
Posts: 14
Time spent in forums: 3 h 56 m 3 sec
Reputation Power: 0
I have created some very basic code so I can just have some framework for the problem. It is shown below:

Code:
``` public class Puzzle
{

public static void main(String[] args)
{
int [][] board = new int [4][4];

board [1][1] = 1;
board [2][1] = 2;
board [3][1] = 3;
board [4][1] = 4;
board [1][2] = 5;
board [2][2] = 6;
board [3][2] = 7;
board [4][2] = 8;
board [1][3] = 9;
board [2][3] = 10;
board [3][3] = 11;
board [4][3] = 12;
board [1][4] = 13;
board [2][4] = 14;
board [3][4] = 15;
board [4][4] = -1; // -1 is just a temporary placeholder for the blank space.

String lineSeparator = "+---+---+---+---+";
String row1 = "| " + board[1][1] + " | " + board[2][1] + " | " + board[3][1] + " | " + board[4][1] + " |";
String row2 = "| " + board[1][3] + " | " + board[2][2] + " | " + board[3][2] + " | " + board[4][2] + " |";
String row3 = "| " + board[1][3] + " | " + board[2][3] + " | " + board[3][3] + " | " + board[4][3] + " |";
String row4 = "| " + board[1][4] + " | " + board[2][4] + " | " + board[3][4] + " | " + board[4][4] + " |";

System.out.println(lineSeparator);
System.out.println(row1);
System.out.println(lineSeparator);
System.out.println(row2);
System.out.println(lineSeparator);
System.out.println(row3);
System.out.println(lineSeparator);
System.out.println(row4);
System.out.println(lineSeparator);

}
} ```

In the above code, I have attempted to form a box around each of the numbers in the array for aesthetic purposes. However, when I execute this code, I receive the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Puzzle.main(Puzzle.java:14)

What does this mean?

#12
November 27th, 2012, 06:51 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 4 m 22 sec
Reputation Power: 347
In post #8 I mentioned that array indexes go from 0 to the length-1
In an array with 4 slots the max index is 3

#13
November 27th, 2012, 06:55 PM
 Seahawk9892
Registered User

Join Date: Nov 2012
Posts: 14
Time spent in forums: 3 h 56 m 3 sec
Reputation Power: 0
Quote:
 In post #8 I mentioned that array indexes go from 0 to the length-1 In an array with 4 slots the max index is 3

Ah, I saw you mention this however I did not understand it until now, thanks. So what am I supposed to do for values above 3?

#14
November 27th, 2012, 06:57 PM
 NormR
Contributing User

Join Date: Aug 2010
Location: SW Missouri
Posts: 3,614
Time spent in forums: 2 Weeks 3 Days 7 h 4 m 22 sec
Reputation Power: 347
There are no indexes above 3 for an array with 4 slots.

#15
November 27th, 2012, 07:02 PM
 Seahawk9892
Registered User

Join Date: Nov 2012
Posts: 14
Time spent in forums: 3 h 56 m 3 sec
Reputation Power: 0
Quote:
 Originally Posted by NormR There are no indexes above 3 for an array with 4 slots.

Wow I feel foolish, now I see what you mean. I needed to begin at 0 instead of 1.

Code:
``` public class Puzzle
{

public static void main(String[] args)
{
int [][] board = new int [4][4];

board [0][0] = 1;
board [1][0] = 2;
board [2][0] = 3;
board [3][0] = 4;
board [0][1] = 5;
board [1][1] = 6;
board [2][1] = 7;
board [3][1] = 8;
board [0][2] = 9;
board [1][2] = 10;
board [2][2] = 11;
board [3][2] = 12;
board [0][3] = 13;
board [1][3] = 14;
board [2][3] = 15;
board [3][3] = -1; // -1 is just a temporary placeholder for the blank space.

String lineSeparator = "+---+---+---+---+";
String row1 = "| " + board[0][0] + " | " + board[1][0] + " | " + board[2][0] + " | " + board[3][0] + " |";
String row2 = "| " + board[0][1] + " | " + board[1][1] + " | " + board[2][1] + " | " + board[3][1] + " |";
String row3 = "| " + board[0][2] + " | " + board[1][2] + " | " + board[2][2] + " | " + board[3][2] + " |";
String row4 = "| " + board[0][3] + " | " + board[1][3] + " | " + board[2][3] + " | " + board[3][3] + " |";

System.out.println(lineSeparator);
System.out.println(row1);
System.out.println(lineSeparator);
System.out.println(row2);
System.out.println(lineSeparator);
System.out.println(row3);
System.out.println(lineSeparator);
System.out.println(row4);
System.out.println(lineSeparator);

}
} ```

Now I receive the output I was looking for:

+---+---+---+---+
| 1 | 2 | 3 | 4 |
+---+---+---+---+
| 5 | 6 | 7 | 8 |
+---+---+---+---+
| 9 | 10 | 11 | 12 |
+---+---+---+---+
| 13 | 14 | 15 | -1 |
+---+---+---+---+

Now I need to begin working on my loop.

 Viewing: Dev Shed Forums > Programming Languages > Java Help > [Homework] Sliding Puzzle