0

In my Game of Fifteen code, the move function for the most part works, but once in a while, a tile will duplicate, looking a bit like this

    1 1 2
    4 5 6
    7 8 _

As you can see, the number 3 disappeared, and it for some reason no longer exists (meaning none of the tiles behave like a tile containing 3), leaving the game with 2 tiles containing the number 1.

Here is my move function:

    bool move(int tile)
   {
        //if user move tile is less than 0, or more than d squared subtract one, this is an invalid move.
        if(tile < 0 || tile > (d * d - 1)) 
             return false;

         bool moved = false;

        //moving functions.  If a space to the left, right, up, or down from 0 tile has the specific tile, swap positions.
         if(!moved && board[zeroRow + 1][zeroCollumn] == tile)
         {
            board[zeroRow][zeroCollumn] = tile;
            board[zeroRow + 1][zeroCollumn] = 0;

            zeroRow = zeroRow + 1;
            moved = true;
        }
        else if(!moved && board[zeroRow - 1][zeroCollumn] == tile)
        {
            board[zeroRow][zeroCollumn] = tile;
            board[zeroRow - 1][zeroCollumn] = 0;

            zeroRow = zeroRow - 1;
            moved = true;
        } 
        else if(!moved && board[zeroRow][zeroCollumn + 1] == tile)
        {
            board[zeroRow][zeroCollumn] = tile;
            board[zeroRow][zeroCollumn + 1] = 0;

            zeroCollumn = zeroCollumn + 1;
            moved = true;
        }
        else if(!moved && board[zeroRow][zeroCollumn - 1] == tile)
        {

            board[zeroRow][zeroCollumn] = tile;
            board[zeroRow][zeroCollumn - 1] = 0;

            zeroCollumn = zeroCollumn - 1;
            moved = true;
        }



        // TODO
        return moved;
    }

Also, variables zeroRow and ZeroCollumn are public variables that are declared in the init() function, where the index's of the tile array containing the "zero" value are stored in zeroRow and zeroCollumn. Can anyone explain what is wrong with my move() function? Or why the duplicating bug happens?

1

It looks to me that your code is swapping tiles off the edge of the board. The code doesn't check whether an array index is out of range. Look at the following:

     if(!moved && board[zeroRow + 1][zeroCollumn] == tile)
     {
        board[zeroRow][zeroCollumn] = tile;
        board[zeroRow + 1][zeroCollumn] = 0;

Let's say d=3 and tile is at 2,1. That means that ZeroRow+1 = 3, which is out of range. Unfortunately, it probably won't produce a runtime error. Instead, it will make the swap with what the computer believes is in that memory location, thus making a bad swap and corrupting the bad memory access.

The code needs to check that these indexes are not out of range, i.e., index>=0 and < d.

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

| improve this answer | |

You must log in to answer this question.

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