# pset 3 won() function always return false

I'm stuck on the logic in pset3 won() function. Apparently, the function just doesn't return true no matter what. Here is my code.

``````bool won(void)
{
int n = 1;
//iterate over the board
for (int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
//check to see if last space is 0
if (board[d-1][d-1] != 0)
{
return false;
}
//check if number increasing
else if (board[i][j] != n)
{
n = n+ (d*i)+j;
return false;
}
}
}
//continue to check last row after this line
return true;
}
``````

Please note that I am including n in hope that, at board, n =1, board, n = 1+ 3*0+ 1 =2; board, n = 1+ 3*1+0 = 4; board , n = 1+ 3*1+ 1= 5.

First, note that you change n only if something is wrong.

Let's suppose that board  = 1 as it should be. Then, this condition

``````else if (board[i][j] != n)
{
n = n+ (d*i)+j;
return false;
}
``````

doesn't work so you still have n == 1, and continue to check board. Suppose it is 2 as it should be. You check

``````else if (board[i][j] != n)
{
n = n+ (d*i)+j;
return false;
}
``````

as n is still == 1, board == 2, condition is true so you return false and game is over

also, even if you correct this, you still have quite a misleading assignment:

``````n = n + (d*i) + j;
``````

indeed, if you checked successfully n = 3 for board, next iteration it's going to be n = n + (d*i) + j = 3 + (d*0) + 2 = 5, while you need to check n == 4.

try to use just n = n + 1 or (d*i) + j without n = n +, if you'd prefer

• Whoops! Too focused on n value and forgot that huge logical mistake there. – Ha Tran Jun 19 '17 at 9:59