# Thread: Tic Tac Toe program

1. ## Tic Tac Toe program

This is supposed to generate and print a tic tac toe program which is seeded by the user's input. It generates the tic tac toe board by using a variable which is assigned to randomly generate either a 1 or a 2, which is prints as X and Y. My problems currently are getting the function "validBoard" to work properly.

validBoard is supposed to check whether or not the board is valid by checking the sum of the 1s and 2s. If the sum is out of the range 15 to 12 then it is invalid. When I run, it says every board is invalid, even when they aren't.

Code:
`<code removed>`

2. So, all your p's (p1 through p9) are either 0 or 1. Therefore the maximum value is 9 and the minimum value is 0. Getting a sum between 12 and 15 will be very impossible.

3. Woops, I thought they were 1 and 2, thanks.

I also need to make a subsequent function after validBoard (which I call winnerBoard), which needs to determine who won (if anyone). I was thinking it would be possible to do this using a bunch of if-else-if statements, but it would be really redundant. Do you know a better way I could do this?

4. (1) You could use for-loops to "condense" your row checks and your column checks.
(2) You could use auxiliary counters (maybe not so easy in this case; easier to do while actually playing the game) so that putting an X in the upper left corner might increment Xrow[0], Xcol[0], and Xdiag[0]. If any of the counters get to three, it's a win.

5. Thanks.

Getting back to validBoard... if validBoard detected that it was invalid board, what would I need to add to get the program to stop running at that point?

6. Originally Posted by muzihc
Thanks.

Getting back to validBoard... if validBoard detected that it was invalid board, what would I need to add to get the program to stop running at that point?
You can use exit(1) which terminates the program all together. Or have something in main() like if (!validBoard) return 0;

7. Thanks c_nta.

I've hit another snag now. What I'm trying to do is have a function called winnerBoard check to see which pattern, if any, won. Then it returns a variable called "win", which holds 0 if there is no winner, 1 if O wins, and 2 if X wins. I'm then trying to use "win" as an input parameter into my function printWinner. For some reason printWinner isn't taking the variable though, it always sees win as 0, even when winnerBoard sees at as 1.

(note: as of right now winnerBoard is only partially complete and just for 'O' winning)

Code:
`<code removed>`

8. Let me guess: you just did winnerBoard(a, b, c, d, e, f, g, h, i) instead of win = winnerBoard(a, b, c, d, e, f, g, h, i).

9. Originally Posted by tabstop
Let me guess: you just did winnerBoard(a, b, c, d, e, f, g, h, i) instead of win = winnerBoard(a, b, c, d, e, f, g, h, i).
Yes, that must be it

Where would win = winnerBoard() go in place of just winnerBoard, though?
(I didn't know I had to do that)

10. Everywhere you call winnerBoard, you need to keep the result to pass to printBoard.

11. Great, it works now perfectly. Thanks a million.