0
bool won(void)
{
// TODO
for(int r=0;r<d;r++)
{
    for(int c=0;c<d;c++)
    {
        if(c==r==(d-1))
        {
            if(board[r][c] != 0)
            {
                return true;
            }
            else
            continue;
        }
        else 
        {
            if(board[r][c] != ((d*r)+c+1))
            {
                return false;
            }
            else 
            continue;
        }
    }
}
return true;
}

Implemented the following code for the function won, but does not seem to be returning true to main. Can someone help me find the error?

0
  1. You cannot chain == like that. Use if (c == d-1 && r == d-1) to describe that. That's the only code-breaking mistake.
  2. There's no need for that else continue;, as there's no code behind.
  3. You don't even have to test for if(board[r][c] != 0). If all tiles but the one in the lower right corner have correct values, the last also should. Just do that if(board[r][c] != ((d*r)+c+1)) only if c != d-1 || r != d-1. Alternatively, you can even check for 0 by using (d*r+c+1)%(d*d), which wraps the numbers around to a range 0..d*d-1.
| improve this answer | |
  • Thank you . It works. – rajat008 Jul 28 '17 at 7:06

You must log in to answer this question.

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