Like Tree1Likes
  • 1 Post By Salem

Need help with Tic Tac Toe program

This is a discussion on Need help with Tic Tac Toe program within the C++ Programming forums, part of the General Programming Boards category; I'm writing a program for a Tic Tac Toe game. I have it together but it is riddled with errors ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    10

    Need help with Tic Tac Toe program

    I'm writing a program for a Tic Tac Toe game. I have it together but it is riddled with errors seemingly all stemming from one piece of code in my header file. The "OnOff" and "gameOnOff(void)" part. I'm not sure what is wrong with this part and errors are not helping much.

    Code:
    #ifndef TicTacToe_h
    #define TicTacToe_h
    
    class TicTacToe
    {
        
    public:
    
        TicTacToe();
        void displayBoard(void);
        bool playerInput(int);
        void placeXo(void);
        bool goodGrid (int, int);
        OnOff gameOnOff(void);
    
    private:
    
        enum OnOff {Winner, Draw, Continue};
        int board[ 3 ][ 3 ];
    };
    
    #endif
    Code:
    #include <iostream>
    #include <iomanip>
    #include "TicTacToe.h"
    
    using namespace std;
    
    TicTacToe::TicTacToe()
    {
        int x;
        int y;
    
        for(x = 0; x < 3; ++x)
        for(y = 0; y < 3; ++y)
            board[ x ][ y ] = ' ';
    }
    
    void TicTacToe::displayBoard(void)
    {
        int x;
        int y;
    
        cout << "  0  1  2\n\n";  
    
        for(x = 0; x < 3; ++x)
        {
            cout << x;
    
            for(y = 0; y < 3; ++y)
            {
                cout << setw( 3 ) << static_cast < char > (board [ x ][ y ]);
                
                if(y != 2)
                cout << " |";
            }
    
            if(x != 2)
                cout << "\n __|__|__"
                     <<    "\n __|__|__\n";
        }
    
        cout << "\n\n"; 
    }
    
    bool TicTacToe::playerInput(int playername)
    {
        int x;
        int y;
    
        do
        {
            cout << "Player " << static_cast < char > (playername)
                 << " Make your move: ";
    
            cin >> x >> y;
    
            cout << '\n';   
        }
    
        while(!goodGrid(x, y));
    
        board [ x ][ y ] = playername;
        displayBoard();
        OnOff xoroOnOff = gameOnOff();
    
        if(xoroOnOff == Win)
        {
            cout << "Player " << static_cast < char > (playername) << " is the winner!\n";
            return true;
        }
    
        else if(xoroOnOff == Draw)
        {
            cout << "The game is a draw.\n";
            return true;
        }
    
        else
            return false;
    }
    
    void TicTacToe::placeXo(void)
    {
        displayBoard();
    
        while(true)
        {
            if(playerInput('X'))
                break;
            else if( playerInput('O'))
                break;
        }
    }
    
    bool TicTacToe::goodGrid(int x, int y)  
    {
        return x >= 0 && x < 3 && y >= 0 && y < 3 && board [ x ][ y ] == ' ';
    }
    
    TicTacToe::OnOff TicTacToe::gameOnOff(void)
    {
        int a;
        int x;
        int y;
    
        if(board[ 0 ][ 0 ] != ' ' && board[ 0 ][ 0 ] == board[ 1 ][ 1 ]
            && board[ 0 ][ 0 ] == board[ 2 ][ 2 ])
            return Winner;
    
        else if(board[ 2 ][ 0 ] != ' ' && board[ 2 ][ 0 ] == board[ 1 ][ 1 ]
                && board[ 2 ][ 0 ] == board[ 0 ][ 2 ])
                return Winner;
    
            for(a = 0; a < 3; ++a)
                if(board[ a ][ 0 ] != ' ' && board[ a ][ 0 ] == board[ a ][ 1 ]
                   && board[ a ][ 0 ] == board[ a ][ 2 ])
                   return Winner;
    
            for(a = 0; a < 3; ++a)
                if(board[ 0 ][ a ] != ' ' && board[ 0 ][ a ] == board[ 1 ][ a ]
                   && board[ 0 ][ a ] == board[ 2 ][ a ])
                   return Winner;
    
            for(x = 0; x < 3; ++x)
    
            for(y = 0; y < 3; ++y)
                    
            if(board [ x ][ y ] = ' ')
                        
            return Continue;
    
            return Draw;
    }
    Code:
    #include <iostream>
    #include "TicTacToe.h"
    
    using namespace std;
    
    int main()
    {
        TicTacToe xogame;
        xogame.placeXo();
    
        return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    > I'm not sure what is wrong with this part and errors are not helping much.
    Well they're certainly of no use to us, since you didn't post them.
    Lesshardtofind likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    10
    Error 1 error C2146: syntax error : missing ';' before identifier 'gameOnOff' line 14

    Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int line 14

    Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int line 14

    Warning 4 warning C4183: 'gameOnOff': missing return type; assumed to be a member function returning 'int' line 14

    Error 5 error C2440: 'initializing' : cannot convert from 'int' to 'TicTacToe::OnOff' line 63

    Error 6 error C2065: 'Win' : undeclared identifier line 65

    Error 7 error C2556: 'TicTacToe::OnOff TicTacToe::gameOnOff(void)' : overloaded function differs only by return type from 'int TicTacToe::gameOnOff(void)' line 100

    Error 8 error C2371: 'TicTacToe::gameOnOff' : redefinition; different basic types line 100

    9 IntelliSense: identifier "OnOff" is undefined line 14

    10 IntelliSense: declaration is incompatible with "<error-type> TicTacToe::gameOnOff()" (declared at line 14 of "c:\users\brian\desktop\TicTacToe.h") line 99

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,045
    You need to put the enum definition before it's first use, in this case before line 14.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    A forward declaration issue perhaps?

    This perhaps
    Code:
    class TicTacToe
    {
    private:
         enum OnOff {Winner, Draw, Continue};
      
    public:
    
        TicTacToe();
        void displayBoard(void);
        bool playerInput(int);
        void placeXo(void);
        bool goodGrid (int, int);
        OnOff gameOnOff(void);
     
    private:
     
        int board[ 3 ][ 3 ];
    };
    BTW, this doesn't work
    if(board [ x ][ 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 11-03-2010, 12:45 PM
  2. Replies: 1
    Last Post: 03-03-2009, 03:47 PM
  3. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 12:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21