# Game of Fifteen Draw function outputting only zeros

I am currently trying to solve a bug that prevents me from printing my wanted values. When I initialize the `board` and try to assign the value it appears to assign only zeros from the for loops even though the values of `n` and `counter` are right. It is either an error in init or the draw function isn't getting the proper values from the `board[d][d]` when I print each current value in `board[i][j]`, it prints zeros. What am I doing wrong? Please help, thank you in advance.

``````void init(void)
{
// grid with pre-chosen dimensions (d)
int board[d][d];
// the number of tiles in that grid
int n = d*d - 1;
// the whitespace in the grid
int blank_space = 0;
// holds the value to be subtracted from n to make a board with integers in descending order
int counter = 0;
// holds the place for swapping the integers 2 and 1 in the case of d being even
int place_holder = 0;
// sets each tile to a value
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
board[i][j] = n - counter;
counter++;
// if current tile is the last, set it to 0
if (j == d-1 && i == d-1)
{
board[i][j] = blank_space;
}
}
}
// if d is even
if (d % 2 == 0)
{
// swap the 2 and 1 integers
board[d-1][d-3] = place_holder;
board[d-1][d-3] = board[d-1][d-2];
board[d-1][d-2] = place_holder;
}
}

/**
* Prints the board in its current state.
*/
void draw(void)
{
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
// last character print blankspace
if (j == d-1 && i == d-1)
{
printf(" _");
}
else
{
// print two numbers for 2-digit numbers, 1 for 1-digit numbers
printf("%2i", board[i][j]);
}
}
// print new line
printf("\n");
}
}
``````

In `init`, do not declare `board` using `int board[d][d];`. This variable is already declared globally, so that all functions can access it.

By declaring your own variable, you shadow the global one (you cannot access the global one because it has the same name as the local variable), and all the numbers you write to the local array die with leaving the scope it's declared in.

I don't see the need for

``````            // if current tile is the last, set it to 0
if (j == d-1 && i == d-1)
{
board[i][j] = blank_space;
}
``````

as it shouldn't do anything (replace 0 with 0).

You however made another small mistake in the `d % 2 == 0` part. You code adds another zero to the board as you messed up the first line of the swap (would have to be `place_holder = board[d-1][d-3];`, not the other way around). But even a simple

``````        board[d-1][d-2] = 2;
board[d-1][d-3] = 1;
``````

would be enough, as you know which numbers are there.

In `draw`, it shouldn't be `if (j == d-1 && i == d-1)` but `if (board[i][j] == 0)`, as the empty tile will move and not always be in the lower right.