0

So I have looked and seen very similar (non-working) solutions, but my code is very slightly different from what I've seen here, and the answers that I've seen haven't really helped me. I'm a complete noob, so bear with me. My code does not seem to returning true when checking to see if the numbers are in order. It seems like it should work to me, but it's obviously not. If I need to clarify this question in some way, please let me know how/what info to include, and I can edit for clarity.

bool won(void)    
{        
    int count = 0;

    for(int i = 0; i < (d - 1); i++)
    {
        for(int j = 0; j < (d - 1); j++)
        {
            if(board[i][j] == count +1)
            {
                count++;
            }
            if(board[i][j] != count + 1)
            {
                return false;
            }
        }
    }
    return true;    
}
1

I see a couple of issues, although I don't immediately see why it is "timing out".

The code depends on two if statements that are NOT interdependent. Let's walk through the code to understand what happens. Say that the code is testing the first square, which should contain 1. Assume that it does. The first if statement evaluates as true, so count is incremented to 1.

Now, the second if statement is evaluated. It will also evaluate as true because board[i][j] is 1, count + 1 now equals 2, and 1 != 2. Since it evaluated as true, the return false statement executes.

In short, this code will never produce a correct true result.

The second if statement is the problem. Instead of a second IF statement, you might consider an if/else statement block. IF/ELSE is used to do either/or processing like you are trying to do here.

Next problem: I see no code to deal with the blank square in the last position.

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

| improve this answer | |
  • The timeout is from not finding "won" condition, so programme keeps asking for input instead of terminating, which the checker is waiting for. – Blauelf Feb 20 '19 at 6:04
  • That would be check50 timing out, not fifteen. I thought check50 was designed to detect that condition. – Cliff B Feb 20 '19 at 6:09
  • If I remember correctly, it detects by checking whether the programme terminates. If it doesn't, timeout, test failed. – Blauelf Feb 20 '19 at 9:53

You must log in to answer this question.

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