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?