0

working on pset3 fifteen.c. I think that something is wrong with my adjacent checking in my move function. It says "Illegal move" even when I choose an adjacent tile.

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
bool move(int tile)
{
    // cycle through board, find location of tile
    for (int j = 0; j < d; j++)
    {
        for (int i = 0; i < d; i++)
        {
            // check possible adjacent locations of the empty space
            if (board[j][i] == tile)
            {
                if (j + 1 > d)
                {
                    if (board[j + 1][i] == 0)
                    {
                        board[j + 1][i] = tile;
                        board[j][i] = 0;
                    }
                }

                if (i + 1 > d)
                {
                    if (board[j][i + 1] == 0)
                    {
                        board[j][i + 1] = tile;
                        board[j][i] = 0;
                    }
                }

                if (j - 1 > 0)
                {
                    if (board[j - 1][i] == 0)
                    {
                        board[j - 1][i] = tile;
                        board[j][i] = 0;
                    }
                }

                if (i - 1 < 0)
                {
                    if (board[j][i - 1] == 0)
                    {
                        board[j][i - 1] = tile;
                        board[j][i] = 0;
                    }
                }
            }
        }
    }
    return false;
}

Any help is appreciated!

0
2

It fails because 3 out of 4 of your if conditions are wrong.

Not the answer you're looking for? Browse other questions tagged .