0

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;

}

0

It looks like you're having the same issues I had. You should compare the addresses of the X and Y position of blank tile (instead of just the values), against the tiles adjacent to the tile the user requested to move. The reason for this is because "outside" the board is garbage values that could be i/j +/-1 and that would cause unwanted behavior.

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.

2
  • Thanks! But I didn't catch that. Could you explain the problem with the pointers to me again? – Lily Sep 23 '16 at 6:00
  • 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .