0

here's my code.

bool won(void)
{
    int counter = 1;

    if (board[d -1][d -1] == '_')
   {
       for(int i = 0; i <d; i++)
       {
           for(int j = 0; j < d; j++)
           {
               if(board[i][j] == counter && counter < (d*d))
               {
                   counter++;
               }
           }
       }
       return true;
   }
    return false;
}

my code shows error when checked with check50. :( 3x3.txt wins the 3x3 board \ expected an exit code of 0, not a prompt for input :( 4x4.txt wins the 4x4 board \ expected an exit code of 0, not a prompt for input

i don't know what mistake i'm making. Also when i changed the '_' with 0, the whole game showed problem .

0

There are a couple of problems. First, changing '_' to 0 is the correct fix to one of them. When you compare to an underscore, you're actually comparing to the numeric ASCII value of an underscore. It needs to be compared against 0.

But, fixing that problem reveals another, larger problem. The logic of checking all of the non-blank tiles is flawed. As written, the code checks to see that the numbers are in sequence. The problem is that when a number is found to be out of sequence, it doesn't do anything about it. The loop just keeps going until the end and then a return true; is executed. Instead, when a number is found to be out of sequence, it should immediately return false.

This should also teach you a hidden lesson. Making a change that seems to make a lot more errors isn't necessarily a bad thing. Sometimes, big errors or a lot of errors can be hidden by a small one. It may appear that the change was breaking code when it was actually fixing one bug and revealing more bugs! You have to develop the skill to recognize the difference between a fix and a non-fix.

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 .