0
bool move(int tile)
{
    int hold;
    for(int i=0 ; i < d ; i++)
    {
        for(int j=0; j<d; j++)
        {
            if(board[i][j] == tile)
            {                
                if(board[i-1][j] == symbol ||
                   board[i+1][j] == symbol ||
                   board[i][j+1] == symbol || 
                   board[i][j-1] == symbol )
                {
                    printf("\n%d\n", tile);
                    hold = tile;
                    tile = symbol;
                    symbol = hold;
                    return true;                    
                }               
            }        
        }
    }
    return false;
}

Its my code for move function... When I tried to move tile 1 which is beside to blank tile its showing illegal move... But when I checked it through gdb control is moving till the second if statement but it will not check all the 4 conditions what i mean is its coming out of the if loop returning false... I'm not understanding why it is happening?? Anybody Help me!!! Thanks in advance...

2
  • what's the symbol defined as? and what do you store in the empty tile?
    – Hedgehog
    Oct 27, 2014 at 21:06
  • symbol is defined as a global character storing '' in it. yeah in empty tile i have stored '' . Its working well in the draw() function
    – Latha S S
    Oct 28, 2014 at 7:16

1 Answer 1

1

You are storing your tile numbers in the board array, right? So you need to change the values in the board array. When you write:

hold = tile;
tile = symbol;
symbol = hold;

Does this change any values in the board array?... That's right, it don't.

You should be doing something like:

// board location that has the tile value
// becomes the empty value (symbol)
board[i][j] = symbol;

and

// board location that has the empty value (symbol)
// becomes the tile value
board[i-1][j] = tile;

plus whatever the 3 other possibilities are.

Caveat emptor: when i is 0, then board[i-1][j] will translate into board[-1][0]... Thou shalt not access elements outside the boundaries of the array.

1
  • Thank u so much Hedgehog:-) Finally t worked!!! The mistake i did is i haven't stored _ in the array so its printing 0 after swapping...
    – Latha S S
    Oct 29, 2014 at 5:50

You must log in to answer this question.

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