0

I'm not getting whats going wrong with my move and draw functions. When I trace the program through gdb I'm getting the 0 values for tile locations.

4
  • are you sure about if(board[d][d] == board[i][j])? i and j will never be equal to d. Also don't post whole solution, post only specific section of the problem, its spoon feeding for others.
    – sinister
    Oct 28 '14 at 15:24
  • Yes so only its outputting _ tile when I run draw() function!!!. hmm ok How to send private messages here? so that I can remove this post...
    – Latha S S
    Oct 28 '14 at 17:26
  • Why are you posting new question? You can just edit your original one and post new code there. Good on adding checks for array boundaries. Re-read my answer to your original question.
    – Hedgehog
    Oct 28 '14 at 17:31
  • 1
    stackexchange does not have feature of one to one chat. If you want guidance that requires displaying of too much of your code, then use social networking sites like facebook to send private messages.
    – sinister
    Oct 29 '14 at 0:14
0

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:

  1. 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 i is 0, kaboom! Segmentation fault... :)

So, just change the order in all four if() statements.

  1. You don't need the hold variable. 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:

(a) board[i][j] contains the tile value; and (b) board[i-1][j] (in the case of the first if()) contains the symbol value.

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 hold (or 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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .