Please, I spent so many hours on this and I can't find the mistake I am doing. From the start, there are some numbers still move when they are illegal move.
See description from check50:
:( 3x3 board, move blank left then right
\ expected "1" character 3 of line 11, not "0":) 3x3 board, move blank up then down
:( 3x3 board, move up-up-left-down-down-left-up-up-right-down-down-right
\ expected "3" character 3 of line 51, not "0":( 3x3 board, from start try to move tile 2, 4, 5, 6, 7, 8
\ expected "2" character 1 of line 7, not "0":( 3x3 board, move blank left (tile 1) then up (tile 4), then try to move tiles 1, 2, 6, 8
\ expected "1" character 5 of line 15, not "0":( 3x3 board, move blank up (tile 3), then try tile 2
here is my move code:
bool move(int tile)
//int tile is number choosen to move by the player
{
int i = 0; // row
int j = 0; // column
// find the location of the number choosen by player
for (i = 0; i < d; i++)
{
for (j = 0; j < d; j++)
{
if (board[i][j] == tile)
{
//check if tile's location is a legal move from the blank's tile
if (i - 1 >= 0 && board[i - 1][j] == 0) // if blank is above tile
{
// swap the blank's tile and the tile
int tmp = board[i][j];
board[i][j] = board[i - 1][j];
board[i - 1][j] = tmp;
return true;
}
else if (i + 1 < d && board[i + 1][j] == 0) // if blank is top tile
{
int tmp = board[i][j];
board[i][j] = board[i + 1][j];
board[i + 1][j] = tmp;
return true;
}
else if (j + 1 >= 0 && board[i][j + 1] == 0) // if blank is above left to
{
int tmp = board[i][j];
board[i][j] = board[i][j + 1];
board[i][j + 1] = tmp;
return true;
}
else if (j - 1 < d && board[i][j - 1] == 0) // if blank is right to tile
{
int tmp = board[i][j];
board[i][j] = board[i][j - 1];
board[i][j - 1] = tmp;
return true;
}
return false;
}
}
}
return 0;
}