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");
}
}