Thread: Tic Tac Toe Program

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    ok. i got the get_a_winner functtion to return winner that is a char. now is the do while loop.I want the program to keep looping until winner = 'X' or 'O'. When winner is 'X' or 'O' someone has won so i want the game to end. this is what i have. This doesn't seem to stop it tho.
    Code:
    do
    { 
            int i =user_move(tic_tac_toe);
            tic_tac_toe[i-1]= choice;
            print_board(tic_tac_toe);
            char winner =get_a_winner(tic_tac_toe);
    }while(winner != 'X' || winner != 'O');
            printf("end of game\n");

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Change || to &&. You want to keep going while BOTH statements are true.

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    english translation: while(winner != 'X' || winner != 'O');....if the winner is not X or the winner is not O then the program will keep going. If the winner is X or is O the program should execute the next line correct? I want to say that if the winner is X or O end of game.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's the English translation, yes. Now: suppose the winner is X. Is the winner not O? Yes, the winner is not O. Therefore we keep going with the loop, since our loop condition is true (one side is true, so the whole thing is true).

    We want the winner to not be X AND the winner to not be O.

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    I changed to && and tested the program and made X be the winner but it still doesn't stop when a winner is found???
    Code:
    Enter what box number that you
    would like to place you move in>
    3
    make another move
      X | X | X
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    player X is the winner
    Enter what box number that you
    would like to place you move in>
    snapshot of output already entered two positions

    by the way thanks for all your help....couldn't have done it without you.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    you do have a return 'X' somewhere in your winner check function?

    Are you declaring winner inside your loop? If so, then winner will go out of scope before the check is made in the while() statement. So make sure winner is declared at the top of the program, rather than inside the loop.

  7. #22
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    Doesn't work like that. program below
    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();
            char winner =get_a_winner(tic_tac_toe);
            
    do
    {
            int i =user_move(tic_tac_toe);
            tic_tac_toe[i-1]= choice;
            print_board(tic_tac_toe);
            char winner =get_a_winner(tic_tac_toe);
    }while(winner != 'X' && winner != 'O');
            printf("end of game\n");
    }
    
    
    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 &#37;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);
    }
    }
    Shouldn't the get_a_winner be inside the loop so that it can be checked everytime a input is made? If i put it outside the do{} it will never check for a winner it'll just keep going.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by marquis1431 View Post
    Doesn't work like that. program below
    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();
            char winner =get_a_winner(tic_tac_toe);
            
    do
    {
            int i =user_move(tic_tac_toe);
            tic_tac_toe[i-1]= choice;
            print_board(tic_tac_toe);
            char winner =get_a_winner(tic_tac_toe);
    }while(winner != 'X' && winner != 'O');
            printf("end of game\n");
    }
    
    
    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 &#37;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);
    }
    }
    Shouldn't the get_a_winner be inside the loop so that it can be checked everytime a input is made? If i put it outside the do{} it will never check for a winner it'll just keep going.
    Remove the bits in red. You only declare variables once. The declaration inside the loop is "shadowing" the variable outside the loop, but when you get to the while() part, the inside-the-loop variable ceases to exist, leaving the outside-the-loop variable which is "random uninitialized data".

    Also, when you get to the bottom of the get_a_winner function you need to return ' ' (no winner).

  9. #24
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    ok. i removed char winner =get_a_winner(tic_tac_toe); all together and left this char choice =get_user_character(); as is and removed the char from char winner =get_a_winner(tic_tac_toe);......it seems to work fine...I should have noticed that i was delaring winner twice.
    I have a function written for a computer play. The computer would take the next open space and play a O.
    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();
    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';
    
    }while(winner != 'X' && winner != 'O');
            printf("end of game\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);
    }
    Shouldn't the for loop cycle thru the array unitl ' ' and then teturn computer_move which in the function will put an 'O' in that spot?
    output
    Code:
        |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    Play with X or play with O
    X
    Enter what box number that you
    would like to place you move in>
    4
        |   |  
    ----+---+----
      X |   |  
    ----+---+----
        |   |  
    Enter what box number that you
    would like to place you move in>
    6
    make another move
      O |   |  
    ----+---+----
      X |   | X
    ----+---+----
        |   |  
    Enter what box number that you
    would like to place you move in>
    It doesn't put an 'O" on the board intil I entered the 6. then it said make another move as if the spot was not empty???? confusing
    Code:
    char user_move(char *tic_tac_toe)
    {
     
            int k;
            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>9)
            printf("invalid move\n");
    
    else if (*tic_tac_toe != ' ')
            printf("make another move\n");
            return (i);
    }
    Is it not cycling thur the array correctly?

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Put a print_board after setting the computer's move to 'O'.

    In your user_move thing, *tic_tac_toe is the first element of the board, always and forever. Maybe tic_tac_toe[i] is what you want instead.

  11. #26
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    great suggestions......it doesn't place the 'O" on the second iteration
    Code:
         |   |  
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    Play with X or play with O
    X
    Enter what box number that you
    would like to place you move in>
    3
        |   | X
    ----+---+----
        |   |  
    ----+---+----
        |   |  
      O |   | X
    ----+---+----
        |   |  
    ----+---+----
        |   |  
    Enter what box number that you
    would like to place you move in>
    5
      O |   | X
    ----+---+----
        | X |  
    ----+---+----
        |   |  
      O |   | X
    ----+---+----
        | X |  
    ----+---+----
        |   |  
    Enter what box number that you
    would like to place you move in>
    8
      O |   | X
    ----+---+----
        | X |  
    ----+---+----
        | X |  
      O |   | X
    ----+---+----
        | X |  
    ----+---+----
        | X |
    shouldn't the computer_move function go thru the array tic_tac_toe until it reaches a ' '?

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    if(tic_tac_toe[computer_move] == ' ')
    This is why your compiler is telling you "assignment inside conditional statement" (or similar).

  13. #28
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    tabstop....i can say that you are a great person for this forum.....It works now....I could not have done it without you...I'm so grateful!!! Thank you sooo much.

  14. #29
    Registered User
    Join Date
    Apr 2008
    Posts
    41
    if i wanted to let te computer be 'X' and the user be 'O' could I do this?
    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);
    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");  
            
            return EXIT_SUCCESS;
    }

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    if (choice == 'X')
    You'll learn eventually.

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