Tic Tac Toe Program

This is a discussion on Tic Tac Toe Program within the C Programming forums, part of the General Programming Boards category; I'm creating a tic tac toe game. This is what i want to do. get the user input (a number ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    41

    Question Tic Tac Toe Program

    I'm creating a tic tac toe game. This is what i want to do. get the user input (a number 1-9 the corresponds to the spaces on the board) and replace it with an X or O in the array tic_tac_toe. if they want an X in postion 4 how do i write the function to replace 4 with X? This is what i have so far

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    #define NUMBER_OF_SQUARES 9 
    
    char tic_tac_toe[NUMBER_OF_SQUARES]={'0', '1','2', '3', '4', '5','6', '7', '8'}; 
    void print_board(); 
    void clear_board(); 
    char get_user_character(char); 
    void user_move(); 
    
    int main() 
    { 
    
    char choice; 
    int tic_tac_toe_board; 
    clear_board(); 
    print_board(); 
    get_user_character(choice); 
    tic_tac_toe[user_move-1]=user_representation; 
    return 0; 
    } 
    
    
    void clear_board(void) 
    { 
    int i; 
    for (i = 0; i < 9; i++) 
    { 
    tic_tac_toe[i] = ' '; 
    } 
    } 
    
    void print_board(void) 
    { 
    char tic_tac_toe[NUMBER_OF_SQUARES]={'0', '1','2', '3', '4', '5','6', '7', '8'}; 
    
    printf("  %c | %c | %c\n", tic_tac_toe[0], tic_tac_toe[1], tic_tac_toe[2]); 
    printf("----+---+----\n"); 
    printf("  %c | %c | %c\n", tic_tac_toe[3], tic_tac_toe[4], tic_tac_toe[5]); 
    printf("----+---+----\n"); 
    printf("  %c | %c | %c\n", tic_tac_toe[6], tic_tac_toe[7], tic_tac_toe[8]);        
    } 
    
    char get_user_character(char choice) 
    { 
    printf("play with X or play with O\n"); 
    scanf("%c", &choice); 
    
    return (choice); 
    } 
    
    
    void user_move(void) 
    { 
    printf("enter what box number that you\n"); 
    printf("would like to place you move in> \n") 
    scanf("%d",

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >if they want an X in postion 4 how do i write the function to replace 4 with X?
    Code:
    tic_tac_toe[choice-'0'] = 'X';
    You could also define choice as an int, then you wouldn't need to subtract '0' from choice, but this would introduce other problems.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    Could you use a switch or if statment. i.e. if move is 4 = tic_tac_toe[choice]?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Could you use a switch or if statment. i.e. if move is 4 = tic_tac_toe[choice]?
    Do you mean like:
    Code:
    switch (choice)
    {
    	case '0':
    		/* do stuff */
    		break;
    	case '1':
    		/* do stuff */
    		break;
    	case '2':
    		/* do stuff */
    		break;
    	case '3':
    		/* do stuff */
    		break;
    	case '4':
    		/* do stuff */
    		break;
    	case '5':
    		/* do stuff */
    		break;
    	case '6':
    		/* do stuff */
    		break;
    	case '7':
    		/* do stuff */
    		break;
    	case '8':
    		/* do stuff */
    		break;
    	default:
    		/* Invalid coordinate */
    		break;
    }

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    can i do this? Shouldn't this work?
    Code:
    void user_move(void)
    {
            char choice;
            static int i;
            printf("enter what box number that you\n");
            printf("would like to place you move in> \n");
            scanf("&#37;d",&i);
    if (i<0 || i>8)
    {
                    printf("invalid move\n");
    }
    else
            tic_tac_toe[i] = choice;
    
            print_board();
    
    }

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Sure, that will work fine. A problem can occur if the user accidently enters something besides a number. Better would be to use fgets() to read what the user entered, then instead of scanf(), use sscanf().

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    The indentation needs fixing to begin with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    I got the program to work but now the get_usermove function skips the scanf.It doesn't let me enter a positon to move it just skips it and prints invalid move....any suggestions?
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    
    #define NUMBER_OF_SQUARES 9 
    
    char tic_tac_toe[NUMBER_OF_SQUARES]={'0', '1','2', '3', '4', '5','6', '7', '8'}; 
    void print_board(char*tic_tac_toe); 
    void clear_board(); 
    char get_user_character(); 
    char user_move(char*); 
    
    int main() 
    { 
    int use_move; 
    char choice; 
    char tic_tac_toe_board; 
    char user_representation; 
    
    clear_board(); 
    print_board( tic_tac_toe); 
    get_user_character(); 
    user_move(&choice); 
    print_board(tic_tac_toe); 
    // return EXIT_SUCCESS; 
    } 
    
    void clear_board(void) 
    { 
    int i; 
    for (i = 0; i < 9; i++) 
    { 
    tic_tac_toe[i] = ' '; 
    } 
    } 
    void print_board(char *tic_tac_toe) 
    { 
    
    printf("  &#37;c | %c | %c\n", tic_tac_toe[0], tic_tac_toe[1], tic_tac_toe[2]); 
    printf("----+---+----\n"); 
    printf("  %c | %c | %c\n", tic_tac_toe[3], tic_tac_toe[4], tic_tac_toe[5]); 
    printf("----+---+----\n"); 
    printf("  %c | %c | %c\n", tic_tac_toe[6], tic_tac_toe[7], tic_tac_toe[8]);        
    } 
    
    char get_user_character() 
    { 
    char choice; 
    printf("Play with X or play with O\n"); 
    scanf("%c", &choice); 
    return (choice); 
    } 
    
    char user_move(char *choice) 
    { 
    char i; 
    printf("Enter what box number that you\n"); 
    printf("would like to place you move in>\n"); 
    scanf("%c", &i); 
    
    if (i <0 || i>8) 
    printf("invalid move\n"); 
    else 
    tic_tac_toe[i] = *choice; 
    
    
    } 
    output:
    
        |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    Play with X or play with O
    X
    Enter what box number that you
    would like to place you move in>
    invalid move
        |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    i should be an int, not a char. (There is no way to get a character with value 0 from the keyboard that I know of, and 1-8 are pretty tricky too.)

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    ok. tabstop when i have it at int this is the output:
    Code:
        |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    Play with X or play with O
    X
    Enter what box number that you
    would like to place you move in>
    4
        |   |  
    ----+---+----
        | &#191; |  
    ----+---+----
        |   |
    Am i not passing choice into the array tic_tac_toe correctly?

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to set tic_tac_toe[i] to 'X', not 'random uninitialized variable'.

    Edit: IOW, the choice in main() and the choice in get_user_character() aren't the same. You return it from get_user_character, but main doesn't do anything with it.
    Last edited by tabstop; 04-12-2008 at 05:11 PM.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    ok in main() i changed char choice to char *choice and user_move(&choice) to user_move(choice).... output now is:
    Code:
        |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    Play with X or play with O
    X
    Enter what box number that you
    would like to place you move in>
    4
        |   |  
    ----+---+----
        | U |  
    ----+---+----
        |   |

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That has nothing to do with the problem. get_user_choice is a function that returns a character. You must put that character somewhere by assigning the result of the function to a variable.

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    thanks tabstop I finally see the light Okay now is this is my new main function. I added function get_a_winner to check a winner and it works. What I want to do is end program when a winner is found using the check winner function. How would I do that in my main function if I have the loop set for 9 iterations?
    Code:
    int main()
    {
            int j;
            clear_board(tic_tac_toe);
            print_board( tic_tac_toe);
    
            char choice =get_user_character();
    for (j=0; j<9; j++)
    {
            int i =user_move(tic_tac_toe);
            tic_tac_toe[i-1]= choice;
            print_board(tic_tac_toe);
            get_a_winner(tic_tac_toe);
    }
            return EXIT_SUCCESS;
    }
    
    
    
    char get_a_winner(char*tic_tac_toe)
    {
            
    if((tic_tac_toe[0] == 'X') && (tic_tac_toe[1] =='X') && (tic_tac_toe[2]=='X')||
       (tic_tac_toe[3] == 'X') && (tic_tac_toe[4] =='X') && (tic_tac_toe[5]=='X')||
       (tic_tac_toe[6] == 'X') && (tic_tac_toe[7] =='X') && (tic_tac_toe[8]=='X')||
       (tic_tac_toe[0] == 'X') && (tic_tac_toe[3] =='X') && (tic_tac_toe[6]=='X')||
       (tic_tac_toe[1] == 'X') && (tic_tac_toe[4] =='X') && (tic_tac_toe[7]=='X')||
       (tic_tac_toe[2] == 'X') && (tic_tac_toe[5] =='X') && (tic_tac_toe[8]=='X')||
       (tic_tac_toe[0] == 'X') && (tic_tac_toe[4] =='X') && (tic_tac_toe[8]=='X')||
       (tic_tac_toe[2] == 'X') && (tic_tac_toe[4] =='X') && (tic_tac_toe[6]=='X'))
    
            printf("player X is the winner\n");
    
    }

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by marquis1431 View Post
    thanks tabstop I finally see the light Okay now is this is my new main function. I added function get_a_winner to check a winner and it works. What I want to do is end program when a winner is found using the check winner function. How would I do that in my main function if I have the loop set for 9 iterations?
    Don't set your loop for 9 iterations, but instead use the result of your winner check. IOW: You want to do something while there's no winner.

    Note that you're get a winner function claims to return char, but does not do so -- you'll need to decide what to return for X winning, what to return for O winning, what to return for a cat game, and what to return for a game still in progress.

Page 1 of 3 123 LastLast
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, 11:41 AM
  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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21