# tic tac toe question (logic question)

• 06-09-2003
revelation437
tic tac toe question (logic question)
assuming i have everything else working, other than the logic to figure out if 3 in a row have been made or not

also assuming i am working with indexes for each of the boxes (0-8) and not using a 2d matrix

other than checking for every combo there is to win (i.e 0,1,2 or 0,4,8) is there a way to check to see if 3 in a row have been made?

i mean, i CAN check for every single combo, but i dont think thats good programming nor really efficiant
• 06-09-2003
Unregd
It's a little simpler with a two-dimensional matrix, but I'm sure there's a way to do this using your method, too:

Consider this: If the top left space does not have the winning token, you can throw out three different winning combinations (horizontal, vertical, and diagonal). If you do this recursively, you can eliminate combinations that would not result in end of game. This way, you won't need to run through every single possible combination every turn of the game.
• 06-09-2003
revelation437
i dont quite getcha
if the topleft isnt a winning token, then yea, the 3 and the only 3 winning combos would be thrown out. how would u do this recursively?
• 06-09-2003
Unregd
Second thought, not recursively. You can nest ifs to do this:

Code:

```if (space[0] == token) {     if (space[1] == token) // row 1 across check     {         ...     }     else if (space[3] == token) // diagonal left to right check     {         ...     }     ... // column 1 down check } else if (space[1] == token) // column 2 check {     if (space[4] == token)     {         ...     } } ...```
If you do this correctly, some combinations will not have to be checked. If you really don't want to check combinations that wouldn't be winning anyway, you could only check possible combinations that have had a token placed on them, but that would be more work than the easy way.

Another possibility is that, when a token is placed, the function that sets it on the tic tac toe board could check only the winning combinations for that space. To make sure all spaces haven't been taken, you'll probably want to increment a counter after each space is successfully taken. If there is no win after the counter reaches 8, it's a draw.