0

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;
}
3

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[0][0] = 1 and x = 1
board[0][1] = 2 and x = 2
board[0][2] = 3 and x = 3
board[1][0] = 4 and x = 4
board[1][1] = 5 and x = 5
board[1][2] = 6 and x = 6
board[2][0] = 0 and x = 7
board[2][1] = 7 and x = 7
board[2][2] = 8 and x = 8

See what happens with board[2][0]? 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.

| improve this answer | |
  • 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? – Ashley Moe Sep 10 '17 at 1:32
  • @AshleyMoe I edited my answer to give example. – curiouskiwi Sep 10 '17 at 1:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .