# Tile puzzle game getting tiles to move (help)

• February 27th, 2013, 07:16 PM
thatguy
Tile puzzle game getting tiles to move (help)
I am building a class called TilePuzzle and it is a text base game and it looks like this.

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 #

The way I programmed the move method it looks for the empty space(#), after finding the empty space it looks at the legal moves and compares the numbers around it to see if it is the number you passed to it, if so then switch them.

Code :

```  public void move(int tileNumber) { if (!isValid(tileNumber)) { return; }   for (int r = 0; r < boardSize; r++) { for (int c = 0; c < boardSize; c++) {   if (tile[r][c] == EMPTY) {   if (r != 0 && tile[r - 1][c] == tileNumber) { // checks up position tile[r][c] = tile[r - 1][c]; tile[r - 1][c] = EMPTY; } else if (r != (boardSize - 1) && tile[r + 1][c] == tileNumber) { // checks down position tile[r][c] = tile[r + 1][c]; tile[r + 1][c] = EMPTY; } else if (c != 0 && tile[r][c - 1] == tileNumber) { // checks left position tile[r][c] = tile[r][c - 1]; tile[r][c - 1] = EMPTY; } else if (c != (boardSize - 1) && tile[r][c + 1] == tileNumber) { // checks right position tile[r][c] = tile[r][c + 1]; tile[r][c + 1] = EMPTY; } } } } }```

boardSize is equal to 4 and EMPTY is equal to #.

now the problem, so the if statements that check for up and left work but the if statements that check for down and right don't work and I tried a couple different things like put a System.out.println(); in both body of the if statement and they are excusing when call but they don't switch the numbers. I also changed the (boardSize - 1) to 3 thinking that might have something to do with it but it still didn't work.

any help or ideas would be greatly appreciated.
• February 27th, 2013, 07:37 PM
Norm
Re: Tile puzzle game getting tiles to move (help)
Quote:

down and right don't work
Can you explain what "don't work" means?
What happens when a down move is chosen?
what happens when a right move is chosen?
• February 28th, 2013, 07:50 AM
thatguy
Re: Tile puzzle game getting tiles to move (help)
Here an example

you enter in 12 and the move method will switch the 12 with the empty space like so.

1 2 3 4
5 6 7 8
9 10 11 #
13 14 15 12

but if you enter 12 again it i will not switch, it just doesn't do anything. that is why I put System.out.println("test"); in the body of the down and right to see if they were being executed and they were but they don't switch the number with the empty space.
• February 28th, 2013, 08:19 AM
Norm
Re: Tile puzzle game getting tiles to move (help)
Quote:

but they don't switch the number with the empty space.
Try debugging the code by adding some println statements that print out the values of the variables used to control the moves the code makes. What are the indexes into the array for the from element and for the to element?

Do you have a test driver that will compile, execute and show the problem?

1 2 3 4
5 6 7 8
9 10 11 #
13 14 15 12

In the board position you posted, would the move be UP for 12 to where the # is?
• February 28th, 2013, 07:42 PM
thatguy
Re: Tile puzzle game getting tiles to move (help)
Code :

``` public void move(int tileNumber) { if (!isValid(tileNumber)) { return; }   for (int r = 0; r < boardSize; r++) { for (int c = 0; c < boardSize; c++) {   if (tile[r][c] == EMPTY) {   if (r != 0 && tile[r - 1][c] == tileNumber) { // checks up position tile[r][c] = tile[r - 1][c]; tile[r - 1][c] = EMPTY; } else if (r != (boardSize - 1) && tile[r + 1][c] == tileNumber) { // checks down position System.out.println("value of tile[r][c]: " + tile[r][c] + " value of tile[r+1][c]" + tile[r+1][c]); tile[r][c] = tile[r + 1][c]; System.out.println("value of tile[r][c]: " + tile[r][c] + " value of tile[r+1][c]" + tile[r+1][c]); tile[r + 1][c] = EMPTY; System.out.println("value of tile[r][c]: " + tile[r][c] + " value of tile[r+1][c]" + tile[r+1][c]); } else if (c != 0 && tile[r][c - 1] == tileNumber) { // checks left position tile[r][c] = tile[r][c - 1]; tile[r][c - 1] = EMPTY; } else if (c != (boardSize - 1) && tile[r][c + 1] == tileNumber) { // checks right position tile[r][c] = tile[r][c + 1]; tile[r][c + 1] = EMPTY; } } } } }```

run:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 #

Enter a number: 12
1 2 3 4
5 6 7 8
9 10 11 #
13 14 15 12
Enter a number: 12
value of tile[r][c]: 35 value of tile[r+1][c]12
value of tile[r][c]: 12 value of tile[r+1][c]12
value of tile[r][c]: 12 value of tile[r+1][c]35
1 2 3 4
5 6 7 8
9 10 11 #
13 14 15 12

The value are right and it looks like they have switch but when it doesn't move.
• February 28th, 2013, 07:51 PM
Norm
Re: Tile puzzle game getting tiles to move (help)
You need to add println() calls to all the if statements, not just a couple of them
and one println first thing inside the if when the empty slot is found.
The messages should say what kind of move was made: up, left, etc
You can't tell looking at the printout which move was made.

Does the printout say that three moves were made when the move() method was called?
Does that make sense?
• March 5th, 2013, 05:00 PM
thatguy
Re: Tile puzzle game getting tiles to move (help)
I think i do,
but what i posted before does what you have asked I believe.

1 2 3 4
5 6 7 8
9 10 11 #
13 14 15 12
Enter a number: 12

The user enter the number 12. so the if statement should switch the # and the 12 and the board should look like this.....

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 #

but it doesn't so i put some System.out statements in the if statement that move the number down.

//the number 35 is the empty slot.

// found the empty tile and the one above that the tile number the user enter (12)
value of tile[r][c]: 35 value of tile[r+1][c]12
// next I assign the empty tile to the value of the tile above it like so.
value of tile[r][c]: 12 value of tile[r+1][c]12
// now i set the tile that was above to empty
value of tile[r][c]: 12 value of tile[r+1][c]35

so logically everything seem to be working under the hood but its not switching them visually. It can't be the toString method because the it works when you switch is up or if the switch is left.

--- Update ---

Sorry as soon as I posted this my buddy sitting next to me suggested something and it works also figured out why my way didn't work.

thank you for your time i really appreciated.
• March 5th, 2013, 05:18 PM
Norm
Re: Tile puzzle game getting tiles to move (help)
Quote:

suggested something and it works