0

I've used check50 to confirm that everything is working except for my won function. I've tried many different variations of the below code, and I'm not sure why it's not working.

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

The while loop checks that m doesn't get to d * d, but the while loop only executes once. The real loop is the nested for loops. They will run and increment m over and over with no check whether it goes past d*d.

In effect, the for loops are checking that the tiles are in sequence, including the last one. The for loops will check whether the blank tile == d * d, not 0. So, it's failing at the last step. If you don't follow, work it out on paper or trace it in debug.

If you think about it, the while loop is extraneous code. With a slight change, the for loops are sufficient. I'll let you figure out how.

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 .