Thread: Trouble Understanding Classes and Objects. Please Help.

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    2

    Trouble Understanding Classes and Objects. Please Help.

    I was going over a simple program that illustrated the basic usage of classes and objects, but I don't quite understand instantiating objects. Specifically, I don't quite get the last main function with the object named "Game". It seems the object can be named anything and it will compile and run. Can someone please explain? And no, I did not write this program. I am simply trying to understand how objects work. Here is the code:

    Code:
    /*****************************************************************************
    * Author: Ethan Croteau
    * Creation Date: 7/31/02
    * Description: this is a simple console app that allows you to play Tic-Tac-Toe.
    * Compile: this is ANSI C++, it was compiled with Borland CPP 5.5.
    *****************************************************************************/
    #include <iostream.h>
    using namespace std;
    class TicTacToe
    
    
        {
        public:
        TicTacToe();
        int PlayerMove(int i);
        void NextPlayer();
        void WinCheck();
        void DrawBoard();
        private:
        int board[3][3];
        int turn; // player1 == 1, player2 == 2
        int gameOver;
        void PlayGame();
    };
    /*****************************************************************************
    * Clear the board, set turn for player1, set gameOver to false, draw the board,
    * then away we go!
    *****************************************************************************/
    TicTacToe::TicTacToe()
    
    
        {
        for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
        board[i][j] = 0;// 0 means empty
        turn = 1; // player1
        gameOver = 0;
        DrawBoard();
        PlayGame();
    }
    /*****************************************************************************
    * i is the board position that the player selects.
    * Calculate the x and y coordinates.
    * Ensure board position is valid. Check if game has been won. switch Players. Update board.
    * Return: 0 if move was made, otherwise return the value stored in the board position.
    *****************************************************************************/
    int TicTacToe::PlayerMove(int i)
    
    
        {
        int x = (i - 1)/3;
        int y = ((i + 2) % 3);
        int returnVal = board[x][y];
        if (returnVal == 0)
    
    
            {
            board[x][y] = turn;
            WinCheck();
            if (!gameOver)
            NextPlayer();
        }
        else
        cout << "Invalid move, try again.\n";
        DrawBoard();
        return returnVal;
    }
    /*****************************************************************************
    * if turn equals 1, set it equal to 2. Otherwise set it equal to 1.
    * The switches the active player.
    *****************************************************************************/
    void TicTacToe::NextPlayer()
    
    
        {
        if (turn == 1)
        turn = 2;
        else
        turn = 1;
    }
    /*****************************************************************************
    * if the game has been won, set gameOver equal to turn.
    * Turn always contains a value that is boolean true: 1 or 2.
    *****************************************************************************/
    void TicTacToe::WinCheck()
    
    
        {
        if ((board[0][0] == turn) && (board[1][0] == turn) && (board[2][0] == turn))
        gameOver = turn;
        else
        if ((board[0][1] == turn) && (board[1][1] == turn) && (board[2][1] == turn))
        gameOver = turn;
        else
        if ((board[0][2] == turn) && (board[1][2] == turn) && (board[2][2] == turn))
        gameOver = turn;
        else
        if ((board[0][0] == turn) && (board[0][1] == turn) && (board[0][2] == turn))
        gameOver = turn;
        else
        if ((board[1][0] == turn) && (board[1][1] == turn) && (board[1][2] == turn))
        gameOver = turn;
        else
        if ((board[2][0] == turn) && (board[2][1] == turn) && (board[2][2] == turn))
        gameOver = turn;
        else
        if ((board[0][0] == turn) && (board[1][1] == turn) && (board[2][2] == turn))
        gameOver = turn;
        else
        if ((board[0][2] == turn) && (board[1][1] == turn) && (board[2][0] == turn))
        gameOver = turn;
    }
    /*****************************************************************************
    * if the game has been won, set gameOver equal to turn.
    * Turn always contains a value that is boolean true: 1 or 2.
    *****************************************************************************/
    void TicTacToe::PlayGame()
    
    
        {
        int i;
        while (gameOver!=turn)
    
    
            {
            //DrawBoard();
            cout << "Player[" << turn << "] Please enter move: ";
            cin >> i;
            PlayerMove(i);
        }
        cout << "Player[" << turn << "] Wins!" << endl;
    }
    /*****************************************************************************
    * Display the game board using ASCII characters.
    *****************************************************************************/
    void TicTacToe::DrawBoard()
    
    
        {
        int temp[9];
        int k = 0;
        for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
    
    
            {
            if (board[i][j] == 0)
            temp[k] = k+49;
            else
    
    
                {
                if (board[i][j] == 1)
                temp[k] = 88;
                else
                temp[k] = 79;
            }
            k++;
        }
        cout << "+---+---+---+\n";
        cout <<"| " << (char)temp[0] << " | " << (char)temp[1] << " | " << (char)temp[2] << " | \n";
        cout << "+---+---+---+\n";
        cout <<"| " << (char)temp[3] << " | " << (char)temp[4] << " | " << (char)temp[5] << " | \n";
        cout << "+---+---+---+\n";
        cout <<"| " << (char)temp[6] << " | " << (char)temp[7] << " | " << (char)temp[8] << " | \n";
        cout << "+---+---+---+\n";
    }
    /*****************************************************************************
    * Instantiate a TicTacToe object, which effectively starts your game play.
    *****************************************************************************/
    int main()
    
    
        {
        TicTacToe Game;
        return 0;
    }
    Last edited by Jeffcubed; 12-05-2002 at 06:15 PM.

  2. #2
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    OK. simply put a class is there to provide a structured way to write your coding. you dont want to put your whole program inside int main because thats only one funtion. Classes provide clearity. When you get to integer main you need to instantiate or create an object of that class.
    ex:

    Code:
    Class crap // my class
    {
     //blah blah blah
    };
    
    int main()
    {
    crap c1;  // take that class or structure and make an object of it
    }
    so basically when you get to main you can name your object ot the tictactoe class anything you want. Whatever you name it that name becomes a object or a child of the tictactoe class and does everything you programed it to do in the class.
    HOPE IT HELPS

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by gamer4life687
    Whatever you name it that name becomes a object or a child of the tictactoe class and does everything you programed it to do in the class.
    Not a child. a child and an object are two very different things.

    You can name "game" anything because it's just a name for an object, just like with a variable. after "int" you can give an arbitrary name, same thing goes for objects.

    The concept is you can make many tictactoe boards and each one has it's own set of variables associated with it.

    For more detailed help:

    Here
    Last edited by Polymorphic OOP; 12-05-2002 at 08:33 PM.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Lets look at main....
    Code:
    int main()
    {
        TicTacToe Game;
        return 0;
    }
    Here you are instantiating (constructing) an object called Game(and you are right this could have been any valid identifier.Its only a variable name after all) of class TicTacToe. Because you pass no arguments the compiler will first try to build your object with the default constructor for class TicTacToe. So here we are actually calling a function.... TicTacToe::TicTacToe()
    Lets see what that does...
    Code:
    TicTacToe::TicTacToe()
    {
        for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
        board[i][j] = 0;// 0 means empty
        turn = 1; // player1
        gameOver = 0;
        DrawBoard();
        PlayGame();
    }
    First it zeros the playing area,then sets a couple of variables,then 2 more functions are called. DrawBoard() and PlayGame().From here you have the board drawn and the game now started.The rest should be easy enough to follow.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    2

    Smile Thanks!!

    You guys turn me on. Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes & Objects [c++]
    By salmansalman in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2008, 08:02 AM
  2. Overloading Array Objects for use with Classes
    By ibleedart in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2007, 06:48 PM
  3. static classes vs objects
    By earnshaw in forum C# Programming
    Replies: 5
    Last Post: 02-08-2006, 03:19 PM
  4. Accessing/working with member objects of parent classes
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2005, 11:01 PM
  5. Mixing objects from different classes?
    By teedee46 in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2002, 10:28 AM