I've tested my init() and draw() together in a separate file, and it works exactly as it should. However, when I try implementing these functions within the fifteen distribution code, draw() prints out all underscores (which is the symbol I'm using the represent an empty space). I should mention that it does print out the correct number of spaces, just not with the correct values!

Any tips to help figure out what might be causing this?

1 Answer 1


"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 the array contents as a list. (You might need to insert a sleep command to see it, because of the clear() function. ) Running in in the debugger will also let you view the contents. Also, is it ALWAYS doing this, or does it print the initial array correctly? Does it only do this after a move? That should give you an idea where to look.

As a side note, look at the following:

            int swap2 = board[i][j];
            swap2 = '_';

The assignment of the board element to swap2 is unnecessary. You could simply do this: int swap2 = '_'; In fact, you could do it as a char instead of an int (and make the appropriate change in the printf() ).

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thanks for the help, Cliff. I discovered that the problem was occurring because I was attempting to declare my "board" array within the specific functions (not realizing that it was already a global variable). I'm not entirely sure why this was causing it to print all underscores (zeroes), but I'm glad to have the issue resolved!
    – klmo8
    Commented Feb 15, 2016 at 22:47
  • That's easy. You created a shadow array. A shadow var or array is one created locally with the same name as a global (or one that is outside the current curly braces, i.e., at a higher scope level). The local var will take precedence over the global. You created a local shadow and populated it. When you exited the function, it went out of scope and vanished, leaving the global array. While you should never depend on the contents of an uninitialized var or array, the global array contained all zeros. That's where it came from.
    – Cliff B
    Commented Feb 15, 2016 at 22:56
  • That makes sense. Cheers!
    – klmo8
    Commented Feb 15, 2016 at 23:26

You must log in to answer this question.

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