Can anyone help me point out the mistake I've made? The underscore just moved so wrong that I can't find the pattern.
1 Answer
First, please change board[i - 1][j] == 0 && i > 0
to i > 0 && board[i - 1][j] == 0
(same for the other lines), otherwise you risk segfaults, as you might dereference a pointer board[-1]
before you realize it is not there.
Then, your algorithm does not stop after swapping, so maybe moving the empty tile up or left does not work, as it's instantly swapped back?