I'm currently working on the move function that's in the game of fifteen and every single move is illegal even though my logic seems correct (at least to me), but it keeps outputting illegal move and I really don't know why. This is my code:
bool move(int tile)
{
int tile_row = 0;
int tile_col = 0;
int blank_row = 0;
int blank_col = 0;
// variables used for storing variables while swapping values
int holder_row = 0;
int holder_col = 0;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
// if the tile value we are looking for equals the number on the board
if (tile == board[i][j])
{
// set the row of that number to the position of i
tile_row = i;
// set the column of that number to the position of j
tile_col = j;
}
// if the value on the board is 0 (blankspace)
if (board[i][j] == 0)
{
// set the blankspace row to i
blank_row = i;
// set the blankspace column to j
blank_col = j;
}
}
}
// if the tile is in the same row as the blank and on one column ahead
if (tile_row == blank_row && tile_col == blank_col + 1)
{
// swap the tile's and the blank's positions
holder_row = tile_row;
holder_col = tile_col;
tile_row = blank_row;
tile_col = blank_col;
blank_row = holder_row;
blank_col = holder_col;
}
// if the tile is in the same row as the blank and on one column behind
else if (tile_row == blank_row && tile_col == blank_col - 1)
{
// swap the tile's and the blank's positions
holder_row = tile_row;
holder_col = tile_col;
tile_row = blank_row;
tile_col = blank_col;
blank_row = holder_row;
blank_col = holder_col;
}
// if the tile is in the same column as the blank and on one row behind
else if (tile_col == blank_col && tile_row == blank_row + 1)
{
// swap the tile's and the blank's positions
holder_row = tile_row;
holder_col = tile_col;
tile_row = blank_row;
tile_col = blank_col;
blank_row = holder_row;
blank_col = holder_col;
}
// if the tile is in the same column as the blank and on one row ahead
else if (tile_col == blank_col && tile_row == blank_row - 1)
{
// swap the tile's and the blank's positions
holder_row = tile_row;
holder_col = tile_col;
tile_row = blank_row;
tile_col = blank_col;
blank_row = holder_row;
blank_col = holder_col;
}
else
{
return false;
}
return 0;
}