I don't know why my positions at 2 and 1 in my odd numbered tiles are not swapping. Here is a mix of pseudo code plus my swap conditional. See anything that is wrong? Thanks.

    // board, whereby board[i][j] represents row i and column j
    int board[MAX][MAX];

    // board's dimension
    int d; 

void init (void)  
    for loop
    print out whole board, in a grid, decrementing tile numbers

    // then swap 2 and 1, if total number of tiles is odd
    if ((((d * d) - 1) % 2) != 0)
           int hold = 0;
           int two_spot = board[d - 1][d - 3];
           int one_spot = board[d - 1][d - 2];

           hold = two_spot;
           two_spot = one_spot;
           one_spot = hold;

Don't you want to swap the two tiles? You are swapping 2 new integers that happen to hold the tile values.

Get rid of those 2 extra integers and simply use the tiles themselves in your swap code.

Let's say I wanted to swap 2 values:

int hold = firstValue;
firstValue = secondValue;
secondValue = hold;

In your case firstValue is board[d-1][d-3] so simply use that (and not a copy of it like you are currently doing) etc.

So, why can't you swap the integers one_spot and two_spot that you created? Here's why.

Think about your program's memory. You've declared a global array called board[][] and then populated the values in the array. That array lives in memory.

Now, you declare an integer called one_spot. That integer also lives in memory (at its own address). You then assign the value of one_spot to the value of board[d-3][d-2]. So if board[d-3][d-2] is 1, then one_spot will now equal 1. But it's still stored at its own place in memory. It has no connection to the board. Now you do the same with two_spot. Now you have 2 integers both stored in memory. If you swap them, you are swapping the values at those 2 addresses. The board array is untouched. This is why you need to swap the board[][] tiles themselves and not the copies you've made.

Later on in the course, you'll learn more about memory management and pointers and at that stage, this might become clearer.

In addition, looks like you are printing the board? Your init() function should only be setting the initial values of the tiles. The draw() function then prints the board to the screen.

| improve this answer | |
  • Thanks, but why does a copy matter? Maybe I am used to the rules in Java, where you can assign any value a variable and use that (since the value itself is kind of long). I did not think this was "illegal" in C... but you are saying it is? – Azurespot Sep 2 '14 at 18:14
  • You can make as many copies as you want (that's not illegal). But swapping copies around isn't going to make any difference to the actual values stored in the board. It's the value in the board that you want to swap, not a copy of that value. Your one-spot isn't pointing to the board. It's a new integer that just happens to have the same numeric value as what is stored in board[d-1][d-2]. Swapping one-spot with two-spot has no impact on the board itself. – curiouskiwi Sep 2 '14 at 20:18
  • Thanks for explaining that... yeah, that's not how it works in Java, so it's good to know. The copies in Java definitely count as the value itself. Well, thanks, I will try changing this and let you know! – Azurespot Sep 3 '14 at 0:17
  • 1
    You create the board in the init() function. You print it in the draw() function. I didn't notice earlier that you were printing it in the init function. init() should only create the board. – curiouskiwi Sep 3 '14 at 4:11
  • 1
    I updated my answer to add more help with the swap thing. – curiouskiwi Sep 3 '14 at 5:52

You must log in to answer this question.

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