0

Help!! I can't get pass check50 for won with the message 'expected prompt input not exit code'. I thought we supposed to expect exit code 0 (here is another discussion on this matter: expected an exit code of 0, not a prompt for input WON)

My code for won is as follow;

    /**
 * Returns true if game is won (i.e., board is in winning configuration), 
 * else false.
 */
bool won(void)
{
    int end_grid = (d * d) - 1;

    // accept won if tile ascending from top left to bottom right
    // iterate over array from row to column
    if(board[0][0] == 1 && board[d - 1][d - 1] == (d *d) - 1)
    {
        for(int i = 0; i < (d - 1); i++)
        {
            for(int j = 0; j < (d - 1); j++)
            {
                board[i][j] = end_grid;
                end_grid++;
                return true;
            }
        }
    }
    return false;
}

Here is my check50 result https://sandbox.cs50.net/checks/e8c49b0bc652491a83be21ea105c00f1

Could anyone please point out to me where I made the mistake? I really appreciate some help.

1 Answer 1

1

Time for some tough love here. There are a lot of problems - almost every line of code. The error basically says that you're getting a premature false positive from win.

Let's look at the if statement. It checks for 1 in the first or upper left tile, which is correct. Next, it looks for (d*d)-1 in the lower right. If all the tiles contained a number in sequence, that tile would contain d*d. BUT, that's a moot point, since the lower right tile is supposed to be blank, meaning that it is supposed to contain 0.

Then there's the for loop statements. for(int i = 0; i < (d - 1); i++) This will cause i (and similarly, j) to stop after executing the loop with i == d - 2. You should understand why this is a problem.

Next, you're reassigning the contents of endgrid to every tile as you loop through them (sort of - see below). The won() function should not change any of the contents of the board array. It should only look at them.

Next is endgrid itself. It's set at (d*d)-1 to start and is incremented from there. Why? If you're using it to check values, it should start at 1.

Finally, I said "sort of" earlier. The next line is a return true statement. Since it is an unconditional return, it's going to terminate the won() function the first time it is hit, so only the first tile will be processed. If the for loops ever execute, you'll get a false positive from won.

BUT then again, the only way that the return true will ever execute, and also the only way that a win will be reported, is if the first or upper left tile is 1 and the lower right tile is (d*d)-1, or, for example, if d==3, then the lower right tile would have to be 8. You can test this with your code by moving the tiles to that position. (The order of the rest of the tiles wouldn't matter.)

You have a lot of work to do here. Depending on what changes you make, it may or may not work, or have additional problems. Once you rework it, if you still can't get it working, please post a new question with new code and details, rather than changing this one.

You should also try running the code with the debugger (debug50), stepping through the code and monitoring the contents of your vars, including the board array. Make sure that each line of code is doing what it is supposed to do.

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

3
  • Thanks heaps, Cliff for pointing out the mistakes! I blame this on fatigue from hours and hours of staring at the computer trying to work out game of fifteen. I've just successfully passed check50 after went through your guidance and looked carefully at my code. You're a legend!!
    – MTreacy
    Commented Oct 9, 2016 at 8:53
  • lol, I was thinking of asking when the last time you slept was, but didn't think it would be very nice of me. ;-)
    – Cliff B
    Commented Oct 9, 2016 at 8:59
  • Hehehe....That probably will be my routine now (no sleep) as I'm trying to rush through to complete the course by December this year. 'Ve been slacking for the last 9 month by only completing 2 Psets and suddenly realised the dateline is closing in. :-)
    – MTreacy
    Commented Oct 9, 2016 at 9:17

You must log in to answer this question.

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