0

In the 'Won' function I tried to do two things: check if the array is in order and to cater to the blank space (underscore in my case)

I used the counter method to check if each cell corresponds to an increasing counter.

I divided my code into two parts:

One, checking if board[d - 1][d - 1] == '_'. Here is my sub-code:

int counter = 1;

int underscore = d * d;

if (board[d - 1][d - 1] == '_')
{
    // assigning the value d*d to the last cell
    board[d - 1][d - 1] = underscore;

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

                // reassigning the _
                board[d - 1][d - 1] = '_';

                return false;
            }
        }
    }
}

In this above code my logic was to convert the value of dd cell to an integer value of dd so that my counter check can find a value equal to that of the value of counter at that cell.

Second, if board[d - 1][d - 1] != '_'. Here is my sub-code:

else
{
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            if (board[i][j] != counter)
            {
                counter++;

                return false;
            }
        }
    }
}

Lastly, I end the code with the below return value just before closing the won function.

return true;

On executing the command ./fifteen 3 < ~cs50/pset3/3x3.txt I got the following output

enter image description here

My program seems to be running well except the declaration of victory part. The return value seems to be always false.

Can you please suggest any modification of the logic/code to make this work?

0

In both code snippets, it tests whether a given array element is NOT equal to counter. If not, it increments counter for the next pass and then returns false. The problem is that this mixes what should be done for == and != into the same action. If they are ==, then the code should increment counter for the next pass, but if !=, then false should be returned.

As for the second code snippet, the whole snippet is unnecessary. If board[d - 1][d - 1] != '_' is true, then the blank is not in the correct location (and another number is in the lower right corner), so the game is not yet won. There's no need to check the entire sequence.

Lastly, (without seeing the full code and the context) the return true is a catchall clause and should return false, not true. True should only be returned when actually verified, not as a catchall.

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

| improve this answer | |
  • As suggested I revamped my code to test for equality rather than inequality. After a couple of more hours of use of printf and gdb, I finally was able to correctly win the game and finally check50. Thanks for your suggestion. – ABor Apr 30 '16 at 13:27

You must log in to answer this question.

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