0

Been killing my head for too long with this.

I get that we check around the tile when we find it in our loop, but then what? I understand completely, conceptually... yes, check around the tile for the underscore, then return true. But how does one do that? Here is what I have, which is wrong. All moves show up as illegal moves, but not sure why.

If anyone has a very targeted answer, you would be my hero. I do really understand everything conceptually, I just can't visualize how this would look in code.

Thanks very much. Here is my checking all sides portion:

char underscore = '_';

    //Linear search for tile from user input

        i loop with d as limit
        {
            j loop with d as limit
            {   
                //Finds the tile
                if (board[i][j] == tile)
                { 
                    //Checks around tile for underscore, only if tile found
                    if (board[i + 1][j] == underscore || 
                        board[i - 1][j] == underscore ||
                        board[i][j + 1] == underscore ||
                        board[i][j - 1] == underscore)
                   {
                       hold = tile;
                       tile = underscore;
                       underscore = hold;

                       return true;
                   }
                }
            }
        }
1
underscore = board[i][j];

You're assigning the value that is stored in that board position to an integer called underscore. At this point, underscore equals zero. This is the same issue you struck the other day with your init() swapping.

I think you're trying to somehow capture the position of the tile that has a value of zero? If that's the case, then you need to be remembering the i and j values of that tile.

Let's say that the blank (the zero) is at board[1][2]. When you come to find out if your tile is adjacent to the blank, you need to be comparing to board[1][2] (not to an integer that happens to be set to 0.) Perhaps you need two integers, called blankX and blankY that store the position? Then you can use board[blankX][blankY] in your later checks for adjacency.

Also, keep in mind that you don't want to check outside the board.

If the person chooses the tile at position board[3][3] (on a 4x4 board), you don't want to be checking board[4][3] or board[3][4] because neither of those actually exist.

As an example, let's say your board looks like this:

8 _ 6
5 7 3
2 4 1

Your first loops will find that board[0][1] is the blank.

Now, the user enters 6. Can it move?

6 is board[0][2] Is board[0][2] next to board[0][1]? Yes, so we want to set board[0][2] = 0 and board[0][1] = 6 and then return true.

Now, the board is redrawn:

8 6 _
5 7 3
2 4 1

Now, the blank is at board[0][2].

Let's say the user enters 1 Can it move?

1 is board[2][2] Is that adjacent to the blank? No, so return false.


EDITS BELOW TO HELP ANSWER QUESTIONS IN COMMENTS


You're doing this with your if statement:

if (board[i < d - 2][j] && board[i + 1][j] == underscore) 

Let's say board looks like this (d = 3):

8 6 _
5 7 3
2 4 1

and you are first time through the loop so i = 0 and j = 0

if (board[i < d - 2][j] && board[i + 1][j] == underscore) 
if (board[0 < 3 - 2][0] && board[0+1][0] == underscore)
           0 < 1 is true, which means it will be 1
if (board[1][0] && board[1][0] == underscore)

now evaluate the board values

if (5 && 5 == underscore)

What are you expecting that to be checking? Let's take this to private reddit. thanks.

| improve this answer | |
  • Thanks, I just really don't understand. I thought we were supposed to swap the values of the tile, not the positions. So I assigned underscore to the position where board[i][j] = 0, so that I could always search for the zero value and then see what the positions around it look like. But even with that logic, I don't how looking at a position is supposed to tell us what the value is.... So confused! – Azurespot Sep 4 '14 at 7:14
  • Mr. curious, if I may ask, how do you know that naming board[i][j]==0 to the int underscore is merely assigning a zero instead of marking that position on the board as where the blank space is? I think my confusion is around that. – Azurespot Sep 4 '14 at 7:49
  • 1
    The statement 'underscore = board[i][j];' by definition says: go get the integer that is stored at the memory location of board[i][j] and set the value of underscore to that value. So if board[i][j] is 0, it's the equivalent of saying "store 0 in the block of memory that I've called underscore" – curiouskiwi Sep 4 '14 at 7:55
  • 1
    You can't know what the spots outside the board are filled with, so you can't rely on them not matching. Either you have to fill all those spots with some value that you then ignore, or you have to not check those spots. Remember, your global board was declared as board[MAX][MAX] and C doesn't automatically zero out an array when it's declared (like Java does) so either you have to do that or deal with them in some other way (by not checking them, for example). – curiouskiwi Sep 4 '14 at 21:42
  • 1
    Stackexchange isn't really designed for back and forth discussion. Are you on reddit too? Can you send me a private message there reddit.com/u/delipity and I can help better. – curiouskiwi Sep 4 '14 at 23:37

You must log in to answer this question.

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