How do I keep track of the blank space positions (i and j) without having to search for it again and again everytime. In the tips, the girl said maybe with a variable?

Because my game is almost finished, but its looking for the blank space every time with a loop, just like for the tile.


Make variables blank_row and blank_col global. Their initial position is board[d][d]. Now every time you swap the adjacent tile with blank tile, just update the location of blank tile with the location of the tile to be swapped.


I just had two global variables, blank_row and blank_col, whenever I went to move a number I checked first if the row it was on = blank_row and then if it was either blank_col + 1 or blank_col -1, if this was false then I did the same but seeing if it was first = blank_col and then + 1 or -1 blank_row.

If either expression was true then I just swapped the blank for the number. If false, nothing.

  • But, You had to get this blank_row and blank_col every play through a loop? If yes, thats what I`m saying, you alwas need to search for the blank positions, or is there a better way of doind this? – Joao Turolla Jun 17 '14 at 14:54
  • Sorry I should have mentioned (now edited) that I defined these as global variables along with the array that holds the board, was just a whole lot easier as that means they are available throughout the program rather than just within the move loop, you could use pointers but I don't think they were covered by Pset3. – nickgilling Jun 17 '14 at 18:30

Using global variables is a good idea! You can declare global variables before main() like that:

In case of a global int variable

// pre-processor directives
#include <stdio.h>

// prototypes
void foo();

// global variables
int g;

int main(void)
    // access it from main()
    g = 10;

void foo()
    // or from any other function
    printf("%d\n", g);

You may either use two global variables (one for the blank tile's row for its column). Or you may use a global int array of size 2.

You must log in to answer this question.

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