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

/**
 * Prints the board in its current state.
 */
void draw(void)
{
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            if (board[i][j] == 0)
            {
                printf(" ");
            }
            else
            printf("%i",board[i][j]);
        }
    }
    printf("\n");
}

When I run the program, the board is not 2d, but 1d.

1 Answer 1

1

Looks like init isn't working. I'm just guessing here because you didn't include many details about what the problem was.

Look at your for loop setups:

for (int i = d - 1; i < d; i++)

Say that d=3. Initialization sets i=2. That means you make exactly one pass through the loop. On the next pass, i=d, so the test fails and the loop exits. Same with the j loop. Why didn't you initialize i and j at 0?

Didn't look any further since this was so severe an issue.

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

9
  • My apologies, I've set i and j to 0. My problem is that the board does initialize correctly. When I run ./fifteen 4 I get 151413121110987654312 and ./fifteen 3, 87654321.
    – Timmy Tran
    Commented Mar 18, 2016 at 5:40
  • I'm confused. If the board does initialize correctly, then why is it a problem? And if that isn't the problem, what is?
    – Cliff B
    Commented Mar 18, 2016 at 5:45
  • Does not, my apologies.
    – Timmy Tran
    Commented Mar 18, 2016 at 15:26
  • Again, confused. The order that you gave for the tiles (left to right, top to bottom), is correct. Exactly what is not initializing correctly? Please edit your question and add examples and description. Is it that the board isn't initializing correctly, or isn't being drawn correctly?
    – Cliff B
    Commented Mar 18, 2016 at 17:00
  • Question updated.
    – Timmy Tran
    Commented Mar 19, 2016 at 1:11

You must log in to answer this question.

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