0

please help. My code seems to be allowing me to move tiles even when there is no open space. while the code correctly moves the underscore to the new space, in some cases, it does not move the tile to the space where the underscore once was.

 /**
 * Initializes the game's board with tiles numbered 1 through d*d - 1
 * (i.e., fills 2D array with values but does not actually print them).  
 */
void init(void)
{   
int k = d * d - 1;

// for each row
for ( int i = 0; i < d; ++i)
    {
        //for each column
        for (int j = 0; j < d; ++j) 
            {
                board[i][j] = k;
                if (d % 2 == 0)
                {
                    if (k == 1)
                        {
                            board[i][j] = 2;
                            board[i][j - 1] = 1;
                        }
                }
                --k;
            }        
    }

}

 /**
  * Prints the board in its current state.
  */
void draw(void)
 {
// for each row
for ( int i = 0; i < d; ++i)
    {
        //for each column
        for (int j = 0; j < d; ++j) 
            {
                if (board[i][j] == 0)
                {
                    printf("-\t");
                }
                else
                printf("%2i\t", board[i][j]);
            }   
        printf("\n");      
      }
}

/**
 * If tile borders empty space, moves tile and returns true, else
 * returns false. 
 */
bool move(int tile)
{   
//for each row
for (int i = 1; i <= d; ++i)
    {
        //for each column
        for (int j = 1; j <= d; ++j)
        {
            if(board[i][j] == tile)
            {
                // move that tile if the move is legal
                if (board[i - 1][j] == 0)   //if above the tile is vacant
                {
                    board[i - 1][j] = board[i][j]; //move the contents of that tile to the vacant position above
                    board[i][j] = 0;  //initial position becomes vacant
                    return true;
                }

                else if (board[i][j - 1] == 0)   //if the LHS of the tile is vacant
                {
                    board[i][j - 1] = board[i][j]; //move the contents of that tile to the vacant position on the LHS
                    board[i][j] = 0; //initial position becomes vacant
                    return true;
                }

                else if (board[i][j + 1] == 0)  //if the RHS of the tile is vacant
                {
                    board[i][j + 1] = board[i][j]; //move the contents of that tile to the vacant position on the RHS
                    board[i][j] = 0; //initial position becomes vacant
                    return true;
                }

                else if (board[i + 1][j] == 0) //if below the tile is vacant
                {
                    board[i + 1][j] = board[i][j]; //move the contents of that tile to the vacant position below
                    board[i][j] = 0; //initial position becomes vacant
                    return true;
                }

                else //anything else return false abeg!
                {
                    return false;
                }
            }
        }
    }

return false;
}

/**
 * Returns true if game is won (i.e., board is in winning configuration), 
 * else false.
 */
 bool won(void)
{
int k = 1;
//for each row
for (int i = 0; i < d; ++i)
    {
        //for each column
        for (int j = 0; j < d; ++j)
        {
            if(board[i][j] == k)
            {
                ++k;
            }
            else
            return false;
        }
    }
    return true;
   //return false;
}
0

You are accessing board cells that are not part of the board, with indices like -1 or d.

Change if (board[i - 1][j] == 0) to if (i > 0 && board[i - 1][j] == 0) and if (board[i + 1][j] == 0) to if (i < d - 1 && board[i + 1][j] == 0), similar for j.

When the left side of && is false, it will not even evaluate the right side.

Also, the won function does not seem to consider the 0 tile.

| improve this answer | |
  • I appreciate your help!.. I still have issues winning the game.. sandbox.cs50.net/checks/f290e57c480a40ef8de73d844410ed4a – Adeyomola Adebayo Jun 4 '17 at 13:01
  • Umm... You say you have issues, but this check50 result is showing all-green? – Blauelf Jun 5 '17 at 16:18
  • One way to fix above won function would be to replace if(board[i][j] == k) with if(board[i][j] == k % (d*d)), which effectively in the comparison replaces the value d*d with 0 – Blauelf Jun 5 '17 at 21:47
  • I resolved the issue... Thank you for your time and your knowledge – Adeyomola Adebayo Jun 6 '17 at 16:32

You must log in to answer this question.

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