0

I'm having trouble seeing where my logic is going wrong with my won() function for Game of Fifteen (Pset 3).

For example looking at my for loop, let's say d=4, i = 2, j = 1. Then board[i][j] = board[2][1] = 10, which is equal to the expression ((d*i)+(j+1)) = ((4*2) + (1+1) = 10. Therefore, 'false' is NOT returned for the first "if" statement, which is correct since 10 is indeed the entry for row 1, column 2. I've went through other i,j values as well, and everything seems to work out, so I have no idea where my program is going wrong.

"Check50" gives me the following error message: :( 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

Any suggestions on what I could be doing wrong?

bool won(void)
{
    // TODO
    if(board[d-1][d-1]!=0)
            {
                return false;
            }
    for(int i = 0; i < d; i++)
    {
        for (int j = 0; j< d; j++)
        {
            if(board[i][j] != (d*i)+(j+1))
            {
                return false;
            }
       }
    }
    return true;
}

2 Answers 2

2

What happens at i == j == (d-1)? That's the last tile and it should equal 0, but your test will fail because of that.

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

1

A trick I like to use when debugging indexed ranges (for loops, for example) is to see what happens at the upper and lower bounds of the ranges. A lot of times, the code breaks down there.

You must log in to answer this question.

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