# Game of Fifteen allows illegal moves and weird Tile swops Pset3

please help. My code seems to be allowing me to move tiles even when there is no open space. while the code correctly moves the underscore to the new space, in some cases, it does not move the tile to the space where the underscore once was.

`````` /**
* Initializes the game's board with tiles numbered 1 through d*d - 1
* (i.e., fills 2D array with values but does not actually print them).
*/
void init(void)
{
int k = d * d - 1;

// for each row
for ( int i = 0; i < d; ++i)
{
//for each column
for (int j = 0; j < d; ++j)
{
board[i][j] = k;
if (d % 2 == 0)
{
if (k == 1)
{
board[i][j] = 2;
board[i][j - 1] = 1;
}
}
--k;
}
}

}

/**
* Prints the board in its current state.
*/
void draw(void)
{
// for each row
for ( int i = 0; i < d; ++i)
{
//for each column
for (int j = 0; j < d; ++j)
{
if (board[i][j] == 0)
{
printf("-\t");
}
else
printf("%2i\t", board[i][j]);
}
printf("\n");
}
}

/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
{
//for each row
for (int i = 1; i <= d; ++i)
{
//for each column
for (int j = 1; j <= d; ++j)
{
if(board[i][j] == tile)
{
// move that tile if the move is legal
if (board[i - 1][j] == 0)   //if above the tile is vacant
{
board[i - 1][j] = board[i][j]; //move the contents of that tile to the vacant position above
board[i][j] = 0;  //initial position becomes vacant
return true;
}

else if (board[i][j - 1] == 0)   //if the LHS of the tile is vacant
{
board[i][j - 1] = board[i][j]; //move the contents of that tile to the vacant position on the LHS
board[i][j] = 0; //initial position becomes vacant
return true;
}

else if (board[i][j + 1] == 0)  //if the RHS of the tile is vacant
{
board[i][j + 1] = board[i][j]; //move the contents of that tile to the vacant position on the RHS
board[i][j] = 0; //initial position becomes vacant
return true;
}

else if (board[i + 1][j] == 0) //if below the tile is vacant
{
board[i + 1][j] = board[i][j]; //move the contents of that tile to the vacant position below
board[i][j] = 0; //initial position becomes vacant
return true;
}

else //anything else return false abeg!
{
return false;
}
}
}
}

return false;
}

/**
* Returns true if game is won (i.e., board is in winning configuration),
* else false.
*/
bool won(void)
{
int k = 1;
//for each row
for (int i = 0; i < d; ++i)
{
//for each column
for (int j = 0; j < d; ++j)
{
if(board[i][j] == k)
{
++k;
}
else
return false;
}
}
return true;
//return false;
}
``````

You are accessing board cells that are not part of the board, with indices like `-1` or `d`.
Change `if (board[i - 1][j] == 0)` to `if (i > 0 && board[i - 1][j] == 0)` and `if (board[i + 1][j] == 0)` to `if (i < d - 1 && board[i + 1][j] == 0)`, similar for `j`.
When the left side of `&&` is false, it will not even evaluate the right side.
• One way to fix above won function would be to replace `if(board[i][j] == k)` with `if(board[i][j] == k % (d*d))`, which effectively in the comparison replaces the value `d*d` with `0` – Blauelf Jun 5 '17 at 21:47