# A wierd error in won function (fifteen)

when I run Check50 my program passes all the tests except one, the terminal spits out this weird cryptic error

:( 3x3 board: make sure game is winnable expected "Tile to move:", not "[2J[0;0H1-2-3|4-5-6|0-7-8 ftw!"

what could possibly be the cause of this considering that the game was won with a 4x4 board?

``````bool won(void)
{
int x = 1;
int count = 0;

for(int i = 0; i < d; i++)
{
for(int j = 0; j < d; j++)
{
if(board[i][j] == x)
{
x++;
count++;
}

if(count == (d * d) - 1)
{
return true;
}
}
}
return false;
}
``````

the hint is in

1-2-3|4-5-6|0-7-8

ie

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

Your won function will say that's a winning position, which it isn't.

That same sort of scenario doesn't occur during the 4x4 gameplay.

To see why this returns true when it shouldn't, add this as a line just before checking if the board value is x:

``````printf("board[%i][%i] = %i and x = %i\n", i, j, board[i][j], x);
``````

When you run it, you should (eventually) see this:

``````board = 1 and x = 1
board = 2 and x = 2
board = 3 and x = 3
board = 4 and x = 4
board = 5 and x = 5
board = 6 and x = 6
board = 0 and x = 7
board = 7 and x = 7
board = 8 and x = 8
``````

See what happens with board? It's got the blank tile (value 0) which should mean that the `won` is false, but your code simply goes to the next board place and checks if that is 7 and it is, and then to next is 8, and returns true.

You need to make sure that the tiles are in the right spot, not just in the right order.

• I tried writing the whole process on paper, but I still don't understand where the error is at, why is it spitting that output? where's the bug in my code? Sep 10, 2017 at 1:32
• @AshleyMoe I edited my answer to give example. Sep 10, 2017 at 1:44