My init() code works beautifully for grids of 3, 4, or 5, but chokes with larger grid of 6, 7, 8, and 9. It looks almost like an integer overflow or floating point imprecision problem, but I can't see anything in the code that would cause an issue.

Any thoughts? Here's my code:

int grid[d][d], x;
x = (d*d) - 1;                     // set value of first tile to highest number in grid
for (int i=0; i<d; i++)  {         // iterate rows
   for (int j=0; j<d; j++)  {      // iterate columns
        grid[i][j] = x;            // assign value for each tile and decrement for next tile

Or maybe it's a problem with the draw() function? Here's my code for that:

int i, j, grid[d][d];
for (i=0; i<d; i++)  {
    for (j=0; j<d; j++)  {
        if (grid[i][j] == 0)  {      // if tile number is 0, print '__' instead
        } else {
            printf("%2i\t", grid[i][j]);
printf("\n\n\n");                    // tab and print extra lines for better grid spacing

Hm. Looks like I redefined the 2D array, which had already been done earlier in the code. Fixed that and it runs great now.

You must log in to answer this question.

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