Thread: Tic Tac Toe program...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Tic Tac Toe program...

    I have written the program and it runs allright, there is just one problem. I can't get the loop to break after a winner is declared and I'm not sure if my winner function is working properly.
    Any help would be greatly appreciated.

    Here is my code:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    //Global constants
    const int COLS = 3;             //Number of columns on the board
    const int ROWS = 3;             //Number of rows on the board
    
    void initBoard (char [][COLS], int  );
    void displayBoard(char [][COLS], int);   
    void player1 (int rowX, int colX, char array[][COLS] ); 
    void player2 (int rowO, int colO, char array[][COLS] );
    char winner (char array[][COLS], char answer);
    void playOneGame(char B[][COLS], char answer);
    
    int main ()
    {
        char yes;
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
        char A[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
        char answer = 'n' ;  
        cout << "To play the game enter the row in which you would like to place your marker and press enter.  Then do the same for the column.\n";
        
        initBoard(A, ROWS);
        do
        {
             playOneGame(B, answer);          
             cout << "Another game? Y/N" ;
             cin >> yes;
        }
        while (yes == 'Y' || yes == 'y') ;
        
          
        system("pause");
        return 0;
    }
    
    void playOneGame (char B[][COLS], char answer)
    {
        int rowX = 0 ;
        int colX = 0 ;
        int rowO = 0 ;
        int colO = 0 ;
         
         for (int i = 0; i < 6 ; i++)
             {
                  player1 (rowX, colX, B); 
                  winner (B, answer) ;
                  if (answer == 'x') 
                     break;
                  displayBoard(B, ROWS);        
                  player2 (rowO, colO, B);
                  winner (B, answer);
                  if (answer == 'o') 
                     break;
                  displayBoard(B, ROWS); 
             }
             
    }
    
    void displayBoard ( char array[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array[x][y] << " ";
             }
             cout << endl;
         }
    }    
    
    
    void initBoard ( char array2[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array2[x][y] << " ";
             }
             cout << endl;
         }
    }
    
    
    void player1 (int rowX, int colX, char array[][COLS])
    {
         
         cout << "Please select a space on the board to place your 'x'\n";
         cin >> rowX >> colX ;
         
         if (rowX == 1 && colX == 1) array[0][0] = 'x' ;
         if (rowX == 1 && colX == 2) array[0][1] = 'x' ;
         if (rowX == 1 && colX == 3) array[0][2] = 'x' ;
         if (rowX == 2 && colX == 1) array[1][0] = 'x' ;
         if (rowX == 2 && colX == 2) array[1][1] = 'x' ;
         if (rowX == 2 && colX == 3) array[1][2] = 'x' ;
         if (rowX == 3 && colX == 1) array[2][0] = 'x' ;
         if (rowX == 3 && colX == 2) array[2][1] = 'x' ;
         if (rowX == 3 && colX == 3) array[2][2] = 'x' ;
         
         
         
    }
    
    void player2 (int rowO, int colO, char array[][COLS] )
    
    {
         cout << "Please select a space on the board to place your 'o'\n";
         cin >> rowO >> colO ;
         
         
         if (rowO == 1 && colO == 1) array[0][0] = 'o' ;
         if (rowO == 1 && colO == 2) array[0][1] = 'o' ;
         if (rowO == 1 && colO == 3) array[0][2] = 'o' ;
         if (rowO == 2 && colO == 1) array[1][0] = 'o' ;
         if (rowO == 2 && colO == 2) array[1][1] = 'o' ;
         if (rowO == 2 && colO == 3) array[1][2] = 'o' ;
         if (rowO == 3 && colO == 1) array[2][0] = 'o' ;
         if (rowO == 3 && colO == 2) array[2][1] = 'o' ;
         if (rowO == 3 && colO == 3) array[2][2] = 'o' ;
    }
    
    char winner ( char B[][COLS], char answer)
    {
         
         
         if (B[0][0] == B[0][1] && B[0][1] == B[0][2])
            answer = B[0][0] ;
         if (B[1][0] == B[1][1] && B[1][1] == B[1][2])
            answer = B[1][0];
         if (B[2][0] == B[2][1] && B[2][1] == B[2][2])
            answer = B[2][0];
         if (B[0][0] == B[1][0] && B[1][0] == B[2][0])
            answer = B[1][0];
         if (B[0][1] == B[1][1] && B[1][1] == B[2][1])
            answer = B[1][1];
         if (B[0][2] == B[1][2] && B[1][2] == B[2][2])
            answer = B[2][2];
         if (B[0][0] == B[1][1] && B[1][1] == B[2][2])
            answer = B[2][2];
         if (B[0][2] == B[1][1] && B[1][1] == B[2][0])
            answer = B[1][1];
         if (answer == 'x' || answer == 'o')
            cout << "player " << answer << " has won\n" ;
         
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    At the moment your winner() function has no form of output to the rest of the program - it has a return type of char, yet doesn't return anything, and your answer parameter is pass-by-value. If you intend to pass answer back to the caller, then you must either return it at the end of winner(), or accept answer by reference.
    If you choose not to return anything from winner(), then change its return type to void

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That was a good try, but you still aren't ignoring '*' characters. Your fix verifies that the winner is 'x' or 'o', but what about a board like this:
    Code:
     x  x  x
     o  o  *
     *  *  *
    Your first if statement will catch the win of x's, but your third if statement will catch the three '*'s in a row and change answer to '*'. That means you'll miss the win for x's.

    It would be better to ignore '*'s in your winner function if statements than to wiat until the end of the function and check for 'x and 'o'.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Tictactoe

    OK I'm not sure how to avoid the '*'s in my function but the program works fine for the class I'm in and doesnt declare '*' as a winner even when the board looks like the one above. I have changed the code to incorporate winner into the playOneGame function but it wtill won't break out of the loop when a winner is determined. It says "Player O (or X) has won the game" and then asks for the next position.

    Here is the code:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <
    using namespace std;
    
    //Global constants
    const int COLS = 3;             //Number of columns on the board
    const int ROWS = 3;             //Number of rows on the board
    
    void initBoard (char [][COLS], int  );
    void displayBoard(char [][COLS], int);   
    void player1 (int rowX, int colX, char array[][COLS] ); 
    void player2 (int rowO, int colO, char array[][COLS] );
    char winner (char array[][COLS], char answer);
    void playOneGame(char B[][COLS], char answer);
    
    int main ()
    {
        char yes;
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
        char A[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
        char answer = 'n' ;  
        cout << "To play the game enter the row in which you would like to place your marker and press enter.  Then do the same for the column.\n";
        
        initBoard(A, ROWS);
        do
        {
             playOneGame(B, answer);          
             cout << "Another game? Y/N" ;
             cin >> yes;
        }
        while (yes == 'Y' || yes == 'y') ;
        
          
        system("pause");
        return 0;
    }
    
    void playOneGame (char B[][COLS], char answer)
    {
        int rowX = 0 ;
        int colX = 0 ;
        int rowO = 0 ;
        int colO = 0 ;
         
         for (int i = 0; i < 6 ; i++)
             {
                  player1 (rowX, colX, B);               
                  answer = winner(B, answer) ;
                  if (answer == 'x') 
                     break;
                  displayBoard(B, ROWS);
                  player2 (rowO, colO, B);              
                  answer = winner(B, answer);
                  if (answer == 'o') 
                     break;
                  displayBoard(B, ROWS);
             }
             
    }
    
    void displayBoard ( char array[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array[x][y] << " ";
             }
             cout << endl;
         }
    }    
    
    
    void initBoard ( char array2[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array2[x][y] << " ";
             }
             cout << endl;
         }
    }
    
    
    void player1 (int rowX, int colX, char array[][COLS])
    {
         
         cout << "Please select a space on the board to place your 'x'\n";
         cin >> rowX >> colX ;
         
         if (rowX == 1 && colX == 1) array[0][0] = 'x' ;
         if (rowX == 1 && colX == 2) array[0][1] = 'x' ;
         if (rowX == 1 && colX == 3) array[0][2] = 'x' ;
         if (rowX == 2 && colX == 1) array[1][0] = 'x' ;
         if (rowX == 2 && colX == 2) array[1][1] = 'x' ;
         if (rowX == 2 && colX == 3) array[1][2] = 'x' ;
         if (rowX == 3 && colX == 1) array[2][0] = 'x' ;
         if (rowX == 3 && colX == 2) array[2][1] = 'x' ;
         if (rowX == 3 && colX == 3) array[2][2] = 'x' ;
         
         
         
    }
    
    void player2 (int rowO, int colO, char array[][COLS] )
    
    {
         cout << "Please select a space on the board to place your 'o'\n";
         cin >> rowO >> colO ;
         
         
         if (rowO == 1 && colO == 1) array[0][0] = 'o' ;
         if (rowO == 1 && colO == 2) array[0][1] = 'o' ;
         if (rowO == 1 && colO == 3) array[0][2] = 'o' ;
         if (rowO == 2 && colO == 1) array[1][0] = 'o' ;
         if (rowO == 2 && colO == 2) array[1][1] = 'o' ;
         if (rowO == 2 && colO == 3) array[1][2] = 'o' ;
         if (rowO == 3 && colO == 1) array[2][0] = 'o' ;
         if (rowO == 3 && colO == 2) array[2][1] = 'o' ;
         if (rowO == 3 && colO == 3) array[2][2] = 'o' ;
    }
    
    char winner ( char B[][COLS], char answer)
    {
         
         
         if (B[0][0] == B[0][1] && B[0][1] == B[0][2])
            answer = B[0][0] ;
         if (B[1][0] == B[1][1] && B[1][1] == B[1][2])
            answer = B[1][0];
         if (B[2][0] == B[2][1] && B[2][1] == B[2][2])
            answer = B[2][0];
         if (B[0][0] == B[1][0] && B[1][0] == B[2][0])
            answer = B[1][0];
         if (B[0][1] == B[1][1] && B[1][1] == B[2][1])
            answer = B[1][1];
         if (B[0][2] == B[1][2] && B[1][2] == B[2][2])
            answer = B[2][2];
         if (B[0][0] == B[1][1] && B[1][1] == B[2][2])
            answer = B[2][2];
         if (B[0][2] == B[1][1] && B[1][1] == B[2][0])
            answer = B[1][1];
         if (answer == 'x' || answer == 'o')
            cout << "player " << answer << " has won\n" ;
         
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> doesnt declare '*' as a winner even when the board looks like the one above.
    But does it correctly declare 'x' as a winner?

    You also still aren't returning an answer from your winner function.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    The program does correctly display a winner when 3 x's or 3 o's are lined up. I'm calling the winner function is the playOneGame function and it returns the answer correctly I just can't break out of the loop.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That answer has been given a couple times already now, so make sure you ask a specific question about what's been suggested if you don't understand.

    I also don't believe that your function works correctly on the example I gave, but I might just be looking at it wrong.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Is ignoring the '*'s a possibilty why i cant break out of the loop. How should I incorporate ignoring the '*'s into my if statements?

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    Eh, too tired to rip thru the code, but I'd suggest adding a game_end function, something like I added to the code below. The game ends as is, but it's just a quick fix.

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    //Global constants
    const int COLS = 3;             //Number of columns on the board
    const int ROWS = 3;             //Number of rows on the board
    int blah=0;
    
    void initBoard (char [][COLS], int  );
    void displayBoard(char [][COLS], int);   
    void player1 (int rowX, int colX, char array[][COLS] ); 
    void player2 (int rowO, int colO, char array[][COLS] );
    char winner (char array[][COLS], char answer);
    void playOneGame(char B[][COLS], char answer);
    
    void kill_game(int game)
     {
     if (game>0)
      {
          cout<<"we're exiting now..."<<std::endl;
            cin.ignore();
            cin.get();
    
      exit(1);
      }
      }
    
    int main ()
    {
        char yes;
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
        char A[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
        char answer = 'n' ;  
        cout << "To play the game enter the row in which you would like to place your marker and press enter.  Then do the same for the column.\n";
        
        initBoard(A, ROWS);
        do
        {
             playOneGame(B, answer);          
             cout << "Another game? Y/N" ;
             cin >> yes;
        }
        while (yes == 'Y' || yes == 'y') ;
        
          
        cin.get();
        return 0;
    }
    
    void playOneGame (char B[][COLS], char answer)
    {
        int rowX = 0 ;
        int colX = 0 ;
        int rowO = 0 ;
        int colO = 0 ;
         
         for (int i = 0; i < 6 ; i++)
             {
                  player1 (rowX, colX, B); 
                  winner (B, answer) ;
                  if (answer == 'x') 
                     break;
                  displayBoard(B, ROWS);        
                  player2 (rowO, colO, B);
                  winner (B, answer);
                  if (answer == 'o') 
                     break;
                  displayBoard(B, ROWS); 
             }
             
    }
    
    void displayBoard ( char array[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array[x][y] << " ";
             }
             cout << endl;
         }
    }    
    
    
    void initBoard ( char array2[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array2[x][y] << " ";
             }
             cout << endl;
         }
    }
    
    
    void player1 (int rowX, int colX, char array[][COLS])
    {
         
         cout << "Please select a space on the board to place your 'x'\n";
         cin >> rowX >> colX ;
         
         if (rowX == 1 && colX == 1) array[0][0] = 'x' ;
         if (rowX == 1 && colX == 2) array[0][1] = 'x' ;
         if (rowX == 1 && colX == 3) array[0][2] = 'x' ;
         if (rowX == 2 && colX == 1) array[1][0] = 'x' ;
         if (rowX == 2 && colX == 2) array[1][1] = 'x' ;
         if (rowX == 2 && colX == 3) array[1][2] = 'x' ;
         if (rowX == 3 && colX == 1) array[2][0] = 'x' ;
         if (rowX == 3 && colX == 2) array[2][1] = 'x' ;
         if (rowX == 3 && colX == 3) array[2][2] = 'x' ;
         
         
         
    }
    
    void player2 (int rowO, int colO, char array[][COLS] )
    
    {
         cout << "Please select a space on the board to place your 'o'\n";
         cin >> rowO >> colO ;
         
         
         if (rowO == 1 && colO == 1) array[0][0] = 'o' ;
         if (rowO == 1 && colO == 2) array[0][1] = 'o' ;
         if (rowO == 1 && colO == 3) array[0][2] = 'o' ;
         if (rowO == 2 && colO == 1) array[1][0] = 'o' ;
         if (rowO == 2 && colO == 2) array[1][1] = 'o' ;
         if (rowO == 2 && colO == 3) array[1][2] = 'o' ;
         if (rowO == 3 && colO == 1) array[2][0] = 'o' ;
         if (rowO == 3 && colO == 2) array[2][1] = 'o' ;
         if (rowO == 3 && colO == 3) array[2][2] = 'o' ;
    }
    
    char winner ( char B[][COLS], char answer)
    {
         
         
         if (B[0][0] == B[0][1] && B[0][1] == B[0][2])
            answer = B[0][0] ;
         if (B[1][0] == B[1][1] && B[1][1] == B[1][2])
            answer = B[1][0];
         if (B[2][0] == B[2][1] && B[2][1] == B[2][2])
            answer = B[2][0];
         if (B[0][0] == B[1][0] && B[1][0] == B[2][0])
            answer = B[1][0];
         if (B[0][1] == B[1][1] && B[1][1] == B[2][1])
            answer = B[1][1];
         if (B[0][2] == B[1][2] && B[1][2] == B[2][2])
            answer = B[2][2];
         if (B[0][0] == B[1][1] && B[1][1] == B[2][2])
            answer = B[2][2];
         if (B[0][2] == B[1][1] && B[1][1] == B[2][0])
            answer = B[1][1];
         if (answer == 'x' || answer == 'o')
           {
            cout << "player " << answer << " has won\n" ;
          blah++;
          kill_game(blah);
          }
    }
    Last edited by Oldman47; 04-11-2007 at 07:44 PM.

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    This may sound kind of lame, but we aren't allowed to use pointers in our source code because we havent covered those yet. I'm open to othr ideas tho.

    Thanks for the help.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Finally got what 'daved' was saying....

    Ok I finally got what daved was saying about returning a value from the winner function. Now my problem resides where I decide to play another game the board won't clear. Where should I put my initBoard function?

    here is my code:

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    //Global constants
    const int COLS = 3;             //Number of columns on the board
    const int ROWS = 3;             //Number of rows on the board
    
    void initBoard (char [][COLS], int  );
    void displayBoard(char [][COLS], int);   
    void player1 (int rowX, int colX, char array[][COLS] ); 
    void player2 (int rowO, int colO, char array[][COLS] );
    char winner (char array[][COLS], char answer);
    void playOneGame(char B[][COLS], char answer);
    
    int main ()
    {
        char yes;
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
        char A[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
        char answer = 'n' ;  
        cout << "To play the game enter the row in which you would like to place your marker and press enter.  Then do the same for the column.\n";
        
        //initBoard(A, ROWS);
        do
        {
             initBoard(A, ROWS);
             playOneGame(B, answer);          
             cout << "Another game? Y/N" ;
             cin >> yes;
        }
        while (yes == 'Y' || yes == 'y') ;
        
          
        system("pause");
        return 0;
    }
    
    void playOneGame (char B[][COLS], char answer)
    {
        int rowX = 0 ;
        int colX = 0 ;
        int rowO = 0 ;
        int colO = 0 ;
    
         for (int i = 0; i < 6 ; i++)
             {
                  player1 (rowX, colX, B);               
                  answer = winner(B, answer) ;
                  if (answer == 'x') 
                     break;
                  displayBoard(B, ROWS);
                  player2 (rowO, colO, B);              
                  answer = winner(B, answer);
                  if (answer == 'o') 
                     break;
                  displayBoard(B, ROWS);
             }
             
    }
    
    void displayBoard ( char array[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array[x][y] << " ";
             }
             cout << endl;
         }
    }    
    
    
    void initBoard ( char array2[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array2[x][y] << " ";
             }
             cout << endl;
         }
    }
    
    
    void player1 (int rowX, int colX, char array[][COLS])
    {
         
         cout << "Please select a space on the board to place your 'x'\n";
         cin >> rowX >> colX ;
         
         if (rowX == 1 && colX == 1) array[0][0] = 'x' ;
         if (rowX == 1 && colX == 2) array[0][1] = 'x' ;
         if (rowX == 1 && colX == 3) array[0][2] = 'x' ;
         if (rowX == 2 && colX == 1) array[1][0] = 'x' ;
         if (rowX == 2 && colX == 2) array[1][1] = 'x' ;
         if (rowX == 2 && colX == 3) array[1][2] = 'x' ;
         if (rowX == 3 && colX == 1) array[2][0] = 'x' ;
         if (rowX == 3 && colX == 2) array[2][1] = 'x' ;
         if (rowX == 3 && colX == 3) array[2][2] = 'x' ;
         
         
         
    }
    
    void player2 (int rowO, int colO, char array[][COLS] )
    
    {
         cout << "Please select a space on the board to place your 'o'\n";
         cin >> rowO >> colO ;
         
         
         if (rowO == 1 && colO == 1) array[0][0] = 'o' ;
         if (rowO == 1 && colO == 2) array[0][1] = 'o' ;
         if (rowO == 1 && colO == 3) array[0][2] = 'o' ;
         if (rowO == 2 && colO == 1) array[1][0] = 'o' ;
         if (rowO == 2 && colO == 2) array[1][1] = 'o' ;
         if (rowO == 2 && colO == 3) array[1][2] = 'o' ;
         if (rowO == 3 && colO == 1) array[2][0] = 'o' ;
         if (rowO == 3 && colO == 2) array[2][1] = 'o' ;
         if (rowO == 3 && colO == 3) array[2][2] = 'o' ;
    }
    
    char winner ( char B[][COLS], char answer)
    {
         
         
         if (B[0][0] == B[0][1] && B[0][1] == B[0][2])
            answer = B[0][0] ;
         if (B[1][0] == B[1][1] && B[1][1] == B[1][2])
            answer = B[1][0];
         if (B[2][0] == B[2][1] && B[2][1] == B[2][2])
            answer = B[2][0];
         if (B[0][0] == B[1][0] && B[1][0] == B[2][0])
            answer = B[1][0];
         if (B[0][1] == B[1][1] && B[1][1] == B[2][1])
            answer = B[1][1];
         if (B[0][2] == B[1][2] && B[1][2] == B[2][2])
            answer = B[2][2];
         if (B[0][0] == B[1][1] && B[1][1] == B[2][2])
            answer = B[2][2];
         if (B[0][2] == B[1][1] && B[1][1] == B[2][0])
            answer = B[1][1];
         if (answer == 'x' || answer == 'o')
            cout << "player " << answer << " has won\n" ;
         return answer;
    }

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why do you have two boards, A and B? Don't you only need one? And your initBoard doesn't actually change the contents of the board, it does exactly the same thing as displayBoard. You want initBoard to change the values in the board to '*'.

    >> How should I incorporate ignoring the '*'s into my if statements?
    You want to save the answer only if the first value is equal to the second value and the second value is equal to the third value and one of the values is not '*'.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Almost there I think...

    Ok I have changed to initBoard function to what I think should initialize the board but its not working at all. I have also changed my if statements to incorporate the *'s. If anyone could tell me why my initBoard function isn't working, or if its in the wrong place that would be awesome.

    Here is my code:

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    //Global constants
    const int COLS = 3;             //Number of columns on the board
    const int ROWS = 3;             //Number of rows on the board
    
    void initBoard (char [][COLS], int  );
    void displayBoard(char [][COLS], int);   
    void player1 (int rowX, int colX, char array[][COLS] ); 
    void player2 (int rowO, int colO, char array[][COLS] );
    char winner (char array[][COLS], char answer);
    void playOneGame(char B[][COLS], char answer);
    
    int main ()
    {
        char yes;                                                                   //Variable to hold response for new game
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};           //Initiailizing tictactoe board
        char answer = 'n' ;                                                         //Initializing winner
        cout << "To play the game enter the row in which you would like to place your marker and press enter.  Then do the same for the column.\n";
        
                                                                 //Initializing playing board
        do
        {
             initBoard(B, ROWS);
             displayBoard(B, ROWS);
             playOneGame(B, answer);          
             cout << "Another game? Y/N" ;
             cin >> yes;
        }
        while (yes == 'Y' || yes == 'y') ;
        
          
        system("pause");
        return 0;
    }
    
    //************************************************************
    //This function allows players to play 1 game of tic tac toe *
    //************************************************************
    
    void playOneGame (char B[][COLS], char answer)
    {
        int rowX = 0 ;
        int colX = 0 ;
        int rowO = 0 ;
        int colO = 0 ;
        
         for (int i = 0; i < 6 ; i++)
             {
                  player1 (rowX, colX, B);               
                  answer = winner(B, answer) ;
                  if (answer == 'x') 
                     break;
                  displayBoard(B, ROWS);
                  player2 (rowO, colO, B);              
                  answer = winner(B, answer);
                  if (answer == 'o') 
                     break;
                  displayBoard(B, ROWS);
             }
             
    }
    
    //**********************************
    //This function displays the board *
    //**********************************
    
    void displayBoard ( char array[][COLS], int rows )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array[x][y] << " ";
             }
             cout << endl;
         }
    }    
    
    //***************************************
    // This function initializes the board  *
    //***************************************
    
    void initBoard ( char array[][COLS], int rows )
    {
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
    }
    
    //*****************************************
    //This function records moves for player1 *
    //*****************************************
    
    void player1 (int rowX, int colX, char array[][COLS])
    {
         
         cout << "Please select a space on the board to place your 'x'\n";
         cin >> rowX >> colX ;
         
         if (rowX == 1 && colX == 1) array[0][0] = 'x' ;
         if (rowX == 1 && colX == 2) array[0][1] = 'x' ;
         if (rowX == 1 && colX == 3) array[0][2] = 'x' ;
         if (rowX == 2 && colX == 1) array[1][0] = 'x' ;
         if (rowX == 2 && colX == 2) array[1][1] = 'x' ;
         if (rowX == 2 && colX == 3) array[1][2] = 'x' ;
         if (rowX == 3 && colX == 1) array[2][0] = 'x' ;
         if (rowX == 3 && colX == 2) array[2][1] = 'x' ;
         if (rowX == 3 && colX == 3) array[2][2] = 'x' ;
         
         
         
    }
    
    //*****************************************
    //This function records moves for player2 *
    //*****************************************
    
    void player2 (int rowO, int colO, char array[][COLS] )
    
    {
         cout << "Please select a space on the board to place your 'o'\n";
         cin >> rowO >> colO ;
         
         
         if (rowO == 1 && colO == 1) array[0][0] = 'o' ;
         if (rowO == 1 && colO == 2) array[0][1] = 'o' ;
         if (rowO == 1 && colO == 3) array[0][2] = 'o' ;
         if (rowO == 2 && colO == 1) array[1][0] = 'o' ;
         if (rowO == 2 && colO == 2) array[1][1] = 'o' ;
         if (rowO == 2 && colO == 3) array[1][2] = 'o' ;
         if (rowO == 3 && colO == 1) array[2][0] = 'o' ;
         if (rowO == 3 && colO == 2) array[2][1] = 'o' ;
         if (rowO == 3 && colO == 3) array[2][2] = 'o' ;
    }
    
    //*************************************************
    //This function determines the winner of the game *
    //*************************************************
    
    char winner ( char B[][COLS], char answer)
    {
         
         
         if (B[0][0] == B[0][1] && B[0][1] == B[0][2] && B[0][2] != '*')
            answer = B[0][0] ;
         if (B[1][0] == B[1][1] && B[1][1] == B[1][2] && B[1][2] != '*')
            answer = B[1][0];
         if (B[2][0] == B[2][1] && B[2][1] == B[2][2] && B[2][2] != '*')
            answer = B[2][0];
         if (B[0][0] == B[1][0] && B[1][0] == B[2][0] && B[2][0] != '*')
            answer = B[1][0];
         if (B[0][1] == B[1][1] && B[1][1] == B[2][1] && B[2][1] != '*')
            answer = B[1][1];
         if (B[0][2] == B[1][2] && B[1][2] == B[2][2] && B[2][2] != '*')
            answer = B[2][2];
         if (B[0][0] == B[1][1] && B[1][1] == B[2][2] && B[2][2] != '*')
            answer = B[2][2];
         if (B[0][2] == B[1][1] && B[1][1] == B[2][0] && B[2][0] != '*')
            answer = B[1][1];
         if (answer == 'x' || answer == 'o')
            cout << "player " << answer << " has won\n" ;
         return answer;
    }

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