I've been working on the move function for an inordinate amount of time now and I keep getting stuck on one issue. I can move the blank tile to the left (to get it out of the corner space), but cannot move it up! For example, in 3x3 board, I cannot get the blank tile to swap with the 3 tile. Using the debugging tool it incorrectly says that tile==board[0][2] which is the location of 6. I've attached my code below and would be interested to hear your thoughts. Thank you so much!
/**
* If tile borders empty space, moves tile and returns true, else
* returns false.
*/
bool move(int tile)
{
int temp;
if (tile==0)
{
return false;
}
//find tile
for (int i=0; i<d; i++)
{
for(int j=0; j<d; j++)
{
if (tile==board[i][j]) //checks if the tile is on the board
{
if (j>0) //determines if C has access to tiles to the left of tile
{
if (0==board[i-0][j-1])//checking if the tile is left of the blank
{
temp = tile;
board[i][j]=board[i-0][j-1];
board[i-0][j-1] = temp;
return true;
}
}
if (j<d-1)//determines if C has access to tiles to the right of tile
{
if (0==board[i+0][j+1])//checking if the tile is right of the blank
{
temp = tile;
board[i][j]=board[i+0][j+1];
board[i+0][j+1] = temp;
return true;
}
}
if (i>0)//determines if C has access to tiles to the above of tile
{
if (0==board[i-1][j-0])//checking if the tile is above the blank
{
temp = tile;
board[i][j]=board[i-1][j-0];
board[i-1][j-0] = temp;
return true;
}
}
if (i<d-1) //determines if C has access to tiles to the below of tile
{
if (0==board[i+1][j+0])//checking if the tile is below the blank
{
temp = tile;
tile=board[i+1][j+0];
board[i+1][j+0] = temp;
return true;
}
}
}
}
}
return false;
}
[EDIT: code unrelated to problem has been removed.]