0

I've been stuck on the move function for a really long time and I can just not figure out how to do it. I've tried to get help from other people and I really just don't understand any of it. Please help me get work on this. My whole code is:

https://pastebin.com/aZ1XGkD9

1 Answer 1

0

Set blank_row and blank_col to d-1, not -1.

There's no need for global variables tile_row and tile_col.

In move (I'm referring to the commented-out version, as it does not have this extra bloat), do not re-declare tile. Instead, check whether the tile you found is neighbouring the blank tile, and if so, swap them, like

            // on tile to swap, plus it's neighbouring the empty tile
            if (board[i][j] == tile && abs(blank_row - i) + abs(blank_col - j) == 1)
            {
                // swap is simpler if you know both values,
                // no temp variable required
                board[i][j] = 0;
                board[blank_row][blank_col] = tile;
                // update coordinates of blank tile
                blank_row = i;
                blank_col = j;
                return true;
            }

This is using abs from math.h, but you could totally use something like (blank_row - i) * (blank_row - i) + (blank_col - j) * (blank_col - j) == 1 instead (both result in 1 in the case that one coordinate is equal while the other differs by exactly one).

Don't forget to return false if no swap has happened.

You must log in to answer this question.

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