# Pset3 Game of Fifteen - Zeroes

``````void init(void)
{

int count = d*d-1;
for(int i=0; i < d; i++)
{
for(int j = 0;j< d;j++)
{
board[i][j] = count;
count--;
}
}
//if even number swap 1 and 2
if(d%2==1)
{
int temp = board[d-1][d-2];
board[d-1][d-2] = board[d-1][d-3];
board[d-1][d-3] = temp;
}
}
``````

``````void draw(void)
{
for(int i=0; i < d; i++)
{
for(int a=0; a < d; a++)
{
/*if(board[i][a]==0)
{
board[i][a] = (char)'#';
printf("%c\t",board[i][a]);
}
else
{*/
printf("%i\t",board[i][a]);
//}

}
printf("\n");
}
``````

## }

``````bool move(int tile)
{
for(int i=0; i < d; i++)
{
for(int j = 0;j< d;j++)
{
if(board[i][j]==tile)
{
if(board[i][j+1]==0 && board[i][j] != board[i][d-1])
{
int temp = tile;
board[i][j] = 0;
board[i][j+1] = temp;
return true;

}
else if(board[i][j-1]==0 && board[i][j] != board[i][0])
{
int temp = tile;
board[i][j] = 0;
board[i][j-1] = temp;
return true;
}
else if(board[i+1][j]==0 && board[i][j] != board[d-1][j])
{
int temp = tile;
board[i][j] = 0;
board[i+1][j] = temp;
return true;
}
else if(board[i-1][j]==0 && board[i][j] != board[0][j])
{
int temp = tile;
board[i][j] = 0;
board[i-1][j] = temp;
return true;
}
else
{
return false;
}
}
}
}
return false;
}
``````

Hello, I'm trying to solve GoF. It outputs the board with no problems, but when I try to move a tile; -For example: the tile 8 in a 3x3 board, it swaps ! It seems like the board is covered with invisible zeroes. How can I solve it ?

You have an array index problem. Look at the following:

``````for(int i=1; i <= d; i++)
``````

When an array has d elements, the range is from 0 to d-1. This code allows the range of elements to go to d - too far. This appears throughout the code.

Since you didn't include the move() function, there's no way to tell what's going on there.

[EDIT: further info after move() was added.]

In addition to the problem above, the move() function goes further outside the bounds of the board[][] array. Look at the following:

``````if(board[i][j+1]==0)
``````

Assume that d=3. The for loop allows i and j to be equal to 3. This test will attempt to look at board[3][4]. The problem with this is that neither index should go past 2. The limit is board[2][2]. Unfortunately, the compiler won't look at this as an error. Instead, it will just grab the data from wherever it thinks these elements are. (Historically, this goes back to a little-used technique for concatenating arrays.) Thus, random data, frequently 0, will be grabbed from these locations and will swap into the valid cells of the array.

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

• *added move(), and still didn't figure it out :[ – log101 Jul 25 '16 at 18:20
• The second problem you pointed can be solved with AND operators and such, but how can I remove these zeros? (Shortly, I didn't get the index problem) – log101 Jul 25 '16 at 20:10

Ok, the code above is updated, in check50 following error occurs;

``````:) fifteen.c exists
:) fifteen.c compiles
:) init initializes 3x3 board correctly
:( init initializes 4x4 board correctly
/ expected "1" character 3 of line 4, not "2"
``````

But the output is correct ! What do I do now ?