Public Group

# Am I using && wrong?

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

## 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 on other sites
Your code looks correct. Step through your code in the debugger and determine why and where Won() returns true.

##### Share on other sites
if((boardarray[1][1] == x) && (boardarray[1][1] == x) && (boardarray[1][1] == x)) return true;

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

##### Share on other sites
Quote:
 Original post by ToohrVykYour code looks correct. Step through your code in the debugger and determine why and where Won() returns true.

:| It's always something like that isn't it?

##### Share on other sites
Strangely enough I now have a bigger bug in my code related to detection of what square I'm clicking and stuff. But I think I should figure that one out myself.

##### Share on other sites
Quote:
 Original post by BringBackFuturamawhenever 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.)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634131
• Total Posts
3015723
×