0

This is my code for identifying the blank tile:

bool move(int tile)
{
    int temp; 
    int didSwap;
    int blankTile = 0;


    // search for blank tile

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

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

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


    // Search if tile is adjacent to blank tile. If so, swap it.

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

My thinking is now that I have attributed the value of '0' which is my blank tile to variable, 'blankTile', that I will now be able to check if 'tile' is adjacent to it. If so, I will be able to swap the tiles.

Is that right?

For some reason, I am getting the error that ' error: subscripted value is not an array, pointer, or vector' every time I write: 'blankTile[row+1][col]' or similar.

Any ideas?

Thanks!

1 Answer 1

0

From what I can tell, it seems that you are using row and col as your counter variables. This is fine, but when you say that board[row][col] = blankTile you aren't saving the values of row and col, rather you are accessing the value inside of that index; that is, 0. Essentially you are saying that blankTile is 0, something that you already know.

I recommend creating two new variables and if board[row][col] == 0, then save the current values of row and col into those variables.

You must log in to answer this question.

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