I'm not getting whats going wrong with my
draw functions. When I trace the program through gdb I'm getting the 0 values for tile locations.
In the draw function, you don't need the whole
if else thing. Just leave the
printf after the
else and the one that moves the cursor to the next line. Done.
In the move function, you are pretty close:
- In the four
if()statements, where you check the bounds, move the bound check before the tile check. Eg, instead of this:
if(board[i-1][j] == symbol && (i-1) >= 0)
you want this:
if((i-1) >= 0 && board[i-1][j] == symbol)
This way the program will first check the bounds condition
(i-1) >= 0 and if it is false, it will not try to check
board[i-1][j] == symbol.
The way you wrote it, the program will first try to check
board[i-1][j] == symbol and if
0, kaboom! Segmentation fault... :)
So, just change the order in all four
- You don't need the
holdvariable. I see several people now trying to implement the swap function. But it is not necessary here. Once you are inside one of the four
if()statements, you know two things:
board[i][j] contains the
tile value; and (b)
board[i-1][j] (in the case of the first
if()) contains the
And what do we want them to be? We want them to be swapped, right? So we simply say:
board[i][j] = symbol; and
board[i-1][j] = tile;
The reason we don't need
temp, as in another example) variable is that
tile is already holding the value that we want to swap, so we don't need to store it in another variable.
So, now, just extrapolate the above for the other three
if() statements and you should be good to go.
And please don't forget to at least up vote the answer, if it helped you. Or mark it as accepted, if answered your question.