# move function doesn't move blank tile sideways [duplicate]

I've just implemented the move function and, when I was testing, I found that it didn't work when I swapped the blank tile with another number on its side, like this:

(I tried swapping 1 with the blank tile, and it duplicated the blank tile and that happens every time I swap with a number on the left or right - it works fine swapping up or down).

I tried to fix this problem, but, from what I've been seeing in the questions here, my code seems to be correct. The problem may be located in another function of my code, so I'll post all of them here.

Consider that I haven't changed a thing in the code itself (just implemented the functions). Also, I haven't yet implemented won, so I won't post it here.

Does anyone know what is happening?

PS.:For academic reasons, I will delete my code from here after the question has been answered.

• I think it was kind of the same problem, but my code is different from the one in the other question, so it didn't help me. Apr 4 '16 at 17:38

Your code is a slight variation of the same problem. Look at the following block from your code:

``````            else if(board[i+1][j] == 0 && board[i+1][j] < d)
{
hold = board[i][j];
board[i][j] = board[i+1][j];
board[i+1][j] = hold;
printf("\nmoved blank: %i with tile: %i\n", board[i+1][j], board[i][j]);
return true;
}
``````

There are two issues: If the code passes the if test, the array can go out of range, and the if test itself is flawed.

Out of range: Assuming the code gets past the if test, let's let d=3. Then i and j can go to 2. That means that i+1 can be 3, which is outside the range of the array, as described in the link I gave.

IF statement: `...&& board[i+1][j] < d...` This tests whether the VALUE STORED in the array is less than d, not whether either index is less than d. Instead, `i+1 < d` would validate the index.

• Oh, now I get it!! Thank you!!! Apr 4 '16 at 18:00
• I fixed that and the only thing I'm getting now is :( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right \ expected "4" character 3 of line 17, not "0" - I don't understand how everything else can be right and this one is not @CliffB Apr 4 '16 at 20:35
• Well, without seeing the current code, I don't know either. But then, that would be a new question. ;-) Apr 5 '16 at 1:26
• I have the same problem!!! I'll try to fix it that way. Jul 5 '16 at 19:07
• I had the same problem. Thanks! Aug 25 '17 at 15:03