0

I've been working on the move function for an inordinate amount of time now and I keep getting stuck on one issue. I can move the blank tile to the left (to get it out of the corner space), but cannot move it up! For example, in 3x3 board, I cannot get the blank tile to swap with the 3 tile. Using the debugging tool it incorrectly says that tile==board[0][2] which is the location of 6. I've attached my code below and would be interested to hear your thoughts. Thank you so much!

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
    bool move(int tile)
    {
    int temp; 

    if (tile==0)
    {
        return false;
    }

    //find tile
    for (int i=0; i<d; i++)
    {
        for(int j=0; j<d; j++)
        {
            if (tile==board[i][j]) //checks if the tile is on the board
            {
                if (j>0) //determines if C has access to tiles to the left of tile
                {
                    if (0==board[i-0][j-1])//checking if the tile is left of the blank
                    {
                        temp = tile;
                        board[i][j]=board[i-0][j-1];
                        board[i-0][j-1] = temp;
                        return true;
                    }
                }

                if (j<d-1)//determines if C has access to tiles to the right of tile
                {
                    if (0==board[i+0][j+1])//checking if the tile is right of the blank
                    {
                        temp = tile;
                        board[i][j]=board[i+0][j+1];
                        board[i+0][j+1] = temp;
                        return true;
                    }
                }

                if (i>0)//determines if C has access to tiles to the above of tile
                {
                   if (0==board[i-1][j-0])//checking if the tile is above the blank
                    {
                        temp = tile;
                        board[i][j]=board[i-1][j-0];
                        board[i-1][j-0] = temp;
                        return true; 
                    } 
                }

                if (i<d-1) //determines if C has access to tiles to the below of tile
                {
                    if (0==board[i+1][j+0])//checking if the tile is below the blank
                    {
                        temp = tile;
                        tile=board[i+1][j+0];
                        board[i+1][j+0] = temp;
                        return true;  
                    }
                }
            }
        }
    }
    return false;
}

[EDIT: code unrelated to problem has been removed.]

1
  • I added all the code that I have made because I suspect that the problem is not actually inside the move function...
    – Erin K
    Oct 31, 2016 at 22:19

1 Answer 1

0

You have a simple code error in move - the kind of error one makes when exhausted or after working too long on one problem. (We all do it at some point. ) You've probably been looking at the forest for so long now, you can't see the dead tree. ;-)

Take a close look at the first three move blocks and then compare them to the fourth block, line by line. What's different about the last move - the one that handles moving to the tile below tile?

Spoiler alert: hover your mouse below to see:

It's a wrong variable problem! The line tile=board[i+1][j+0]; should be board[i][j]=board[i+1][j+0];

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

3
  • Thanks for the quick response. ARGH I keep making mistakes like this!! Thank you for pointing this out. I fixed the typo, however, that did not fix the initial problem, where the code incorrectly identifies the location of "tile." For example, in the initialized 3x3 board, it identifies board[0][2] as the 3 tile, instead of the board[1][2], where the tile is actually located. It then reports moving the 3 tile as an illegal move even though it is directly above the blank. After reporting the illegal move, it prints the solved board. Do you have any idea why this is?
    – Erin K
    Nov 1, 2016 at 0:09
  • If it was correctly fixed, move doesn't incorrectly identify the location of tile. Having said that, the won code is seriously flawed (and should be a separate question). The big problem (there are others) is that it uses the assignment operator = and not the equality test operator, ==. This means that when won is being called, it is reassigning the values of every tile on the board! This is also hiding the actions of move when it redraws the board. I suggest that you temporarily change won() to a simple return false; until you get move working correctly, and then work on won.
    – Cliff B
    Nov 1, 2016 at 0:33
  • Thank you! I commented out the won function and move worked perfectly. Now to work on that won...
    – Erin K
    Nov 1, 2016 at 4:16

You must log in to answer this question.

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