1

I rewrote almost the entire code and I finally was able to make the tiles swap. Problem is at every swap the blank tile gets duplicated and the tile swapped disappears, eventually rendering a Segfault.

I have checked similar problems in this forum, but I think I have all my possible issue makers covered. My i and j don't go over 3 and I am swapping the values, not the indexes. I'm tapped out.

Can anyone tell me what's wrong with my code (it's partially pseudo)?

    ...

    int blank: 0;
    int blank_row: d - 1; //keeps track of blank top-bottom
    int blank_col: d - 1; //keeps track of blank left-right

    for (i = 0...)
    {
        for (j = 0...)
        {
            if (board[i][j] == tile) //search for tile
            {
                ... ii ...
                ... jj ...
            }
        }
    }

    if ((ii > 0 && board[ii - 1][jj] == blank ) || (ii <= (blank_row - 1) && board[ii + 1][jj] == blank) || (jj > 0 && board[ii][jj - 1] == blank) || (jj <= (blank_col - 1) && board[ii][jj + 1] == blank))
    {   
        board[ii][jj] = blank;
        board[blank_row][blank_col] = tile;

        return true;
    }

    else            
        return false;
2

1 Answer 1

0

I think I figured it out. The vars int blank_row: d - 1; and int blank_col: d - 1; need to be initialized globally, after init(), in order for the value of d to be captured correctly. I made this change and now it seems to be working. Even check50 agreed :) This one kicked my butt OMG!

You must log in to answer this question.

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