bool won(void)
{
int counter = 1; // sets counter to 1 (as the [0][0] will start from 1 upwards to d*d-1.)
for (int i = 0; i < d; i++) // goes over the board to make sure everything is at the value it should be.
{
for (int j = 0; j < d; j++)
{
if (counter == (d * d) - 1) // && board[d-1][d-1] == 0) // Basically, makes sure the all but one of the tiles are the same. If the last tile is 0 and the rest are the same = victory.
{
return true;
}
if (board[i][j] == counter) // checks vs the counter then increases counter by 1 for the next round.
{
counter++;
}
}
}
return false;
Let's say this is a 3x3 board.
In my mind the way I hoped it would work: 1) Counter is at 1, checks with boards [0][0] which should be 1, if it is, increment counter and proceed to [0][1], if its 2=2, proceed further and so on.
I thought it would stop only if the counter finds 8 matches which would only be possible in a an ascending manner.
However, the 3x3 'wins' if the board is at: 1 2 3 4 5 6 _ 7 8
So my solution was to also check whether the last tile is 0 (currently I left it past //. But this just feels wrong and potentially buggy in the real world.
My question is, why on earth does it stop when the counter hits 6? Even though it's checking if the counter is equal to 8 (d*d-1 = 3x3-1)
I'm at a complete loss and have no idea what is wrong with this.