0

So this is my code:

bool won(void)
{
// check number
int number = 1;

for(int row = 0; row < d; row++)
{
    for(int column = 0; column < d; column++)
    {
        if(number == (d * d) - 1)
        {
            return true;
        }
        if(board[row][column] == number)
        {
            number++;
        }
        else
        {
            number = 1;
            return false;
        }
    }
}

return false;
}

When I play the game, and beat it, the function works, check50 gives me a thumbs up for the 4x4, but for the 3x3 it doesn't, where am I going wrong?

1

Interesting logic error. This could fail on any size board. Let's say that the last row on your 3x3 is 7 0 8 and that the rest of the board is in a winning condition. Here's what is happening. The code checks that 7 is in the right position and increments number. On the next pass through the loop, the first thing it checks is if number == d*d-1. Since it is, it reports a win. This can occur if the board is correct, or if the last two tiles are reversed.

The code in this algorithm needs to both check that number == d*d-1 and that the last non-blank tile is in the right position. OR, it could check that all the blank tiles are in the correct positions and then check for the blank tile in the last spot.

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

| improve this answer | |

You must log in to answer this question.

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