0

I'm having an issue my won function to return true. When I use the 3x3.txt file I get an Illegal Move message when won should return true. Not really sure what I'm missing here.

Here is my code:

bool won(void)
{
    for (int i = 0; i < d; i++)
    {
        for (int j = 0; j < d; j++)
        {
            if (board[i][j] == (d*i)+(j+1))
            {
                if (board[i][j] == board[d][d] && board[i][j] == 0)
                    return true;
            }
        }
    }
    return false;
}
  • Can you explain the logic of if (board[i][j] == board[d][d] && board[i][j] == 0) statement? IMO board[d][d] should not exist. – sinister Dec 14 '14 at 2:56
  • You're right. I switched it out but still am getting the same error. I've updated the original question to reflect the new code. – Mike Ridley Dec 14 '14 at 3:58
  • Think about the case when board[i][j] is having space. there will be exactly one unique value of 0<=i<d and 0<=j<d, and that will satisfy board[i][j] != (d*i)+(j+1), returning false from your function. – sinister Dec 14 '14 at 6:41
  • Welcome to the site, Mike. Please don't "fix" the code in your original question. Instead, add some discussion of any changes you make, for example, "I replaced the inner if with this line: ..." We want questions to show the "wrong" code that goes with the original issue, so that when there's a helpful answer, readers can see the before-and-after picture. – Air Dec 15 '14 at 16:32
  • Ok, thank you for the notification. – Mike Ridley Dec 16 '14 at 14:26

You must log in to answer this question.

Browse other questions tagged .