0

So I'm solving pset3 and there's a bug in part of my function that I just can't spot. My approach is search for the tile the first time, if there indeed is an adjacent blank tile, I'll save the coordinates and instead of linear searching for the tile everytime, I just check if the tile is adjacent to the blank tile. It goes wrong at the first move only if I try to move a tile that's above the blank tile. It creates two blanks instead of swapping.

  bool move(int tile)
{
  if(tile > d*d-1 || tile < 1)
    return false;

int tileRow;
int tileCol;

if(blankPosRow == -1) //blankRow and blankCol globally initialized as -1 
{   int i;
    int j;
    for(i = 0; i<d ; i++)
    {
        for( j = 0; j<d; j++)
        {
            if(board[i][j]==tile)
            {
                tileRow = i;
                tileCol = j;
                break;
            }
        }
    }



    if(board[tileRow][tileCol-1] == 0) //blank above the tile
    {
        board[tileRow][tileCol-1] = tile;
        board[tileRow][tileCol] = 0;
        blankPosRow = tileRow;
        blankPosCol = tileCol;
        return true;
    }

    if(board[tileRow][tileCol+1] == 0) //blank below the tile (BUG HERE)
    {
        board[tileRow][tileCol+1] = tile;
        board[tileRow][tileCol] = 0;
        blankPosRow = tileRow;
        blankPosCol = tileCol;
        return true;
    }

    if(board[tileRow-1][tileCol] == 0) //blank to the left of the tile
    {
        board[tileRow-1][tileCol] = tile;
        board[tileRow][tileCol] = 0;
        blankPosRow = tileRow;
        blankPosCol = tileCol;
        return true;
    }

    if(board[tileRow+1][tileCol] == 0) // blank to the right of the tile
    {
        board[tileRow+1][tileCol] = tile;
        board[tileRow][tileCol] = 0;
        blankPosRow = tileRow;
        blankPosCol = tileCol;
        return true;
    }

   return false; 

}

    if(board[blankPosRow][blankPosCol-1] == tile) //tile above blank
    {

        board[blankPosRow][blankPosCol] = tile;
        board[blankPosRow][blankPosCol-1] = 0;
        blankPosCol--;
        return true;
    }

    if(board[blankPosRow][blankPosCol+1] == tile) //tile below blank
    {
       board[blankPosRow][blankPosCol] = tile;
       board[blankPosRow][blankPosCol+1] = 0;
       blankPosCol++;
       return true; 
    }

    if(board[blankPosRow-1][blankPosCol] == tile) //tile to the left of blank
    {
        board[blankPosRow][blankPosCol] = tile;
        board[blankPosRow-1][blankPosCol] = 0;
        blankPosRow--;
        return true;  
    }

    if(board[blankPosRow+1][blankPosCol] == tile) //tile to the right of blank
    {
        board[blankPosRow][blankPosCol] = tile;
        board[blankPosRow+1][blankPosCol] = 0;
        blankPosRow++;
        return true;
    }



return false;

}

0

The code tests for tiles off the edge of the board. For example, when a tile is tested at board[x][d-1], it looks at board[x][(d-1) + 1], but that's d, and d is out of range. Same logic applies when an index is 0 and it looks at 0-1.

It needs to test to make sure the indexes don't go out of range.

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 .