Thread: Error message in tic tac toe problem please help

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

    Error message in tic tac toe problem please help

    I keep getting an error message on line 23 using bloodshed dev-C++ that states:

    "invalid conversion from '[*][3]' to 'int'"

    I was wondering what the hell that meant.

    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 displayBoard(int [][COLS], int);   
    void player1 (int, int, int); 
    void player2 (int);
    
    int main ()
    {
        int space = 0 ;
        int space2 = 0 ;
        int B[ROWS][COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
        cout << "The playing board looks like:\n";
        displayBoard(B, ROWS);    
        player1 (space, B, ROWS);
        displayBoard(B, ROWS);
        system("pause");
        return 0;
    }
    
    
    void displayBoard ( int 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 space, int array[][COLS], int rows)
    {
         
         cout << "Please select a space on the board to place your 'x' by selecting space 1 through 9\n";
         cin >> space ;
         
         if (space == 1) array[0][0] = 'x' ;
         if (space == 2) array[0][1] = 'x' ;
         if (space == 3) array[0][2] = 'x' ;
         if (space == 4) array[1][0] = 'x' ;
         if (space == 5) array[1][1] = 'x' ;
         if (space == 6) array[1][2] = 'x' ;
         if (space == 7) array[2][0] = 'x' ;
         if (space == 8) array[2][1] = 'x' ;
         if (space == 9) array[2][2] = 'x' ;
         else cout << "invalid entry" ;
         
         
    }
    
    void player2 (int space2)
    
    {
         cout << "Please select a space on the board to place your 'x' by selecting space 1 through 9\n";
         cin >> space2 ;
         int B[ROWS][COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
         if (space2 == 1) B[0][0] = 'x' ;
         if (space2 == 2) B[0][1] = 'x' ;
         if (space2 == 3) B[0][2] = 'x' ;
         if (space2 == 4) B[1][0] = 'x' ;
         if (space2 == 5) B[1][1] = 'x' ;
         if (space2 == 6) B[1][2] = 'x' ;
         if (space2 == 7) B[2][0] = 'x' ;
         if (space2 == 8) B[2][1] = 'x' ;
         else if (space2 =9) B[2][2] = 'x' ;
    }
    
    char winner (char winner)
    {
         int B[ROWS][COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
         if (B[0][0] == B[0][1] && B[0][1] == B[0][2])
            winner = B[0][0];
         if (B[1][0] == B[1][1] && B[1][1] == B[1][2])
            winner = B[1][0];
         if (B[2][0] == B[2][1] && B[2][1] == B[2][2])
            winner = B[2][0];
         if (B[0][0] == B[1][0] && B[1][0] == B[2][0])
            winner = B[1][0];
         if (B[0][1] == B[1][1] && B[1][1] == B[2][1])
            winner = B[1][1];
         if (B[0][2] == B[1][2] && B[1][2] == B[2][2])
            winner = B[2][2];
         if (B[0][0] == B[1][1] && B[1][1] == B[2][2])
            winner = B[2][2];
         if (B[0][2] == B[1][1] && B[1][1] == B[2][0])
            winner = B[1][1];
         return winner ;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This is your protoype
    void player1 (int, int, int);

    This is your definition
    void player1 (int space, int array[][COLS], int rows)

    Make them match.
    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.

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

    Another problem....

    Thanks for that!! I got the program to compile by matching the prototypes to work but when prompted to enter the position for player 1, no matter which number I enter I get "invalid entry."

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

    This is the output.....

    The playing board looks like:
    1 2 3
    4 5 6
    7 8 9
    Please select a space on the board to place your 'x' by selecting space 1 through 9
    2
    invalid entry
    1 120 3
    4 5 6
    7 8 9
    Press any key to continue . . .

  5. #5
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    int B[ROWS][COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
    Well that might have something to do with it.

    When you declare an array with more than one dimension, you have to enter values in the proper way. You're declaring values like its an array like this:
    Code:
    int B[9] = {1,2,3,4,5,6,7,8,9};
    Where what you need to do is this:
    Code:
    int B[ROWS][COLS] = {{1,2,3},{4,5,6},{7,8,9}};
    That may or may not be the problem, just something i noticed.

    Good luck!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    26
    Thanks for the input, I fixed that but the same error message occurred. So ifnyone else has suggestions I'll be here all night.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The else that displays the invaid entry is tied only to the if (space == 9) part. So it will run as long as space is not 9. You should make the whole list of ifs an if/else if chain. Another option is to use a switch, and put the "invalid entry" output in the default case of the switch.

    Note that your player2 list has a similar problem. Once you get player1 working, use the same thing for the player2 function.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    void player1 (int space, int array[][COLS], int rows)
    {
         
         cout << "Please select a space on the board to place your 'x' by selecting space 1 through 9\n";
         cin >> space ;
         
         if (space == 1) array[0][0] = 'x' ;
         if (space == 2) array[0][1] = 'x' ;
         if (space == 3) array[0][2] = 'x' ;
         if (space == 4) array[1][0] = 'x' ;
         if (space == 5) array[1][1] = 'x' ;
         if (space == 6) array[1][2] = 'x' ;
         if (space == 7) array[2][0] = 'x' ;
         if (space == 8) array[2][1] = 'x' ;
         if (space == 9) array[2][2] = 'x' ;
         else cout << "invalid entry" ;
         
         
    }
    Why pass space to the function if you just ignore whatever it contains and read input into it? It might as well be a local variable. Ditto for player2().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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

    Its starting to work...

    I changed up my code a little bit and can now play a full game but I need the loop to end when there is a winner and then ask if another game is to be played and if so to start the loop over again. Any thoughts on how to accomplish this would be helpful.

    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 displayBoard(char [][COLS], int);   
    void player1 (int rowX, int colX, char array[][COLS], int rows ); 
    void player2 (int rowO, int colO, char array[][COLS], int rows );
    char winner (char array[][COLS]);
    
    int main ()
    {
        int rowX = 0 ;
        int colX = 0 ;
        int rowO = 0 ;
        int colO = 0 ;
        int space2 = 0 ;
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
        cout << "The playing board looks like:\n";
        displayBoard(B, ROWS);        
        
        for (int i = 0; i < 6 ; i++)
        {
            player1 (rowX, colX, B, ROWS); 
            winner (B);   
            displayBoard(B, ROWS);        
            player2 (rowO, colO, B, ROWS);
            winner (B);
            displayBoard(B, ROWS); 
        }
        
          
        system("pause");
        return 0;
    }
    
    //Function that initializes 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;
         }
    }    
    
    void player1 (int rowX, int colX, char array[][COLS], int rows)
    {
         
         cout << "Please select a space on the board to place your 'x' by by entering the row first and then the column, pressing 'enter' after each entry\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], int rows )
    
    {
         cout << "Please select a space on the board to place your 'o' by entering the row first and then the column, pressing 'enter' after each entry\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 = 'n' ;
         
         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];
         return answer ;
    }

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Put the bulk of your current main into another function called say playOneGame()

    Then main becomes
    Code:
    do {
      playOneGame();
      cout << "Another ? ";
      cin >> yes;
    } while ( yes == 'y' );
    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.

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

    Winner function not working properly....I think

    I can play the game just fine but I can't get the code to declare the winner after a winner is determined.

    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 displayBoard(char [][COLS], int);   
    void player1 (int rowX, int colX, char array[][COLS], int rows ); 
    void player2 (int rowO, int colO, char array[][COLS], int rows );
    char winner (char array[][COLS]);
    void playOneGame(char B[][COLS]);
    
    int main ()
    {
        char yes;
        char Y = 'Y' ;
        char y = 'y' ;
        char B[ROWS][COLS] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};
        cout << "The playing board looks like:\n";
        displayBoard(B, ROWS);  
        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 ;
        
         for (int i = 0; i < 5 ; i++)
             {
                  player1 (rowX, colX, B, ROWS); 
                  winner (B);   
                  displayBoard(B, ROWS);        
                  player2 (rowO, colO, B, ROWS);
                  winner (B);
                  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], int rows)
    {
         
         cout << "Please select a space on the board to place your 'x' by by entering the row first and then the column, pressing 'enter' after each entry\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], int rows )
    
    {
         cout << "Please select a space on the board to place your 'o' by entering the row first and then the column, pressing 'enter' after each entry\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 = 'n' ;
         
         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];
         return answer ;
    }

  12. #12
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    You don't check the return value from winner(). And BTW, I have a strong feeling that winner() won't give you what you want from it.
    Last edited by Oysterman; 04-08-2007 at 04:29 PM.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You have to use the return value of the winner function. Right now you call the function and ignore it's return value. If the return value is not 'n', then the game is over and you can declare the winner.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I imagine that player1() and player2() could be condensed into one function . . . .

    What is the purpose of this?
    Code:
        char Y = 'Y' ;
        char y = 'y' ;
    You should look at the output from g++ with warnings turned on:
    Code:
    $ g++ -W -Wall -ansi -pedantic -c ttttffff.cpp
    ~/ttttffff.cpp: In function `int main()':
    ~/ttttffff.cpp:18: warning: unused variable `char Y'
    ~/ttttffff.cpp:19: warning: unused variable `char y'
    ~/ttttffff.cpp: In function `void player1(int, int, char (*)[3], int)':
    ~/ttttffff.cpp:68: warning: unused parameter `int rows'
    ~/ttttffff.cpp: In function `void player2(int, int, char (*)[3], int)':
    ~/ttttffff.cpp:89: warning: unused parameter `int rows'
    $
    There's a bunch of stuff you're not using.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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

    Still trying to get the winner declared.....

    Ok I think I fixed all the little things, but I still cannot figure out how to display a winner when a winner is determined. My program spits out a winner everytime it is ran.

    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 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]);
    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 ;
        
         for (int i = 0; i < 6 ; i++)
             {
                  player1 (rowX, colX, B); 
                  winner (B) ;
                  displayBoard(B, ROWS);        
                  player2 (rowO, colO, B);
                  winner (B);
                  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];
         
         cout << answer ;
    }

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