I have tried to do this, but it has only resulted in a segmentation fault.

1 Answer 1


Simply put, if tile is in board[i][j], you need to check the positions directly above, below, and on either side for 0. In other words, board[i+1][j], board[i-1][j], board[i][j+1], board[i][j-1].

But since you're saying you are getting seg faults, I'm betting you already do this.

There's another part to this. Along with checking these array elements, you also have to make sure that those indexes are not out of range. In other words, i+1 or j+1 must be < d and i-1 and j-1 must be >0. If you don't make these checks, you could be swapping with data outside of the array and corrupting data. It's an error, but it often won't throw an error and/or terminate the program.

This is probably a lot to do with your other question. As a side note, you can't simply locate the tile to move, locate the 0, and swap them. This would allow the player to swap opposite corners or any two non-adjacent tiles. These are invalid moves.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Hi, Im sorry, it was long ago, but is there any point in doing this checks?I mean you swaping tile with blanck space, since both are inside range, there is no chance you may get a seg fault. I mean I did it this way and it works just as it supposed to. Am I wrong somewhere?
    – Garve Worm
    Commented Sep 1, 2016 at 14:14

You must log in to answer this question.

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