Thread: Tic Tac Toe Program

  1. #31
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fix your indentation
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #32
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    Still not working output is
    Code:
        |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    Play with X or play with O
    O
    Enter what box number that you
    would like to place you move in>
    5
        |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    no winner yet
        |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    Enter what box number that you
    would like to place you move in>
    6
        |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    no winner yet
        |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |

  3. #33
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You'll have to show some code, I'm afraid. Be sure you using == inside if statements and = outside if statements (for actual assignments).

  4. #34
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    Sorry
    Code:
    int main()
    {
            int j =0;
            char winner;
            int O;
            clear_board(tic_tac_toe);
            print_board( tic_tac_toe);
            char choice=get_user_character();
    
            int computer_move=get_computer_move(tic_tac_toe);
    
    if (choice == 'O')
            tic_tac_toe[computer_move] == 'X';
    else
            tic_tac_toe[computer_move] == 'X';
    
    
    do
    {
            int i =user_move(tic_tac_toe);
            tic_tac_toe[i-1]= choice;
            print_board(tic_tac_toe);
            winner =get_a_winner(tic_tac_toe);
            int computer_move=get_computer_move(tic_tac_toe);
            
    //      tic_tac_toe[computer_move] = 'O';
            print_board(tic_tac_toe);
    
    }while(winner != 'X' && winner != 'O');
            printf("end of game\n");
    
            return EXIT_SUCCESS;
    }

  5. #35
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by marquis1431 View Post
    Sorry
    Code:
    int main()
    {
            int j =0;
            char winner;
            int O;
            clear_board(tic_tac_toe);
            print_board( tic_tac_toe);
            char choice=get_user_character();
    
            int computer_move=get_computer_move(tic_tac_toe);
    
    if (choice == 'O')
            tic_tac_toe[computer_move] == 'X';
    else
            tic_tac_toe[computer_move] == 'XO';
    
    
    do
    {
            int i =user_move(tic_tac_toe);
            tic_tac_toe[i-1]= choice;
            print_board(tic_tac_toe);
            winner =get_a_winner(tic_tac_toe);
            int computer_move=get_computer_move(tic_tac_toe);
            /* Need the OXO thing from above to put the computer move on the board */
    //      tic_tac_toe[computer_move] = 'O';
            print_board(tic_tac_toe);
    
    }while(winner != 'X' && winner != 'O');
            printf("end of game\n");
    
            return EXIT_SUCCESS;
    }
    Things to remove Things to add
    Just based on this I don't see why user moves aren't put in the board.

  6. #36
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    Something got jacked up.
    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*); 
    void clear_board(char*); 
    char get_user_character(); 
    char user_move(char*); 
    char get_a_winner(char*); 
    int get_computer_move(char*); 
    
    int main() 
    { 
    int j =0; 
    char winner; 
    int O; 
    clear_board(tic_tac_toe); 
    print_board( tic_tac_toe); 
    char choice=get_user_character(); 
    
           int computer_move=get_computer_move(tic_tac_toe); 
    
    if (choice == 'X') 
           tic_tac_toe[computer_move] = 'X'; 
    else 
           tic_tac_toe[computer_move] = 'O'; 
    
    
    do 
    { 
    int i =user_move(tic_tac_toe); 
    tic_tac_toe[i-1]= choice; 
    print_board(tic_tac_toe); 
    winner =get_a_winner(tic_tac_toe); 
    int computer_move=get_computer_move(tic_tac_toe); 
    
            
    if (choice == 'X') 
           tic_tac_toe[computer_move] = 'X';    
    else 
           tic_tac_toe[computer_move] = 'O'; 
            
    // tic_tac_toe[computer_move] = 'O'; 
    print_board(tic_tac_toe); 
    
    }while(winner != 'X' && winner != 'O'); 
    printf("end of game\n"); 
    
    return EXIT_SUCCESS; 
    } 
    
    void clear_board(char*tic_tac_toe) 
    { 
    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 *tic_tac_toe) 
    { 
    int i; 
    printf("Enter what box number that you\n"); 
    printf("would like to place you move in>\n"); 
    scanf("%d", &i); 
    
    if (i < 0 || i>9) 
    { 
    printf("invalid move\n"); 
    } 
    else if (tic_tac_toe[i] != ' ') 
    { 
    printf("make another move\n"); 
    return (i); 
    } 
    } 
    char get_a_winner(char*tic_tac_toe) 
    { 
    char X; 
    char O; 
    char winner; 
    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')) 
    {    
    winner = 'X'; 
    printf("player %c is the winner\n",winner); 
    return (winner); 
    } 
    else if((tic_tac_toe[0] == 'O') && (tic_tac_toe[1] =='O') && (tic_tac_toe[2]=='O')|| 
           (tic_tac_toe[3] == 'O') && (tic_tac_toe[4] =='O') && (tic_tac_toe[5]=='O')|| 
           (tic_tac_toe[6] == 'O') && (tic_tac_toe[7] =='O') && (tic_tac_toe[8]=='O')|| 
           (tic_tac_toe[0] == 'O') && (tic_tac_toe[3] =='O') && (tic_tac_toe[6]=='O')|| 
           (tic_tac_toe[1] == 'O') && (tic_tac_toe[4] =='O') && (tic_tac_toe[7]=='O')|| 
           (tic_tac_toe[2] == 'O') && (tic_tac_toe[5] =='O') && (tic_tac_toe[8]=='O')|| 
           (tic_tac_toe[0] == 'O') && (tic_tac_toe[4] =='O') && (tic_tac_toe[8]=='O')|| 
           (tic_tac_toe[2] == 'O') && (tic_tac_toe[4] =='O') && (tic_tac_toe[6]=='O')) 
    { 
    winner = 'O'; 
           printf("player %c is the winner\n", winner); 
    return (winner); 
    } 
    else 
    { 
    printf("no winner yet\n"); 
    } 
    } 
    int get_computer_move(char *tic_tac_toe) 
    { 
    int computer_move; 
    for (computer_move=0; computer_move<9; computer_move++) 
    if(tic_tac_toe[computer_move] == ' ') 
    return (computer_move); 
    }
    output
    Code:
        |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    Play with X or play with O
    X
    Enter what box number that you
    would like to place you move in>
    5
      X |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    no winner yet
      X | X |  
    ----+---+----
        |   |  
    ----+---+----
        |   |

  7. #37
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    okay I got it back working but it doesn't display the computer move twice output
    Code:
        |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    Play with X or play with O
    O
    Enter what box number that you
    would like to place you move in>
    6
      X |   |  
    ----+---+----
        |   | O
    ----+---+----
        |   |  
      O |   |  
    ----+---+----
        |   | O
    ----+---+----
        |   |  
    Enter what box number that you
    would like to place you move in>
    7
      O |   |  
    ----+---+----
        |   | O
    ----+---+----
      O |   |  
      O |   |  
    ----+---+----
        |   | O
    ----+---+----
      O |   |  
    Enter what box number that you
    would like to place you move in>
    Code:
    int main()
    {
    int j =0;
    char winner;
    int O;
    clear_board(tic_tac_toe);
    print_board( tic_tac_toe);
    char choice =get_user_character();
    int computer_move=get_computer_move(tic_tac_toe);
    if (choice == 'X')
            tic_tac_toe[computer_move] = 'O';
    else
            tic_tac_toe[computer_move] = 'X';
    
    do
    {
    int i =user_move(tic_tac_toe);
    tic_tac_toe[i-1]= choice;
    print_board(tic_tac_toe);
    winner =get_a_winner(tic_tac_toe);
    int computer_move=get_computer_move(tic_tac_toe);
    if (choice == 'X')
            tic_tac_toe[computer_move] = 'O';
     else  
            tic_tac_toe[computer_move] = 'X';
    
    tic_tac_toe[computer_move] = 'O'; 
    print_board(tic_tac_toe);
    }while(winner != 'X' && winner != 'O');
    printf("end of game\n");
    
    // int computer_move=get_computer_move(tic_tac_toe);
    //       char winner =get_a_winner(tic_tac_toe);
    return EXIT_SUCCESS;
    }

  8. #38
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    on the second move for the computer it doens't display it on the board? any suggestions

  9. #39
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you forget to change if(this=that) to if(this==that) inside get_computer_move?

  10. #40
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you have no indentation. Any suggestions?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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