Thread: Error message in tic tac toe problem please help

  1. #16
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    You still don't do anything with the return value from winner(). And I still believe winner() is broken.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> My program spits out a winner everytime it is ran.
    That's because you have the code in there to spit out the answer every time the function is called. If you want the winner to be displayed only in certain situations, then add code to display it only in those situations. If the return value is not 'n', then the game is over and you can declare the winner.

    Another note, though, is that you need to handle empty squares as well. You should be ignoring '*' in your if statements.

  3. #18
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Almost there...

    Ok so now I can get the computer to spit out the winner, but its late and doesnt recognize when the 'x' player wins for some reason.

    Here it is:

    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 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]);
    
    int main ()
    {
        char yes;
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
        char answer = 'n' ;
        cout << "The playing board looks like:\n";
        displayBoard(B, ROWS);  
        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";
        do
        {
             playOneGame(B);          
             cout << "Another game? Y/N" ;
             cin >> yes;
        }
        while (yes == 'Y' || yes == 'y') ;
        
          
        system("pause");
        return 0;
    }
    
    void playOneGame (char B[][COLS])
    {
        int rowX = 0 ;
        int colX = 0 ;
        int rowO = 0 ;
        int colO = 0 ;
        char answer;
        
         for (int i = 0; i < 6 ; i++)
             {
                  player1 (rowX, colX, B); 
                  winner (B, answer) ;
                  if (answer == 'x' || answer == 'o') break;
                  displayBoard(B, ROWS);        
                  player2 (rowO, colO, B);
                  winner (B, answer);
                  if (answer == 'x' || 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 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" ;
         
    }

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