0
void init(void)
{
    int start_tile = (d * d) - 1;
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            board[i][j] = start_tile;
            start_tile--;
            if ((d-1)% 2 != 0)
            {
                int swap = board[d-1][d-2];
                board[d-1][d-2] = board[d-1][d-3];
                board[d-1][d-3] = swap;
            }
        }
    }
}

When board is drawn when d = 4, 6, or 8, the result appears like so (using 4 as an example):

15 14 13 12 
11 10  9  8 
 7  6  5  4 
 3  _  1  _ 
Tile to move: 

Two questions: 1) What happened to the number 2? (2) Why is it looking at the array value [d - 1][d - 1]?

2

Actually, the swap shouldn't be inside any for loop. You only want to do the swap once. Putting it inside any loop does it multiple times. Since the swap only depends on the value of d, and nothing else, just do the swap after all of the loops.

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

| improve this answer | |
  • 1
    ... and if you were really alert, you'd realize that they don't need to be "swapped" at all. Just assign 1 when start_tile== 2 and 2 for 1. – Cliff B Sep 13 '16 at 5:34
1

Just figured it out! My if statement was nested incorrectly. It should not be in the inner for loop!

| improve this answer | |

You must log in to answer this question.

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