Thread: tic tac toe problem-so close yet so far :(

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    tic tac toe problem-so close yet so far :(

    Attached (Attachment 9405) is what I have so far for a program that plays Xs and Os between you and the computer. The problem is that the program just stops running sometimes and i don't know why! I have a feeling it either has something to do with the computers turn or when either player tries to take a move that has already been taken. Any help would be really really appreciated as I've been working on this for 3 days now and I just can't spot the problem! Sorry for the lack of comments, but its mostly self explanatory.
    Thanks in advance,
    Debz

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your program offers the choice to play second, but if you do, the whole program prints a message and ends.

    Usually, input that uses scanf() will blow up from time to time because of pollution in the keyboard buffer, being left behind. Maybe a user's finger hits two keys as he / she pushes down or maybe it's the omnipresent \n newline char, when you hit enter.

    You can help clean it up, by pulling a char off the keyboard buffer immediately after every scanf(), using getchar(). Using fgets() and sscanf(), etc., allows more control still.

    You don't need to srand() every time you want a random number. Just once, at the start of the program, is all you need (or can use).

    If you put variables anywhere else except at the start of the function, then people with older compilers can't compile your code without moving the declaration of those variables. Not fun, and most may not bother.

    Your program ran OK when I chose first move. Clean up the keyboard buffer, and see if that fixes your problem. Don't bother with fflush(stdin), doesn't work, because fflush() works on output buffers, not input.

    I'll play your TTT more later, and see what happens then. Any other details of the problem would be helpful.

    And Welcome to the Forum!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  2. Magic Square and Tic Tac Toe
    By curlious in forum Game Programming
    Replies: 3
    Last Post: 07-28-2003, 05:50 PM
  3. Tic Tac Toe display problem
    By Munkey01 in forum Game Programming
    Replies: 9
    Last Post: 03-01-2003, 06:35 PM
  4. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 04:21 PM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM