How to prevent the latter function from being called twice

closed account (E8A4Nwbp)
In my visual tictactoe game, I check for a win like so:

1
2
3
4
5
6
7
8
9
10
11
12
void HelloWorld::CheckWin(int x, int y)

{

	Check3PiecesForMatch(0, 0, 1, 0, 2, 0);

	Check3PiecesForMatch(0, 1, 1, 1, 2, 1);

	Check3PiecesForMatch(0, 2, 1, 2, 2, 2);

	Check3PiecesForMatch(0, 0, 0, 1, 0, 2);
//... 


And then act accordingly to the win like so:

1
2
3

void HelloWorld::Check3PiecesForMatch(int x1, int y1, int x2, int y2, int x3, int y3)
{ //... 


However, if a player gets a double win :
xoo
oxo
xxx
then the latter function will be called twice, and do it's actions twice, inconveniently. What changes can I make to the former function to prevent the latter from being called twice?

Kind regards and thank you very much in advanced
Last edited on
if only check for match returned a bool that told whether it matched or not...

also, sometimes it is actually more efficient to just do more work than to try to prevent doing more work. The logical tests and extra variables and all the infrastructure to do that may be more actual program /cpu work, and your program looks like that could be the case.
Last edited on
closed account (E8A4Nwbp)
if only check for match returned a bool that told whether it matched or not...

I'm sorry, could you post a brief code example, the sentence you gave to me isn't too clear, not that it is bad, It's just overly simple
post a brief code example


bool HelloWorld::Check3PiecesForMatch(int, int, int, int, int, int);
if you do what he said ^^ you can return true or false on whether the function found a match or not in each call.
the you can chain the logic so that once any one of them becomes true, you stop calling the rest.

bool done = Check3PiecesForMatch(0, 0, 1, 0, 2, 0);

if (!done)
done = Check3PiecesForMatch(0, 1, 1, 1, 2, 1);
...repeat for the rest
closed account (E8A4Nwbp)
Thanks @jonnin , and Glad to have your 5500th post :D

Topic archived. No new replies allowed.