0

I have been working on my won function for days now, and have read all the relevant posts on this site but still cant work out where I'm going wrong. The code does not return true when passed the 3x3.txt input even when all the tiles are correctly arranged in order. I have tried to avoid checking the bottom right tile in the loop, according to the advice I've read on previous posts, but I'm not getting anywhere.

My logic is that is if each tile is equal to the counter value, which increments with each pass, and the counter is set to a highest value of (d*d)-1, the loop will check that all the tiles are in order bar the tile at board[d-1][d-1]. If this is true, the blank tile must therefore be at board[d-1][d-1] as all the other tiles have been accounted for, and the game should be won. But its not working and as a beginner to programming, I don't understand why.

Here is my code:

bool won(void)
{
    int counter = 1;
    while(counter < d*d-1)
    {
        for(int i = 0; i < d; i++) //iterate over rows
        {
            for(int j = 0; j < d; j++) //iterate over columns
            {
                while(board[i][j] != board[d-1][d-1])
                {
                    if(board[i][j] == counter)
                    {
                        counter++;
                    }
                    else
                    {
                        return false;
                    }
                }    
            }
        }           
    }
    return true;
}

Please can someone point me in the right direction?

  • thank you so much cliff, your advice was right on the mark! I changed in the inner while loop to an if condition, ie if(board[i][j] != board[d-1]d-1], and now everything works!! I realised from your hint that in as much as Im avoiding to check tile at board[d-1][d-1] in the loop, the first while statement limiting the counter was superfluous, and removed it. – adam kirsch May 24 '17 at 22:06
2

You are definitely on the right track and are very close.

Hmmm.... Let's look at the inside block of code:

            while(board[i][j] != board[d-1][d-1])
            {
                if(board[i][j] == counter)
                {
                    counter++;
                }
                else
                {
                    return false;
                }
            }    

On the first pass, i=j=0, board[i][j] = 1 and counter = 1. So, the if statement is true, counter is incremented, and the code loops back to the while statement. But remember, this is a while statement. None of the controlling values have changed, so it is going to loop again. Now, board[i][j] = 1 and counter = 2. The else clause executes and it returns false. This code would never return true for any arrangement of tiles.

This test needs to be tested one time here, not in a loop. You have the loops established with the for loops. I'll leave it to you to figure out how to do this just once. It should be simple enough to figure out. IF you can't, let me know. ;-)

Next, although it doesn't break your code, why do you think you need the outer while loop? I'll let you chew on that one on your own. ;-)

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 .