CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2010
    Posts
    0

    Problem trying to make two objects access and edit same data

    Hi,

    I am currently trying to write a board game, and I am having trouble making each GameTile on the board access the same Point on the board.
    Each GameTile contains a certain number of Points, and sometimes Points overlap(the Point on a corner of one tile may be the same Point on the corner of an adjacent tile)
    Each Point on the board has a boolean value to determine whether that Point is occupied or not.

    In the code below, I have two GameTiles, tile1 and tile2 respectively.

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    class Point
    {
    private:
    
        bool occupied;
    
    public:
    
        Point(){}
    
        void setOccupied(bool yesOrNo)
        {
            occupied = yesOrNo;
        }
    
        bool checkOccupied()
        {
            return (occupied);
        }
    
    };
    
    
    class GameTile
    {
    private:
    
        Point allPoints[2];
        Point *pSinglePoint;
    
    public:
    
        GameTile(){}
    
        void addElement(int arrIndex,Point next)
        {
            pSinglePoint = &next;    // Pointer points to the memory location of "next" element(in this case a "Point")
            allPoints[arrIndex] = *pSinglePoint;    // element of allPoints = what is located at the memory address of  pSinglePoint
    
        }
        void setSurroundingPointOccupied(int arrIndex, bool yesOrNo)
        {
    
            allPoints[arrIndex].setOccupied(yesOrNo);   // This should set the Point boolean value
                                                        // to true or false(not a copy, but the actual object is being edited here)
    
        }
    
        bool checkSurroundingPointOccupied(int arrIndex)
        {
            return(allPoints[arrIndex].checkOccupied());    // This should return 0 or 1.
        }
    
    
    };
    
    int main()
    {
    
        GameTile tile1;
        GameTile tile2;
    
        Point point1;
        Point point2;
    
        tile1.addElement(0,point1);
        tile1.addElement(1,point2); // tile contains 2 points
    
        tile2.addElement(0,point1);
        tile2.addElement(1,point2); // this tile contains 2 points(which are the same points as the above tile)
    
        bool yes = true;
        bool no = false;
    
        tile1.setSurroundingPointOccupied(0,yes);
        tile1.setSurroundingPointOccupied(1,no);
    
        cout << tile1.checkSurroundingPointOccupied(0) << endl;
        cout << tile1.checkSurroundingPointOccupied(1) << endl;
    
        cout << tile2.checkSurroundingPointOccupied(0) << endl;
        cout << tile2.checkSurroundingPointOccupied(1) << endl;
    
        return 0;
    }
    I have attempted to make each GameTile contain 2 Points(which are the same Points, for testing purposes) and when I edit the boolean value for point1 through tile1, I expect this value to have be changed within tile2 as well. (For example, when I set point1's boolean to true, I expect that when I return the value of point1 from within tile2, it will return 1, without directly setting the variable within tile2.)

    The output I expected:
    1
    0
    1
    0

    But the actual output on running the program:
    1
    0
    0
    89 (or another large number, changes each time program is run)

    I believe Pointers are the way to go with this, however I am new to them, and despite reading various tutorials on Pointers, I'm still obviously doing something wrong.
    Any help on how to make this program do what I want it to will be greatly appreciated.

    By the way, I am running this using Code::Blocks 8.02 and the GNU GCC Compiler if this is of any help.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Problem trying to make two objects access and edit same data

    Could you tell us what the game is supposed to be? I'm having problem understanding the game. If I understand what you are trying to do, I'll have better chances to help you on how to do it.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Problem trying to make two objects access and edit same data

    Dont store a point array in each game tile. make an array either in main or a class of its own which contains all the points in all tiles. pass the points you are interested in by reference or pointer to the game tiles. Hold them in the tile class by reference or by pointer.

    The problems you experience are because all your parameter passing is by value so each tile has its own copy of the point in question and what happens in one tile wont be reflected in a second tile even tho essentially the point is the same point.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured