# pset3 fifteen - doesn't work ONLY with blank = 0

I've tried many times that the code works very well with blank equal to -1, 99, or other values other than zero. I can get "ftw" for these vales, but segmentation fault, or multiple BLANKs starting to pop up on the board only occurs when BLANK = 0 (the tile and blank values don't switch properly). Where's the problem?

(I define BLANK as 0.)

``````int blank_x;
int blank_y;

bool move(int tile)
{
// get the tile's position
int m;
int n;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (board[i][j] == tile)
{
m = i;
n = j;
}
}
}

// check for a valid move
if(board[blank_x][blank_y] == board[m+1][n] && 0 <= m <= d-2 && 0 <= n <= d-1)
{
board[m+1][n] = tile;
board[m][n] = BLANK;
blank_x = m;
blank_y = n;
return true;
}
else if(board[blank_x][blank_y] == board[m-1][n] && 1 <= m <= d-1 && 0 <= n <= d-1)
{
board[m-1][n] = tile;
board[m][n] = BLANK;
blank_x = m;
blank_y = n;
return true;
}
else if(board[blank_x][blank_y] == board[m][n+1] && 0 <= n <= d-2 && 0 <= m <= d-1)
{
board[m][n+1] = tile;
board[m][n] = BLANK;
blank_x = m;
blank_y = n;
return true;
}
else if(board[blank_x][blank_y] == board[m][n-1] && 1 <= n <= d-1 && 0 <= m <= d-1)
{
board[m][n-1] = tile;
board[m][n] = BLANK;
blank_x = m;
blank_y = n;
return true;
}

return false;
}
``````

You have a serious problem with your tests. For example, look at the following:

``````if(board[blank_x][blank_y] == board[m+1][n] && 0 <= m <= d-2 && 0 <= n <= d-1)
``````

More specifically, look at the parts. Let's take one portion to be absolutely clear what's wrong: `0 <= m <= d-2` In high school math (or earlier?) we were taught that this means that m is between the two outside arguments and it was perfectly acceptable. However, in computer programming, this is NOT acceptable. Logic tests like these cannot be chained like this. They have to be split into separate clauses, as follows:

``````                  0 <= m  &&  m <= d-2
``````

This error exists throughout the move function ( 8 times? ) and has to be corrected.

It can be further simplified because only one of these has to be checked in each test, i.e., when looking at m+1, only `m <= d-2` needs to be checked. For m-1, only `m >-0`, etc.

As a side note, you could simplify the test even more by just checking whether board[m+1][n] == BLANK rather than board[blank_x][blank_y].

I haven't checked for other errors, but these need to be corrected first. Depending on whether other errors are introduced, as well as how you correct this, it isn't prudent to look further just now. There may or may not be more issues, but this will get you going.

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

• Hahaha omg. What a stupid minor error. Yes, it worked after the logic tests are separated. Thank you very much. U knew that both that " board[m+1][n] == BLANK "and "I only have to test on m, not n included." I was just trying everything I could. But still wondering why the original expression works with BLANK as other values. Oct 14 '16 at 13:31