# CS50 FIFTEEN my "move" function doesn't work

My name is Samuel and I'm 14 years old. I have been working on problem set 3 but have a problem with the "move" function. When an illegal tile has been inserted, it reply's with "illegal move" while when it is a legal tile that I entered, nothing happens. No message is shown but the tile does not swap with the blank space.

``````bool move(int tile)
{
int blank_space = 0, temp;

if (tile == 0)
{
return false;
}
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (board[i][j] == tile)
{
if (j > 0)
{
if (board[i-0][j-1] == blank_space)
{
temp = board[i][j];
blank_space = board[i-0][j-1];
board[i][j] = temp;
return true;
}
}
if (i > 0)
{
if (board[i-1][j-0] == blank_space)
{
temp = board[i][j];
blank_space = board[i-1][j-0];
board[i][j] = temp;
return true;
}
}
if (j < d-1)
{
if (board[i+0][j+1] == blank_space)
{
temp = board[i][j];
blank_space = board[i+0][j+1];
board[i][j] = temp;
return true;
}
}
if (i < d-1)
{
if (board[i+1][j+0] == blank_space)
{
temp = board[i][j];
blank_space = board[i+1][j+0];
board[i][j] = temp;
return true;
}
}
}
}
}
return false;
}
``````

Thank you :) ,

From,

Samuel

Simply put, the code doesn't swap anything. Let's take one block of code and walk through it, but a little out of sequence:

``````            if (board[i-0][j-1] == blank_space)
{
temp = board[i][j];
blank_space = board[i-0][j-1];
board[i][j] = temp;
return true;
}
``````

It first assigns the value of board[i][j] to temp. Note that this value is equal to the value stored in tile. Next, it assigns board[i-0][j-1] to blank_space. This is 0, the value that was already stored in blank_space, so this is do-nothing code. Following that, it stores the value in temp back into board[i][j], which was already there. In summary, those three lines don't swap any values, they do nothing and would accomplish the same thing if they weren't there at all.

Think about what you are trying to do. You want to identify the target tile and the blank tile in the board array. Once you do, you want to make sure that they are adjacent to each other. The code structure does that.

But next, you want to swap those values in the array. You could do a value swap using an interim variable, like this:

``````temp = a;
a = b;
b = temp;
``````

but there's an easier, more efficient way in this case. You know that the value of the tile to be swapped is stored in `tile`. You also know that 0 is stored in blank_tile. The straightforward way to do it is to assign tile to the array element that is 0 and assign blank_tile to the array element that contains the target tile. I'll leave it to you to figure out the code to do this. It should be simple enough.

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