0

Please give me some suggestions, I think I'm reasonably close:

bool move(int tile)
{
    int temp; 
    int didSwap;
    int blankRow;
    int blankCol;

    // search for blank tile

   for(int row = 0; row < d; row++)
{
    for(int col = 0; col < d; col++)
    {
        if(board[row][col] == 0)
        {
            blankRow = row;
            blankCol = col;
        }
    }
}

// check that tile is not too big or too small

if(tile > d * d - 1 || tile < 1)
{
    return false;
}

// search for blank tile

for(int row = 0; row < d; row++)
{
    didSwap = 0;
    for(int col = 0; col < d; col++)
    {
        if(tile == board[blankRow+1][blankCol] && tile != board[d+1][d])
        {
            temp = board[blankRow][blankCol];
            board[blankRow][blankCol] = tile;
            tile = temp;
            didSwap = 1;
            return true;
        }
        else if(tile == board[blankRow-1][blankCol] && tile != board[d-1][d])
        {
            temp = board[blankRow][blankCol];
            board[blankRow][blankCol] = tile;
            tile = temp;
            didSwap = 1;
            return true;
        }
        else if(tile == board[blankRow][blankCol+1] && tile != board[d][d+1])
        {
            temp = board[blankRow][blankCol];
            board[blankRow][blankCol] = tile;
            tile = temp;
            didSwap = 1;
            return true;
        }
        else if(tile == board[blankRow][blankCol-1] && tile != board[d][d+1])
        {
            temp = board[blankRow][blankCol];
            board[blankRow][blankCol] = tile;
            tile = temp;
            didSwap = 1;
            return true;
        }
        else
        {
            return false;
        }
    }
}
return false;
}

I can't work out for the life of me how this is not working. Is it something to do with the swap?

1

There are a few problems here. First, the test conditions all have a problem. Look at the first one, for example:

if(tile == board[blankRow+1][blankCol] && tile != board[d+1][d])

As indexes for board[][], anything greater than d-1 is invlaid and will reference memory outside of the array or in another part of the array, depending on what the values of the index are, how the array is stored in memory, and how other vars are stored around it. It won't generate an error, but it will try to look at where it thinks this element in the array is. That means that it's going to grab some random or garbage data from somewhere in memory and make the test, so every one of these tests will have totally unpredictable results, which can result in both false positives and false negatives.

Next, the content of the inner loop is a series of chained if/elseif/else clauses. Each if or elseif contains a return (which is probably correct), but the last else is an unconditional return false. That means that the loop can only execute once. It's either going to pass a test and return true, or fail all of them and return false without ever testing a second element in the board[][] array.

Finally, while didswap may be a flag to detect a swap, it serves no purpose because nothing is ever done with it. It can be removed completely from the code with no effect.

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

You must log in to answer this question.

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