This is my first stackoverflow post.
I am doing the CS50 course on edx and currently I am stuck at problem set 3. I am implementing the game of fifteen. The init() function initizalizes the board and the draw function should draw it but there is a problem.
The draw function does not get the values from the init() function. I experimented with it, in the init() function the values are correct, but in the draw function all of them are 0's.
What's the problem?
/**
* Initializes the game's board with tiles numbered 1 through d*d - 1
* (i.e., fills 2D array with values but does not actually print them).
*/
void init(void)
{
//initializing the board
int board[d][d];
int x = (d*d) -1;
//this loop goes trough each row
for(int i = 0; i < d; i++){
//this goes trough each column
for(int j = 0; j < d ; j++){
//this condition handles the case of swapping the 2 and 1 when the grid is even
if( (d % 2) == 0 && (x == 2) ){
//assigning the number 1
board[i][j] = x-1;
//going to the next column
j++;
//assigning the number 2
board[i][j] = x;
//setting the x = 0 so the loop can end
x=0;
}
//this happens if the above conditions are not met
else {
//assigning the value to the grid
board[i][j]= x;
//decrementing the value
x--;
}
//ending the loop
if(x == 0){
break;
}
}
//ending the loop after the last tile is initialized
if(x == 0){
break;
}
}
}
/**
* 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("%2i", board[i][j]);
} else {
printf("_");
}
}
printf("\n");
}