Skip to main content
1 of 2
Blauelf
  • 21k
  • 2
  • 13
  • 22

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, 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.

Blauelf
  • 21k
  • 2
  • 13
  • 22