0

I am trying to make the moves happen. So far, the board shows correctly, with the 2 and 1 swapped if odd "d" is provided. But when I type any number at "Title to move:" it just keeps saying "Illegal move" and nothing happens. I don't know what's wrong. I've declared int i, int j, and int swapper. Please help!

// TODO
    int blank_tile = 0;

    for (i = 0; i < d; i++)
    {
        for (j = 0; j < d; j++)
        {
            if (board[i][j] == 0)
                board[i][j] = blank_tile;

            if (board[i][j] == tile) //search for tile
            {
                if (board[i][j + 1] == blank_tile || board[i - 1][j] == blank_tile || board[i + 1][j] == blank_tile) //swap tile with blank_tile
                {   
                    swapper = blank_tile;
                    blank_tile = tile;
                    tile = swapper;
                }

                return true;
            }

            else
                return false;
        }
    }
    return false;
}
0

You have several major problems here. First, look carefully at this if/else:

        if (board[i][j] == tile) //search for tile
        {
           ...
        }
        else
            return false;

It says that if you don't find the tile in board[i][j], return false. That means that unless the tile you're searching for is the very first tile, it's going to quit and return false to the calling code. It'll never get past the first tile.

Next, the conde inside that if statement will, if executed, do a great job of swapping the values stored in swapper, blank_tile, and tile. Unfortunately, it won't do a thing to swap the actual values stored in board[][]. In short, nothing on the actual board gets swapped. You have to swap the actual elements in the board[][] array.

Next, look at the test for the blank tile.

            if (board[i][j + 1] == blank_tile || board[i - 1][j] == blank_tile
                  || board[i + 1][j] == blank_tile) //swap tile with blank_tile

This is supposed to identify that there's a blank tile adjacent to the tile to be swapped. Unfortunately, it doesn't identify which tile. It merely narrows it down to 4 tiles. How do you know which one to swap? Worse still, it tests outside the bounds of the array (it goes off the edge of the board). Consider what happens when d=3. Now, say that i=2 and j=2. That means that i+1=3 and j+1=3. That's two cases where the array index will be beyond the dimensions of the array. Usually, this results in invalid data being swapped into the board array (usually, extra 0's) and valid data being deleted and replaced by these 0's.

Think about these lines:

int blank_tile = 0;
    ...
        if (board[i][j] == 0)
            board[i][j] = blank_tile;

This is called dead code. It executes, but effectively does nothing. In short, it says that if board[i][j] is 0, change it to 0. These last two lines could be completely removed and result in no change at all, except to improve performance.

There's a lot of work to be done. Once rewritten, if you have any new questions, please post a new question with the updated code.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • Thanks for the reply. I will work on it. – Lele Carabina Oct 14 '16 at 14:14
  • Ok. So I should be working with d instead of i and j to correctly get the boundaries of the board, right? And the tile I should focus on in the inner if statement should not be blank_tile, but tile, I think, because the code returns a message if the move is illegal, so I have to track tile, instead. Right? – Lele Carabina Oct 14 '16 at 14:22
  • I will create a new post as I made several changes and still have issues. Thanks for your help. – Lele Carabina Oct 14 '16 at 20:32

You must log in to answer this question.

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