I have been changing and trying a bunch of stuff but it won't work. Could any of you see what is wrong with my code? I already have read a lot of answers for the same function but I still can't find the solution for my code. Thanks in advance!
bool move(int tile)
{
int swap;
// search tile which must be moved
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
// swap tile with blank tile
if (tile == board[i][j])
{
// Prevents moving a tile above the top of the 2D-array
if (i > 0)
{
if (board[i - 1][j] == 0)
{
swap = tile;
board[i][j] = board[i - 1][j];
board[i - 1][j] = swap;
return true;
}
}
// Prevents moving a tile left of the 2D-array
else if (j > 0)
{
if (board[i][j - 1] == 0)
{
swap = tile;
board[i][j] = board[i][j - 1];
board[i][j - 1] = swap;
return true;
}
}
// Prevents moving a tile under the 2D-array
else if (i < d)
{
if (board[i + 1][j] == 0)
{
swap = tile;
board[i][j] = board[i + 1][j];
board[i + 1][j] = swap;
return true;
}
}
// Prevents moving a tile right of the 2D-array
else if (j < d)
{
if (board[i][j + 1] == 0)
{
swap = tile;
board[i][j] = board[i][j + 1];
board[i][j + 1] = swap;
return true;
}
}
}
}
}
return false;
}