0

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;
}
1

After making a swap, remember to return true. return 0; is same as return false;, so your function never returns something else.

Don't swap the tile_row and blank_row variables. Those have no meaning outside of move, so your board never changes state. Instead, change board[tile_row][tile_col] to 0 and board[blank_row][blank_col] to tile.

Some room for improvement, if you store blank_row and blank_col in the global namespace (and initialise them in init and update on tile swap in move), you wouldn't have to search it again, and could check only the up to four neighbouring tiles for having value tile. This also has room for bugs (when reading off-board tiles), so maybe fix your code first before you attempt that.

| improve this answer | |

You must log in to answer this question.

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