I don't understand why the second part of this code doesn't get executed. It within the init function and I expect it to run after the loop. The last tile still returns 0 and with odd number of tiles the swap does't occur.

 void init(void)
        int tileNumber = d*d-1; // Set as highest number on the board

        for (int i = 0; i < d ; i++) // Set row
            for  (int j = 0; j < d; j++) // Set collum
                board [i][j] = tileNumber;

        // Set blank space to 99
        board [d][d] = 99;

        //If odd number of tiles, swap the tiles containing 1 and 2

        if ((d == 4) || (d == 6) || (d ==8))
            board [d][d-1] = 2;
            board [d][d-2] = 1;


1 Answer 1


It probably is executing, but not the way you think. You're lucky it isn't seg faulting. (Well, actually, a seg fault would be better. )

Think about the array indexes that you're using. Remember that the array is a 2-d array with d elements in each dimension. More importantly, remember that the dimension indexes start at 0, not 1, so the largest valid value of each index is d-1, not d.

So what happens when you try to do something with board[d][d]? Or board[d][d-1]? You can think about how to fix that, while I wait for the code to fail with a tile == 99. But that's another story! ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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