# The tile doesnt swap or move when i use this code!

I have been stuck on this for two days now. Would really appreciate some fast help! every time i run this it and put in Tile to move:3 all the numbers on the board change can u give me ideas on how to solve this issue. Also if i make board[r][c]==tile in move() it says illegal move and when i run check50 it doesnt recognize it and just returns false thats why i had to change all my code. atleast now it doesnt say illegal move....please help me!!!

``````    void init(void)
{
// TODO
int board[d][d];
int t=d*d;
for(r=0;r<d;r++)
{
for(c=0;c<d;c++)
{
board[r][c]=t-1;
t=t-1;
}
}
if(d%2==0)
{
board[d-1][d-2]=2;
board[d-1][d-3]=1;
}
}

int br;
int bc;

void draw(void)
{
// TODO
int board[d][d];
for(r=0;r<d;r++)
{
for(c=0;c<d;c++)
{
if(board[r][c]==0)
{
printf("__");
}
else
{
printf("%2i  ",board[r][c]);
}

}
printf("\n");
}
}

bool move(int tile)
{
for(r=0;r<d;r++)
{
for(c=0;c<d;c++)
{
if(tile==board[r][c])
{
br=r;
bc=c;
}
}
}
if(board[br][bc+1]==0 &&  c+1<=d-1)
{
board[br][bc+1]=tile;
board[br][bc]=0;
return true;
}
else if(board[br][bc-1]==0 && c-1>=0)
{
board[br][bc-1]=tile;
board[br][bc]=0;
return true;
}
else if(board[br-1][bc]==0 && r-1>=0)
{
board[br-1][bc]=tile;
board[br][bc]=0;
return true;
}
else if(board[br-1][bc]==0 && r+1<=d-1)
{
board[br-1][bc]=tile;
board[br][bc]=0;
return true;
}

return false;// TODO
}
``````

Why is this line, `board[r][c]=t-1;` in the move() function? It will reassign values to every tile on the board. It looks like a remnant of a cut and paste from init(). The code needs to find the blank tile and the tile to move without altering anything along the way.
As it turns out, a larger problem is lurking in the code that wasn't originally posted. The following line appears in both init() and draw(): `int board[d][d];` The board[ ][ ] array needs to be a global variable. By redeclaring it inside of each function, the code creates something called a shadow variable. Simply put, a shadow variable is a local variable that masks another var of the same name. So, when the code creates the shadow var array in init, that array gets processed locally in init, but the global array remains hidden and unaltered. When the code exits init, the shadow array ceases to exist and the global array (assuming you created one) is once again exposed. The same process happens in draw. This is the source of the random numbers. Also, by declaring locally, the content of the array is not accessible in any other function.