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.
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.