The board was already declared for you as a global array, so you should be setting the initial values in your init() and then drawing that array in your draw(). The draw() function is also called after every move. Your move() function will be updating the values in the array.
Notice here in the fifteen.c code we were given, near the top:
// board, whereby ...
You cannot check if number is even with if (d/2 == 0)
use % sign (remainder sign).
Number is even if you divide the number with 2 and remainder becomes 0.
if (d%2 == 0)
Use %2d so that each time you get space for two letters and \t tab for equal spacing.
and remove the ...
"Any tips to help figure out what might be causing this?"
If this draw() is printing out all underscores and the board layout is correct even though it is all underscores, it looks like the array must contain all zeros. The only way an underscore would print is when the value is 0. You should look at the contents of the board array. Maybe just print out ...
In your init function you need to assign values to each position in the board array. In the draw function, you use the values from your board array to print to the screen. Note that the integer array board[MAX][MAX] has already been declared as a global variable in the distributed code.
board is a global variable. When you redeclared it inside of the init() function, you created what is called a shadow variable. A shadow variable is a variable that is created with local scope that masks one with greater scope, such as a var outside a currently active loop or a global variable.
When the code exits init(), the local board array is ...
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'...
In move () you are reading out of bounds. For example On a 3x3 board if j is 0 you will read j-1. This goes for the whole perimeter of the board. You need to account for these bounds.
You also need to have a closer look at draw() when you move left and right. Think carefully about when you need to add a newline.
The swapping looks ok (at least in the first init function). The thing that is "not giving the expected results" is the draw function. The draw function should print the "current state" of the board array. This draw function is drawing the same board every time, based on the calculated value arr_len. And it doesn't swap (not should it!).
It really looks like screen artifacts. It could have to do with screen size. Did you by any chance have (optional) fun with "ANSI escape sequences"? FWIW the original clear function is:
printf("\033[%d;%dH", 0, 0);
I found I could add a printf (eg: printf("THE BOARD\n");) at the beginning of the draw function and it did not affect the ...
You might want to get rid of the first set of nested loops:
for(int i=0; i<d; i++)
for(int j=0; j<d; j++)
if(board[i][j] == 0)
blank = board[i][j];
Since this is just setting blank equal to 0 whenever it finds the 0 tile.
You can substitute that by:
blank = 0;
Or just by comparing against ...
8 7 6 5 3 1 0 -3 -6
It is what makes init () when d3, just have to take pen and paper and do some calculations.
for(int i = 0; i < d; i++)
int k = i + 1;
it leads to a wrong result.
We start with a control variable before for loop, that contains the desired value when the loop (i = 0, j = 0) starts this value can be k = d*d-1
The board array is a global variable. In both functions, the code is creating a shadow array called board.
By redeclaring the board array in each function, it is creating a local array that takes precedence, hiding the global array. This is not the same array as the global array. Once the function ends, the local ...
your init function is incorrect as noted @Matt Obert.
if (d%2 != 0)
if d = 4, for example, your function init will not doing nothing at all, since board [d-1] [d-2]; It not even initialized to a valid value.
int z = (d*d) - 1;
Nor is housed in the right place, at each iteration of i (suppose the loop has three iterations) you do z = 8, in ...