0

Whenever I run Check50 my program passes all the tests except the last two, the terminal spits out

:( 3x3 board: make sure game is winnable timed out while waiting for program to exit

:( 4x4 board: make sure game is winnable timed out while waiting for program to exit

I believe that the problem might be in my won function but here's the entirety of my program, can someone please point out what's wrong with my code?

void init(void)
{
    int value = (d * d) - 1;

    for(int i = 0; i < d; i++)
    {
        for(int j = 0; j < d; j++)
        {
            board[i][j] = value;
            value--;
        }
    }

    if(d % 2 == 0)
    {
        int swap = board[d - 1][d - 2];
        board[d - 1][d - 2] = board[d - 1][d - 3];
        board[d - 1][d - 3] = swap;
    }
}

/**
 * Prints the board in its current state.
 */
void draw(void)
{
    for(int i = 0; i < d; i++)
    {
        for(int j = 0; j < d; j++)
        {
            if(board[i][j] == 0)
            {
                printf(" _ ");
            }
            else
            {
                printf("%2i ", 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)
            {
                if(j > 0)
                {
                    if(board[i][j - 1] == 0)
                    {
                        int temp = tile;
                        board[i][j] = board[i][j - 1];
                        board[i][j - 1] = temp;

                        return true;
                        draw();
                    }
                }

                if(j < d - 1)
                {
                    if(board[i][j + 1] == 0)
                    {
                        int temp = tile;
                        board[i][j] = board[i][j + 1];
                        board[i][j + 1] = temp;

                        return true;
                        draw();
                    }
                }

                if(i > 0)
                {
                    if(board[i - 1][j] == 0)
                    {
                        int temp = tile;
                        board[i][j] = board[i - 1][j];
                        board[i - 1][j] = temp;

                        return true;
                        draw();
                    }
                }

                if(i < d - 1)
                {
                    if(board[i + 1][j] == 0)
                    {
                        int temp = tile;
                        board[i][j] = board[i + 1][j];
                        board[i + 1][j] = temp;

                        return true;
                        draw();
                    }
                }
            }
        }
    }
    return false;
}

/**
 * Returns true if game is won (i.e., board is in winning configuration),
 * else false.
 */
bool won(void)
{
    int x = 0;

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

                if(board[i][j] == d * d)
                {
                    return true;
                }
            }
        }
    }
    return false;
}
1

This if(board[i][j] == d * d) looks problematic. What tile on a 4x4 board will be 16? The max tile on a 4x4 board is 15! The only tile that is not "in order" on a winning board is board[d - 1][d - 1].

There's a problem here if(board[i][j] < board[i][j + 1]) too. When j reaches d - 1, program will be testing board[i][d]. But the max subscript is d - 1.

Think about won as the "inverse" of init. You know precisely what value should be in each board location. Starting with (d * d) - 1 and decreasing...until the last location which is "special".

| improve this answer | |

You must log in to answer this question.

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