I have problem with the move function from pset3, CS50. I think that the problem is in the corner tiles but I can't solve it. Thanks in advance!
/** * If tile borders empty space, moves tile and returns true, else * returns false. */
bool move(int tile)
{
int tmp;
for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
// up
if(i > 0)
{
if(board[i - 1][j] == 0 && i - 1 >= 0)
{
tmp = board[i - 1][j];
board[i - 1][j] = board[i][j];
board[i][j] = tmp;
return true;
}
}
// down
if(i < d - 1)
{
if(board[i + 1][j] == 0 && i + 1 < d)
{
tmp = board[i + 1][j];
board[i + 1][j] = board[i][j];
board[i][j] = tmp;
return true;
}
}
// left
if(j > 0)
{
if(board[i][j - 1] == 0 && j - 1 >= 0)
{
tmp = board[i][j - 1];
board[i][j - 1] = board[i][j];
board[i][j] = tmp;
return true;
}
}
// right
if(j < d - 1)
{
if(board[i][j + 1] == 0 && j + 1 < d)
{
tmp = board[i][j + 1];
board[i][j + 1] = board[i][j];
board[i][j] = tmp;
return true;
}
}
}
}
return false;
}