# Game Of Fifteen Won Function

my won function for Game of Fifteen does not exit. check50 says it timed out waiting for the program to exit. My other functions for game of fifteen seem fine. Help would be appreciated!

``````    bool won(void)
{
// TODO
int loop = 1;
for (int i = 0; i <= d-1; i++)
{
for (int j = 0; j <= d-1; j++)
{
if (board[i][j] == loop)
{
loop++;
}
else
{
return false;
}

}
}

return true;
}
``````

It will always return false. Let's say d=3. That means i runs from 0 to 2 and j runs from 0 to 1. That means that there are 3x2 = 6 passes through the inner loop, so loop is incremented to 6, not 9.

The next issue is this: While the code will check that a row is in sequence, it doesn't compare the last element of one row to the first of the next. So, the following could be seen as a win, assuming the prior problem was fixed.

``````   4 5 6
1 2 3
7 8 0
``````

This would produce a false positive win.

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

• Ok, I updated my code to make it so that it iterates through every column in the rows and if that board tile equals the loop number starting at 1, keep incrementing until reaching the end and then check to see if that matches the board dimensions*dimensions and if it does return true.....too bad it still doesn't work. I don't know what I did wrong, thanks. – R_C Oct 6 '17 at 0:50
• Ummm, what's the range for j? You could also make it a little more efficient. When the if condition inside the two for loops fails, it could immediately return false, using an else clause. This would also make the first test clause of the final if statement unnecessary. – Cliff B Oct 6 '17 at 1:32
• Alright, I made the changes you suggested but it still is timing out while waiting for program to exit? – R_C Oct 6 '17 at 19:39