# Pset3/fifteen: move

I think some of the move code I've written is working correctly in that once the "Tile to move:" message appears and I enter a logical tile to move, I do not see the "illegal move" message, but when I do choose a number that does not border the blank space, I do get the "illegal move" message. However, the board does not redraw and the "Tile to move:" message remains after a correctly chosen tile to move.

Any hints on mistakes I've made or additions I need are very much appreciated!

``````    void init(void)
{
// TODO
int grid [d][d];
int i = 0;
int j = 0;
grid [i][j] = (d * d) - 1;
int n = (d * d) - 1;
for (j = 0;j < d;j++)
{
board [i][j] = n;
n--;
}

while (i < d)
{
i++;

for (j = 0;j < d;j++)
{
board[i][j] = n;
n--;

}
if (i == d-1)
{   if (d == 4 || d == 6 || d == 8)
{   //swap 1 and 2

int temp = board[i][j-2];
board[i][j - 2] = board[i][j - 3];
board[i][j - 3] = temp;
}

}
}
}

/**
* Prints the board in its current state.
*/
void draw(void)
{
// TODO
int i;
int j;

for (i = 0;i < d;i++)
{
for (j = 0;j < d;j++)
{   if (board[i][j] == 0)
printf("%2c  ",'_');
else
printf("%2d  ",board[i][j]);

}

printf("\n");
}

}

/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
{
// TODO
// SEARCH for tile
int i = 0;
int j = 0;

{
for (i = 0;i < d;i++)
{
for (j = 0; j < d;j++)

{   if (board[i][j] == tile)

{

if (board[i][j + 1] == 0 || board[i][j - 1] == 0
|| board[i + 1][j] == 0 || board[i - 1][j] == 0)
{   // move tile
int blank = 0;
int temp = tile;
board[i][j] = blank;
blank = temp;

return true;
}
}
}
}
}
return false;
}

/**
* Returns true if game is won (i.e., board is in winning configuration),
* else false.
*/
bool won(void)
{
// TODO
return false;
}
``````

• Same problem. `tile` contains the value of the tile to move, but the board[][] element that contains that value has to be swapped with the blank or 0 element in the board array. You're still swapping the value of tile with blank, which really does nothing useful, but in the middle, you're inserting the blank value, or 0, into the array. Also, you're testing to see IF one of the adjoining tiles is blank, not WHICH is blank, and then trying to swap if that's true. How do you know which of the 4 is the actual blank? You'll need 4 separate tests and code blocks for this. Oct 7 '16 at 22:43