The won() function, which I thought was pretty straightforward, initially gave me some trouble. I had it trying to evaluate the if statement seen below in a loop that iterated over each position of the board.
bool won(void)
{
// TODO
for (int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
if(board[i][j] != (j + 1) + (i * d) || board[i][j] != 0 )
{
return false;
}
}
}
return true;
}
If the value at the given coordinate didn't match what was expected, (formula) OR Zero, then false. But if we find our expected values, we would iterate all the way through the loop and return true. But this wouldn't work. I ran printf to check values against my if statement check and everything was as expected. When 1 was at coordinates [0][0], my if statement was checking to see if the value there was other than 1 or zero. So it should have continued the loop, but instead it returned false.
I had to modify the code to the following before it would work.
bool won(void)
{
// TODO
for (int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
if(board[i][j] == (j + 1) + (i * d) || board[i][j] == 0 )
{
continue;
}
else
return false;
}
}
return true;
}
Can anyone shed some light on this? I'm guessing that I've misunderstood some of the finer points of if statement logic. But "if not 1 or zero, return false" seems straightforward.