When you do:
for(int i = 0; i < d; i++)
{
for (int j = 0; j < d; ++j)
{
...
/* Swapping ALWAYS the same tiles, since 'd' is a fixed value */
int t = board[d-1][d-2];
board[d-1][d-2] = board[d-1][d-3];
board[d-1][d-3] = t;
...
}
printf("\n");
}
}
The general idea is close to right (for the "swapping tiles" algorithm): grabbing a certain tile, store it in a temp value, and then swap two contiguous values. The problem is that, never mind the for
loop, you're always accesing the same positions in the board[][]
array, because you're accesing it with d
, which is a fixed value.
Besides, you're actually performing these two steps within the same loop:
- Filling the board with tiles
- Swapping values to be moved
Of course, first times through the loop you're trying to swap values that have not been initialized yet. Luckily enough, you get zero
, but in theory you could get any value at all (remember those ?
in the lectures?)
You should consider splitting those steps in two different functions, like:
I.e.: your board should be entirely initialized before your first attempt to move a tile.
Perhaps you should start by laying out your algorithm in pseudocode, and better understand how the program will work, and only then translate that to actual 'C' code.