I am starting to do Game of Fifteen [pset3] today. I've just written init() function. I want to make sure it works before forging ahead to draw() function. Please, take a look at my implementation.

 * Initializes the game's board with tiles numbered 1 through d*d - 1
 * (i.e., fills 2D array with values but does not actually print them).  
void init(void)
    for (int i = 0, k = d; i < d, k < d; i++, k *= 2) // where i is row, k is the connection between first row and i'th row
        for (int j = 0; j < d; j++) // where j is column
            board[i][j] = ((d * d) - (j + 1)) - k;

Is that true? Do I still need to check odd or even number of tiles?



When you wrote

k = d and k < d and k *= 2,

I think you meant

k = 0 and k < d * d and k += d

(but as you're checking i, you wouldn't have to check k in the loop condition)


Yes, you need to swap the 1 and 2 tiles for even dimension, otherwise the game has no solution.

You must log in to answer this question.

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