# Problem with move(), pset3

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;
``````

}

Do mind that you can tremendously improve the design of your code by generalizing here. You can check all four corners inside a single `if` condition by use of the `&&` and `||` operators. You could also make a `swap()` function and just pass that the addresses of the tiles you want to move.
• I was a little wrong, you're not checking outside the board, since the board is [DIM_MAX][DIM_MAX] big, but you're only using d * d. So, `i+1` (or equivalent) is checking outside the board you are using (d * d) but still inside the declared `board`, here you might find the value you are looking for, so the move() proceeds to swap the two. Because of this you don't want to compare values, instead you compare adresses by use of `&`, which are guaranteed to be unique. Remember memory is not actually two-dimensional, you're just structuring it so visually. – kluvin Sep 23 '16 at 8:56