# pset3: Help with Fifteen Move function

I need help with my move function. I can't figure out how to store the blank tile position only once without searching it together with the TILE input. Also, my move based on this code is always illegal. Draw and Init prints out the correct array.

``````bool move(int tile)
{
int r = DIM_MIN;
int c = DIM_MIN;
int blankr, blankc;
for (int row=0; row<d;row++)
{
for (int col=0; col<d;col++)
{
if (board[row][col] == tile)
{
r = row;
c = col;
}
else if (board[row][col] == 0)
{
blankr = row;
blankc = col;
}
}
}
return false;
if (r == DIM_MIN)
return false;
if (((abs(r-blankr)==1) || (abs(c-blankc)==1)) && !((abs(r-blankr)==1) &&     (abs(c-blankc)==1)))
{
int temp1 = board[r][c];
board[r][c]=board[blankr][blankc];
board[blankr][blankc]=temp1;
}
else return false;
}
``````

I think it's ok to search for both tile and empty tile, though you could maintain global variable(s) to store the position of the empty tile, some even keep track of the positions of all tiles, usually using one index, calculated as `d*row+col`.

You have a line `return false;` after your for-loop, that will leave the function without moving the tile.

You have to ensure that one of the differences has an absolute value of 1, the other has an absolute value of 0. There are many ways to express this, for example

``````if (((abs(r-blankr)==1) || (abs(c-blankc)==1)) && ((abs(r-blankr)==0) || (abs(c-blankc)==0)))
``````

or

``````if (abs(r-blankr)==1 && abs(c-blankc)==0 || abs(r-blankr)==0 && abs(c-blankc)==1)
``````

(`&&` has higher precedence than `||`)

or if you want

``````if (abs(r-blankr)+abs(c-blankc)==1)
``````

As you know the values, the swap operation could be

``````    board[r][c] = 0;
board[blankr][blankc] = tile;
return true;
``````

without `temp1` (and don't forget the return value!)

• Hello, thanks for the help it was incredibly useful. Do you mind giving an example how to keep track of the position of all tiles using `d*row+col`? That's the only part I don't understand from your answer Jan 24 '17 at 12:01
• The formula `i=d*row+col` can transform 2D coordinates to a linear index (here arbitrarily called `i`), you'd transform back using `row=i/d` and `col=i%d`. You then have an integer array with one element per tile, and store the linearised coordinate of that tile. Jan 24 '17 at 12:07

Every move is illegal because after function finds the tile location and the blank tile it's done and returns to main here `return false;`. In fact nowhere does the function `return true;`

Since `blankr` and `blankc` are local variables, you will need to search for the zero tile every time. If they were declared globally, like `board` is, they would persist throughout the program.

`DIM_MIN` and `DIM_MAX` are not significant to this function. They are simply used to verify user input. Imagine if you have built a 4x4 board. You may want to move a tile that is in the last row. The last row will have index 3. Program will always `return false;` here `if (r == DIM_MIN)`