Could someone explain me why my superb idea isn't working? Okay, perhaps it isn't that superb but it was my original own idea.

In pset3 fifteen game when seeing if the game is won, I first created a winning array then I went through in a for loop the board:

if (board[i][j] == winning_board[i][j])

then if counter == number of numbers on board return true.

But for some reason the comparison of arrays doesn't work? When I changed the logic wee bit; counted something else, my program worked.

Is there something special when comparing array values?


Actually, that is exactly what I did and it works. I did have trouble at first because the winning board array that I created was faulty, so you need to make sure that what your comparing is valid. Also, the board for even and odds might be different if you initialize the array with the game board.

  • Good to know. I thought that perhaps this is an address issue then and it would compare the actual addresses of the arrays instead of values so I moved forward from this. – Agetribe Jul 15 '14 at 8:07

Try to see the pattern in this board(which is correct for 3x3 board):

8 7 6
5 4 3
2 1 _

What you can simply observe is that the tiles in the board start from d2 - 1 and decrease by 1 until the value in array is down to 1.

To check this, the simple code would be:

int req_num == d*d - 1;
for(int x = 0; x < d;x++)
    for(int y = 0; y < d;y++)
       if ( board[x][y] != req_num )
           return 1;
return 0;

Happy Coding!

  • Thank you for this. It isn't quite what I asked. Like I said, I got the code working in the end using a different approach. What I don't understand is that why doesn't this work: 1. Create a winning board after getting d. 2. After every move count the number of correct board places: loop and compare board[i][j] == winning_board[i][j] 3. If counted amount d*d-1 then true else false. – Agetribe Jul 5 '14 at 6:34
  1. At the end of the loop, are you maybe comparing counter to the total number of tiles on the board, or the total number of places? There are row * col places on the board, but (row * col) - 1 tiles. This would be a so called "off-by-one" error. Use GDB or printf to inspect the value of counter, and whatever it is being compared to. Are the values the same?

  2. Possibly the blank 'space' value is not what you expect it to be. Use GDB or printf to look at the values in both arrays each time the if statement executes. Are all the values identical?

You must log in to answer this question.

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