0

I tried to initialize the positions of the blank tile, blanki and blankj, inside the move function but they would keep resetting to (d-1). Now, I initialize them in the init function but the terminal keeps returning "unused variable" error. Also, blanktileis not initializing.

Does anyone know how to solve this problem? Thanks.

void init(void)
{
    //initialize blanki and blankj
    blanki=d-1;
    blankj=d-1;

    int number = d*d-1;
    for(int i=0; i<d; i++)
    {
        for(int j=0; j<d; j++)
        {
            board[i][j]=number;
            number = number - 1;
        }
    }
    //swap last two integers if odd tiles
    if(d%2==0)
    {
     int value = board[d-1][d-2];
     board[d-1][d-2]=board[d-1][d-3];
     board[d-1][d-3]=value;
    }
}

void draw(void)
{
    for(int i=0; i<d; i++)
    {
        for(int j=0; j<d; j++)
        {
            if(j==d-1)
            {
                printf("[ ]");
            }
            else
            {
                printf("%2d ",board[i][j]);
            }
        }
        printf("\n");
    }    
}

bool move(int tile)
{
    for(int i=0; i<d; i++)
    {
        for(int j=0; j<d; j++)
        {
            //Get i and j
            if(board[i][j]==tile)
            {
                //if i and j are one space from blank tile
                if(((abs(i-blanki))==1 && (j==blankj))
                || ((abs(j-blankj))==1 && (i==blanki)))
                {
                    //swap blank tile position with tile
                    blanktile = board[blanki][blankj];
                    board[blanki][blankj] = tile;
                    board[i][j] = blanktile;
                    //remember the new positions of blank tile
                    blanki = i;
                    blankj = j;
                    return true;
                }   
            }
        }
    }    
    return false;
}
1
 blanki=d-1;
 blankj=d-1;

Both variables are only accessible in init (). I think I understand your idea, but not successful, the best way to do this would be to declare two global variables indicating that:

 blanki = d -1;
 blankj = d -1;
 board[blanki][blankj] = board[d - 1][d - 1];
3
  • MARS is right. The issue of scope. A variable declared inside of a function (including main) is only available inside of that function. Only a global variable declared outside of all functions, like board[][] in this case, is available across all functions. Globals should be used sparingly and only when needed, such as access across functions when pass-by-copy isn't an option.
    – Cliff B
    Aug 20 '16 at 20:14
  • I tried to declare the global variables blanki and blankj but now there is the error: initializer element is not a compile-time constant. Any idea why this is happening?
    – elskies
    Aug 26 '16 at 15:11
  • I am curious because I have exactly the same problem as earyl! Also get erro 'initializer element is nog a compile-time constant'. Can someone enlighten me/us?
    – Maple
    Mar 22 '17 at 17:50

You must log in to answer this question.

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