Jump to content
  • Advertisement
Sign in to follow this  
BringBackFuturama

Am I using && wrong?

This topic is 4411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright, so I'm doing a basic tic-tac-toe program using C++ and SDL, which you guys helped me with a bit before. I have it almost done, but I've encountered a major bug, and I think I've tracked down where it is, I just can't see how it's not working.
if(Won())
{	
    for(int i = 0; i < 3; ++i)	
    {
	for(int j = 0; j < 3; ++j)
	std::cout << boardarray[j] << ' ';
    }	
    break;
}

This is the section of my code which checks the function Won() which is this:
bool Won()
{
	int x = 1;
	if(player == two) ++x;
	if((boardarray[0][0] == x) && (boardarray[1][1] == x) && (boardarray[2][2] == x)) return true;
	if((boardarray[0][2] == x) && (boardarray[1][1] == x) && (boardarray[2][0] == x)) return true;
	if((boardarray[0][0] == x) && (boardarray[0][1] == x) && (boardarray[0][2] == x)) return true;
	if((boardarray[1][1] == x) && (boardarray[1][1] == x) && (boardarray[1][1] == x)) return true;
	if((boardarray[2][0] == x) && (boardarray[2][1] == x) && (boardarray[2][2] == x)) return true;
	if((boardarray[0][0] == x) && (boardarray[1][0] == x) && (boardarray[2][0] == x)) return true;
	if((boardarray[0][1] == x) && (boardarray[1][1] == x) && (boardarray[2][1] == x)) return true;
	if((boardarray[0][2] == x) && (boardarray[1][2] == x) && (boardarray[2][2] == x)) return true;
	return false;
}

I think this is pretty much correct. It basically checks ever possible winning condition and returns true if one of them fits. But something is wrong with this, since whenever I click one box on the game board it exits through the if(Won()) break; loop even though the output it prints to the stdout file only has the box I clicked with a value. So there's something wrong with the Won() function. But what could it be? Am I imporperly using the && statement?

Share this post


Link to post
Share on other sites
Advertisement

if((boardarray[1][1] == x) && (boardarray[1][1] == x) && (boardarray[1][1] == x)) return true;


4th line. Looks fairly suspect to me...

Share this post


Link to post
Share on other sites
Quote:
Original post by BringBackFuturama
whenever I click one box on the game board it exits through the if(Won()) break; loop

Just to clarify: "if" is not a loop. The break does not make sense in this code snippet. (But I suppose the break belongs to some outer loop which we cannot see here.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!