# PSET3- Game of fifteen, function move();

So, my move function is ALMOST working, but sometimes when swaping numbers both tile AND blank space shows up as a blank space...can you guys check my code? Thanks! Ps..Blank is a global variable, where the function draw(); keeps the location of the blank space.

``````bool move(int tile)
{
int row;
int column;
//iterates over the board until tile user typed is found
for (int i=0; i<d; i++){
for (int j=0; j<d; j++){
if (board[i][j] == tile){
row = i;
column= j;
}
}
}
//check if tile is valid: left, right, top or bottom to underline, and return true.
if (board[row+1][column] == blank)
{
int aux = board[row][column];
board[row][column]=blank;
board[row+1][column]=aux;
return true;
}
else
if (board[row-1][column] == blank){
int aux = board[row][column];
board[row][column]=blank;
board[row-1][column]=aux;
return true;
}
else
if (board[row][column+1] == blank){
int aux = board[row][column];
board[row][column]=blank;
board[row][column+1]=aux;
return true;
}
else
if (board[row][column-1] == blank){
int aux = board[row][column];
board[row][column]=blank;
board[row][column-1]=aux;
return true;
}
//otherwise return false.
return false;
}
``````

## 1 Answer

This is a common problem. The code is going off the edge of the board. Look at the following:

``````if (board[row][column+1] == blank){
``````

Let's assume d=3 and column=2. What is at board[row][column+1]? Probably 0. Remember, the two dimensions can't go past d-1 or below 0. In this case, column + 1 = 3 which is too far. Similar logic applies to each of the other cases.

Because its going off the edge, and depending which edge, the code will pull either a 0 (usually) or an unpredictable number (uncommon) into the board.

You need to address this so that the row and column values don't exceed the dimensions of the board.

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