0

I've been working on this for ages and I'm pretty sure it works but I can't get the correct output through check50 so I'm not sure if I can submit it like this.

Basically I've currently got the blank set to d*d (I though this would make the coding for 'won' easier) and I've tested it with 3x3.txt and 4x4.txt and both work fine. But if I change the value of blank to zero, it messes everything up - starts putting extra blanks in the grid, losing the numbers. I don't change anything else in my code, just the value of the blank. Any ideas?

Here are the relevant bits of my code:

void draw(void)
{
for (int i = 0; i < d; i++)
{   
    for (int j = 0; j < d; j++)
    {
        if (board[i][j] == (d*d))
        {
            printf("  _ ");
        }
        else
        {
            printf(" %2d ", board[i][j]);
        }
    }
    printf("\n");
}
}

/**
* If tile borders empty space, moves tile and returns true, else
* returns false. 
*/
bool move(int tile)
{
for (int i = 0; i < d; i++)
{   
    for (int j = 0; j < d; j++)
    {

        if (board[i][j] == tile)
        {
            //up
            if (i > 0)
            {
                if (board[i - 1][j] == (d*d))
                {
                    board[i - 1][j] = tile;
                    board[i][j] = (d*d);
                    return true;
                }
            }
            //down
            if (i < d)
            {
                if(board[i + 1][j] == (d*d))
                {
                    board[i + 1][j] = tile;
                    board[i][j] = (d*d);
                    return true;
                }
            }
            //left
            if (j > 0)
            {
                if (board[i][j - 1] == (d*d))
                {
                    board[i][j - 1] = tile;
                    board[i][j] = (d*d);
                    return true;
                }
            }
            //right
            if (j < d)
            {
                if(board[i][j + 1] == (d*d))
                {
                    board[i][j + 1] = tile;
                    board[i][j] = (d*d);
                    return true;
                }
            }
        }
    }
}
return false;
}
0

The issue depends on what you are loading into the board[][] array. If you are loading 0 as the smallest number, then d*d is too large. If you are starting at 1, then it should work. The point is that you can't have 0 and d*d stored in the array.

Then, there's a separate problem. The code allows accesses to data outside of the board[][] array. Look at the specific case where i (or j) equals d-1 and look at the following:

        //down
        if (i < d)
        {
            if(board[i + 1][j] == (d*d))
            {
                board[i + 1][j] = tile;
                board[i][j] = (d*d);

Say that d=3 and i=2. That means that the code is assigning tile to board[3][j], but that is an invalid index (i must be 0,1 or 2). This won't necessarily trigger an error, but the next line will introduce whatever represents a blank tile into board[i][j]. In short, the results are unpredictable because random data stored at that invalid index could satisfy the test condition, while the actual tile you seek is somewhere else.

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

1
  • Ah ok... I think I get it. I think your first point is mute as I don't have 0 and dxd stored at the same time (the array starts at 1). But your second point makes a lot of sense. If it's reaching for data outside of the array then that perhaps explains why it works fine if the blank is stored as dxd (as it probably won't find 9 or 16 stored outside of the array) but breaks if the blank is stored as zero (which it could run into outside of the array. I guess the fix is to change the first if condition to: if (i < (d - 1))? – MelJ79 May 16 '16 at 19:26

You must log in to answer this question.

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