My move function is not properly working. It will sometimes move numbers far away from where the blank space is, and also sometimes causes other numbers that should have been switched with the blank space to become blank spaces themselves. Here is my code:
bool move(int tile)
{
for (int i = d; i >= 1; i--)
{
for (int j = d; j >= 1; j--)
{
if(board[i][j] == tile)
{
if(board[i-1][j] == 0 && i-1 >= 0)
{
int tmp = board[i][j];
board[i][j] = board[i - 1][j];
board[i - 1][j] = tmp;
return true;
}
else if(board[i+1][j] == 0 && i+1 < d)
{
int tmp = board[i][j];
board[i][j] = board[i + 1][j];
board[i + 1][j] = tmp;
return true;
}
else if(board[i][j-1] == 0 && j-1 >= 0)
{
int tmp = board[i][j];
board[i][j] = board[i][j-1];
board[i][j-1] = tmp;
return true;
}
else if(board[i][j+1] == 0 && j+1 < d)
{
int tmp = board[i][j];
board[i][j] = board[i][j+1];
board[i][j+1] = tmp;
return true;
}
}
}
}
return false;
}
Thanks in advance.