pset3 fifteen check50

I get the following error on check50:

``````:( 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 have tried this out, and the fourth instruction 'down' returns illegal move, thus my move function must be the problem, however I can't figure out the exact problem never mind trying to resolve it!

Some direction would be appreciated, many thanks

Here is my move function:

``````bool move(int tile)
{
// TODO
//int row = board[d - 1][d - 1];
//int col = board[d - 1][d - 1];
int temp;

for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (tile == board[i][j])
{

{
if (board[i - 1][j] == 0 && i - 1 > 0)
{
temp = board[i - 1][j];
board[i - 1][j] = board[i][j];
board[i][j] = temp;
return true;
}
if (board[i + 1][j] == 0 && i + 1 < d)
{
temp = board[i + 1][j];
board[i + 1][j] = board[i][j];
board[i][j] = temp;
return true;
}
if (board[i][j - 1] == 0 && j - 1 > 0)
{
temp = board[i][j - 1];
board[i][j - 1] = board[i][j];
board[i][j] = temp;

return true;
}
if (board[i][j + 1] == 0 && j + 1 < d)
{
temp = board[i][j + 1];
board[i][j + 1] = board[i][j];
board[i][j] = temp;
return true;
}
}

}
}
}

return false;
}
``````

`if (board[i - 1][j] == 0 && i - 1 > 0)` and `if (board[i][j - 1] == 0 && j - 1 > 0)` causes a serious problem. `board[i - 1][j]` will only be 0 if `i - 1` is 0. Let's say that `i - 1` equals 0. The first part of your if statement evaluates to true, but then we reach `i - 1 > 0`. If `i - 1` equals 0, then you get `0 > 0`, which is not true. Thus, the code drops through to the bottom and returns false when it should've returned true. The same goes for `j - 1`.
In addition, you never use the variable `tile` that is passed to the function. Might I ask why? Instead of setting `board[i][j - 1]` and any variation thereof to `tile` and then continue the swapping process, you create a new variable `temp` and then swap that way. It'd be more efficient to use `tile`.
• @Sofia That's not the "wrong" way of doing this, but a more efficient way would be to have `if (board[i - 1][j] == 0 && i - 1 > -1)` I hope this answers your question, and if it does, please hit the green checkmark! It helps a lot :-) Feb 2, 2017 at 22:39