1

I'm still stuggling with the game of fifteen, and I could make it works. However, when I run check50 it displays

:( 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

Every change I made, I get the same error...
I know that it's an error in my WON function, that keeps prompting for input instead of exit with code 0. But I couldn´t solve it so far...

My code initially was

bool won(void)
{
    // TODO
    int n = 0;
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            n ++;
            if (board[i][j] != n)
            { 
                return false;
            }
        }
    }
    return true;
}

Now, it's

bool won(void)
{
    // TODO

int n = 1; 

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

I'd be very grateful if you could hint me what I'm doing wrong.

1 Answer 1

1

HINT: What is the value of the lower right hand corner tile supposed to be when the game is won? What is your code looking for?

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

15
  • I understand it's 0. I used to have in the if statement If (board[i][j] ==n && board[d-1][d-1] == 0) then return true, but the check50 went crazy with that...
    – Maggie
    Commented Jul 12, 2016 at 0:34
  • What happens when i and j both equal d-1? n would equal d*d, but the tile would be 0, so the test would fail on the first half.
    – Cliff B
    Commented Jul 12, 2016 at 0:45
  • Oh I see. Should the iterations run til i and j < d - 1?
    – Maggie
    Commented Jul 12, 2016 at 1:07
  • Why don't you test it and see what happens? ;-)
    – Cliff B
    Commented Jul 12, 2016 at 2:18
  • Check50 got all red, only green exist, compiles and initializes :'(
    – Maggie
    Commented Jul 12, 2016 at 2:47

You must log in to answer this question.

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