This code swaps digits but when i enter illegal key instead of illegal key prompt it replaces that digit with 0.

``````bool move(int tile)

{   int blank;
for(int i=0; i<d; i++)
{
for(int j=0; j<d; j++)
{
if(board[i][j] == 0)
{
blank = board[i][j];
}
}
}
for(int i=0; i<d; i++)
{

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

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

if(board[i+1][j] == blank)
{

board[i+1][j] = board[i][j];
board[i][j]= blank;
blank = board[i][j];
return true;

}
else if(board[i-1][j] == blank)
{
board[i-1][j] = board[i][j];
board[i][j] = blank;
blank = board[i][j];
return true;
}
else if(board[i][j+1] == blank)
{
board[i][j+1] = board[i][j];
board[i][j] = blank;
blank = board[i][j];
return true;
}
else if(board[i][j-1] == blank)
{
board[i][j-1] = board[i][j];
board[i][j] = blank;
blank = board[i][j];
return true;
}
else
{
return false;
}

}
}
}
return false;
}
``````

You might want to get rid of the first set of nested loops:

``````for(int i=0; i<d; i++)
{
for(int j=0; j<d; j++)
{
if(board[i][j] == 0)
{
blank = board[i][j];
}
}
}
``````

Since this is just setting `blank` equal to `0` whenever it finds the 0 tile.

You can substitute that by:

``````blank = 0;
``````

Or just by comparing against `0` in your code instead of a variable `blank`.

In the following set of loops you might want to review these cases:

What happens when `i` or `j` are `0`?

Let's say the tile you are looking for is at `board[0][4]`.

The loop nest is at the point where `i` is `0` and `j` is `4`.

If the function checks for:

``````else if(board[i-1][j] == blank)
``````

It is checking:

``````board[-1][4]
``````

Since there no index `-1` this might cause you either errors or undesired behaviour.

What happens when `i` or `j` are equal to `d - 1`?

The same will happen if your dimension is 3 and the tile that is being tested in `move()` is at `board[2][2]`.

If the function checks for:

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

It is checking:

``````board[2][3]
``````

If there is an array `board[][]` of 3x3. The maximum indexes the function should check for are `board[2][2]`.

If you want some suggestions, here are some:

You could use the first set of nested loops you have to find where is your 0 tile.

You could use this same set to look for the location of the tile you would like to move.

You can subtract their respective rows and columns and compare them to check validity of the movement.

``````1 2 3
4 5 6
7 8 9
``````

If you want to move 5, you can only move it to 2, 4, 6, or 8. This means that either the row needs to be the same and the column off by one, or the opposite. The opposite is, the column needs to be the same and row off by one.

Having said this, let us say you have the following variables:

``````int row0;
int column0;

int rowTile;
int columnTile;
``````

If you can correctly find the position of your 0 tile and your desired move tile, you can do math with them.

Also, before you even do any of this, you can check if the desired tile is completely wrong. You can check if it is less than 1 or greater than you maximum allowed tile.

Here is some pseudo-code:

``````// check if tile is greater than maximum allowed tile OR less than one
// if so, return false (Illegal move)

// Declare variables for rows and columns for the 0 tile and the desired move tile respectively

// Do nested loops to find coordinates of 0 tile

// Do nested loops to find coordinates of desired move tile

// Do math to check if the desired move tiled can indeed be moved

// if so, swap values

// return true

// else return false
``````

Here is a draft of the nested loops to find rows and columns

``````int row;
int column;

for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
if(board[i][j] == value)
{
row = i;
column = j;
break;
}
}
}
``````

And here is the logic (along with the math) of the proximity that is valid for a move between the 0 tile and desired move tile:

Assuming,

`int row0` contains the row index of the 0 tile

`int column0` contains the column index of the 0 tile

`int rowTile` contains the row index of the desired move tile

`int columnTile` contains the column index of the desired move tile

You want to swap if the following is true:

``````if( (abs(rowTile - row0) == 1 && abs(columnTile - column0) == 0) ||
(abs(rowTile - row0) == 0 && abs(columnTile - column0) == 1))
``````

Which means what I wrote above:

Either the row needs to be the same and the column off by one, or the opposite. The opposite is, the column needs to be the same and row off by one.

If two values are equal, the absolute value of their difference is going to be 0.

If two values are off by one, the absolute value of their difference is going to be 1.