Skip to main content
added 83 characters in body
Source Link
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 (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.

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.

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.

Source Link
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.