1

I have been working on the move function for fifteen for a few days and I don't understand where I am going wrong, please can someone advise me where/what I should focus on. Here is my code:

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
bool move(int tile)
{
    // TODO
    //int row = board[d - 1][d - 1]; 
    //int col = board[d - 1][d - 1];
   //int temp;

for (int i = 0; i < d; i++)
{
    for (int j = 0; j < d; j++)
    {
        if (tile == board[i][j]) 
        {
            //board[i][j] = tile;
            {
                if (board[i - 1][j] == 0 && i - 1 > 0)
                {
                    board[i][j] = board[i - 1][j];
                    board[i - 1][j] = board[i][j];
                    return true;
                }
                if (board[i + 1][j] == 0 && i + 1 < d)
                {
                    board[i][j] = board[i + 1][j];
                    board[i + 1][j] = board[i][j];
                    return true;
                }
                if (board[i][j - 1] == 0 && j - 1 > 0)
                {
                    tile = board[i][j - 1];
                    board[i][j - 1] = board[i][j];

                    return true;
                }
                if (board[i][j + 1] == 0 && j + 1 < d) 
                {
                    board[i][j] = board[i][j + 1];
                    board[i][j + 1] = board[i][j];
                    return true;
                }
            }

        }
    }
}


return false;

}

Many thanks

1 Answer 1

2

Resolved!

I was watching week 4s lecture and realised that my swap didn't make sense, I was expecting the program to switch two values at the same time. So I used a temp variable to store the value of blank tile, then moved the tile value to the blank tile and finally stored the value of the temp variable in the then original tile value. Hope that makes sense, I'm still working on my pseudocode technique!

temp = board[i - 1][j];
board[i - 1][j] = board[i][j];
board[i][j] = temp;

You must log in to answer this question.

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