I'm currently working on pset3 game of fifteen, and my move function is not working. Below is my code for move().
bool move(int tile)
{
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (board[i][j] == tile)
{
if (board[i + 1][j] == 0)
{
int tmp = board[i][j];
board[i][j] = board[i + 1][j];
board[i + 1][j] = tmp;
return true;
}
if (board[i][j + 1] == 0)
{
int tmp = board[i][j];
board[i][j] = board[i][j + 1];
board[i][j + 1] = tmp;
return true;
}
if (board[i - 1][j] == 0)
{
int tmp = board[i][j];
board[i][j] = board[i - 1][j];
board[i - 1][j] = tmp;
return true;
}
if (board[i][j - 1] == 0)
{
int tmp = board[i][j];
board[i][j] = board[i][j - 1];
board[i][j - 1] = tmp;
return true;
}
}
}
}
return false;
}
I have accounted for the '0' tile being above (board[i + 1][j]), below (board[i - 1][j]), left (board[i][j - 1]), and right (board[i][j + 1]). I used the following swapping method to swap the tiles.
int tmp = a;
int a = b;
int b = tmp;
However, when I run the program and input a tile to move, some (not all) of the numbers just turn to 0 (leaving the original 0 untouched as well) instead of actually swapping the tiles. Here is an excerpt:
7 0 0
8 0 0
5 4 3
Any help would be appreciated. Thanks!