Can't seem to find the problem in the function, can you?

Started by
5 comments, last by Estauns 21 years, 8 months ago
Okay, so let me set this up for you. This function loops through a 13x13 array (m_gameGrid) and checks to make sure EVERY spot''s m_iState is 0, except for the middle spot [6][6], it has to be set to 1. The problem is, I seem to win the game, as long as ANY blocks are 1, within either the 6th row OR the 6th column, AND the middle spot, which obviously isn''t what I want. So lets show you this visually.. 000 010 <--- True Winner 000 010 111 <--- Shouldn''t be a winner, but is anyways.. 010 100 010 <--- Isn''t a winner. 000 Heres the code,
  
void CGameGrid::CheckWinner(void)
{
	int iWinner = 0;
	if(m_gameGrid[6][6].m_iState == 0)
	{
		iWinner += 1;
	}

	// No point going through the loop if the middle isn''t blue.

	if(iWinner == 0)
	{
		for(int iRow = 0; iRow < GRID_ROWS; iRow += 1)
		{
			for(int iColumn = 0; iColumn < GRID_COLUMNS; iColumn += 1)
			{
				if(iRow != 6 && iColumn != 6)
				{
					if(m_gameGrid[iRow][iColumn].m_iState == 1)
					{
						iWinner += 1;
					}
				}
			}
		}
	}

	if(iWinner == 0)
		m_bSolved = true;
	else
		m_bSolved = false;

	return;
}
  
"Where genius ends, madness begins."Estauns
Advertisement
well.

make a check to see if the first and the third are equal to 000

if not, its not a winner.
else it is.


I''m not completly sure I got your problem right
I don''t think you did ^_^

ALL squares in the m_gameGrid have to be 0, or "off" and the middle has to be 1, or on.

And I did a 3x3 example to show you, but the grid can have odd shapes, sometimes it''ll be like a capital T instead of a 3x3 grid. Things like that.
"Where genius ends, madness begins."Estauns
Hm. From your nebulous description, I spotted three potential problem spots:

1) the need for an "else if" instead of an if after you check to see if the middle square is 1 (right below the "// No point going through the loop..." comment)
2) You''re incrementing iWinner when any position is equal to 1 within that loop. That didn''t seem to be your intention.
3) Finally, you''re setting bSolved to true if iWinner is 0, which seems a bit hokey.

All in all, that code is rather ugly, and it doesn''t seem to come close to doing what you described.

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

Incrementing iWinner by 1 IS What I want to do.

When the game finds a square with a state of 1, it increments iWinner by 1, so that it no longer equals 0 and thus, doesn''t declare the puzzle solved.
"Where genius ends, madness begins."Estauns
As far as I can tell, you want to check every square except (6,6) in the loop, and if any of them are 1, the player hasn''t won. If that''s the case your if statement should read:

if((iRow != 6) || (iColumn != 6))

which is equivalent to if(!(iRow == 6 && iColumn == 6))
Ah. I see. That just wasn't intuitive to me, because the way booleans work, any non-zero value is true, so I would've thought that Winner being greater than one would make it true.

Anyway, that code works for me.

EDIT: After, that is, I make the change that Krunk suggested above (I figured it out before I read his post though).

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on August 17, 2002 8:17:20 PM]

[twitter]warrenm[/twitter]

This topic is closed to new replies.

Advertisement