Thread: Tic Tac Toe program

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    33

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    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. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    (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. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    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. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by muzihc View Post
    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. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    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>
    Last edited by muzihc; 10-21-2008 at 06:51 PM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Quote Originally Posted by tabstop View Post
    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. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Everywhere you call winnerBoard, you need to keep the result to pass to printBoard.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    33
    Great, it works now perfectly. Thanks a million.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe!
    By Sinensis in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 04:40 PM
  2. Check tic tac toe winner
    By cjmdjm in forum C++ Programming
    Replies: 3
    Last Post: 11-04-2005, 12:41 PM
  3. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  4. My First c++ program : TIC TAC TOE !
    By renderstream in forum C++ Programming
    Replies: 7
    Last Post: 03-07-2004, 04:58 PM
  5. Need help with Tic Tac Toe
    By Zerostatic in forum C++ Programming
    Replies: 19
    Last Post: 07-19-2002, 07:20 PM