Thread: my array resets to initial value

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    my array resets to initial value

    My c++ should be proficiency should be visible through my problem

    I'm trying to write small app in order to generate some hexagon tiles.

    I've created a class board which I use for my array, I use this form of drawing on the screen since I'm busy learning c++ .

    I have a method in my class that set coords and draw a hexagon by means of some arithmatic setting values in the array.

    Once this is done I redraw the array.

    The problem that I have is after every setting of the coords it doesn't remember the previous my previous hexagon. I have had this problem before when I wrote a small tic-tac-toe game.

    I know how to fix it (not sure if it's the right way). I set the coords of the instance of the class "myboard".

    I thought the proper way to get around this is by passing the instance of the class to the classes method. The drawing works but it doesn't remember the previous hexagon.

    I'm sure this solution is a much bigger concept which I need to grasp if I want to succeed at c++.

    This is why I asked in the forum in order to be able to understand it.

    Thanks
    Jannie

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class board
    {
        public:
        board();
        ~board();
    
        string arboard[50][50];
    
        void refreshboard();
        void addtile(board myboard,int x,int y);
    
    };
    
    //constructor init board with x
    board::board(){
    
        for(int i=0;i<20;i++)
        {
            for (int j=0;j<20;j++)
            {
                arboard[i][j] = "X";
                cout<< arboard[i][j]  ;
    
            }
            cout << endl;
        }
    
    };
    
    board::~board(){};
    
    void board::refreshboard()
    {
    //this function will draw the board  with tiles one it.
      for(int i=0;i<20;i++)
        {
            for (int j=0;j<20;j++)
            {
                cout<< arboard[i][j];
            }
            cout << endl;
        }
    };
    
    
    void board::addtile(board myboard,int x,int y)
    {
        // this version draw the tile but it only ever draw one on the board . I thought by passing the myboard instance to it it will have the same effect as the one in main but it doesn't I believe I am doing something wrong here but not sure what.
       /* arboard[y][x] = " ";
        arboard[y][x+1] = "/";
        arboard[y][x+2] = " ";
        arboard[y][x+3] = "\\";
        arboard[y][x+4] = " ";
        //Draw middle
        arboard[y+1][x] = "|";
        arboard[y+1][x+1] = " ";
        arboard[y+1][x+2] = 1;
        arboard[y+1][x+3] = " ";
        arboard[y+1][x+4] = "|";
        //draw bottom
        arboard[y+2][x] = " ";
        arboard[y+2][x+1] = "\\";
        arboard[y+2][x+2] = " ";
        arboard[y+2][x+3] = "/";
        arboard[y+2][x+4] = " ";*/
    
    };
    
    void SetTile(board myboard,int x,int y)
    {
            //function to set the tile to interface with the class method
           myboard.addtile(myboard,x,y);
           myboard.refreshboard();
    }
    
    int main()
    {
    
        int x = 0;
        int y=0;
        int i = 0;
        board myboard;
    
        while (i<5)
        {
        cout << "Insert X and Y \n";
        cin >> x ;
        cin >> y;
    
     //this works if I set it here.
        myboard.arboard[y][x] = " ";
        myboard.arboard[y][x+1] = "/";
        myboard.arboard[y][x+2] = " ";
        myboard.arboard[y][x+3] = "\\";
        myboard.arboard[y][x+4] = " ";
        //Draw middle
        myboard.arboard[y+1][x] = "|";
        myboard.arboard[y+1][x+1] = " ";
        myboard.arboard[y+1][x+2] = 1;
        myboard.arboard[y+1][x+3] = " ";
        myboard.arboard[y+1][x+4] = "|";
        //draw bottom
        myboard.arboard[y+2][x] = " ";
        myboard.arboard[y+2][x+1] = "\\";
        myboard.arboard[y+2][x+2] = " ";
        myboard.arboard[y+2][x+3] = "/";
        myboard.arboard[y+2][x+4] = " ";
    
        SetTile(myboard,x,y);
        }
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    3
    I think it's because in actual fact I'm setting one board and display the properties of the class an not that of the instance.

    I'm going to try modify the code.

    I might be wrong though.

    Ok, so I've discovered I'm not actually passing anything to SetTile. Well the address is all different so I'm passing perhaps a bunch of jibberish.

    --- SOme time later.

    So I've modified my code to directly call the class method in terms of say myboard.addtile and myboard.refresh. This works fine. But from an article on the site I read you should interact with the class via interfaces to prevent big code changes when the class changes. So I tried to do this from start but it would appear not to work.

    How can I use this interface function in order to acheive this ?
    Last edited by jannie; 10-18-2011 at 07:01 PM. Reason: add more info

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    // Note the reference parameter
    // passing by value gets you a new temporary board each time
    // you call it
    void SetTile(board &myboard,int x,int y)
    {
            //function to set the tile to interface with the class method
           myboard.addtile(myboard,x,y);
           myboard.refreshboard();
    }
    Like so.

    Or you could make SetTile a member function, then all you would need would be the xy parameters.
    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.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Don't pass a board into the addTile method. It already have a board.
    The syntax you want to use to call the method is:
    Code:
    myboard.SetTile(x, y);
    Then once you have done this, post the code that you are trying to use to do it properly but have a problem with. By posting the code where you've commented out some of the stuff that would do it properly, we can't see what you were doing wrong.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    3

    Thanks for reply

    I've managed to get around that but I have tested using the reference.

    I can draw the tile now based on coords and I'm busy trying to making the drawing function recursive.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    class board
    {
        public:
        board();
        ~board();
    
        string arboard[50][50];
    
        void initboard();
        void refreshboard();
        void addtile(int x,int y,int z);
    
    };
    
    //constructor init board with x
    board::board(){};
    board::~board(){};
    
    void board::initboard()
    {
    
          for(int i=0;i<20;i++)
        {
            for (int j=0;j<20;j++)
        {
                arboard[i][j] = "X";
                cout<< arboard[i][j]  ;
        }
            cout << endl;
        }
    };
    
    
    void board::refreshboard()
    {
    //this function will draw the board  with tiles one it.
      for(int i=0;i<20;i++)
        {
            for (int j=0;j<20;j++)
            {
                cout<< arboard[i][j];
            }
            cout << endl;
        }
    
    };
    
    
    void board::addtile(int x,int y,int z)
    {
    
        int newx = 0;
        int newy = 0;
        int tiles = 0;
        int levels = 1;
    
        //number of new hegaxons to be drawn
        int iterations = ((2*(2*levels)-1) + (2*(1+ levels)));
    
        // count hexagons
    
        for(int i=0;i<20;i++)
        {
            for (int j=0;j<20;j++)
            {
                if (arboard[i][j] == "|")
                {
                    tiles++;
                }
    
            }
        }
    
        // this version draw the tile but it only ever draw one on the board .
    
        arboard[y-1][x-2] = " ";
        arboard[y-1][x-1] = "/";
        arboard[y-1][x] = " ";
        arboard[y-1][x+1] = "\\";
        arboard[y-1][x+2] = " ";
        //Draw middle
        arboard[y][x-2] = "|";
        arboard[y][x-1] = " ";
        arboard[y][x] = 1;
        arboard[y][x+1] = " ";
        arboard[y][x+2] = "|";
        //draw bottom
        arboard[y+1][x-2] = " "; // this is the new coord for next tile.
        arboard[y+1][x-1] = "\\";
        arboard[y+1][x] = " ";
        arboard[y+1][x+1] = "/";
        arboard[y+1][x+2] = " ";
    
    
    };
    
    /*void SetTile(board &myboard,int x,int y,int z)
    {
            //function to set the tile to interface with the class method
           myboard.addtile(x,y,z);
           myboard.refreshboard();
    }*/
    
    int main()
    {
    
        int x = 0;
        int y=0;
        int i = 0;
        board myboard;
        myboard.initboard();
    
        while (i<5)
        {
        cout << "Insert X and Y \n";
        cin >> x ;
        cin >> y;
    
        myboard.addtile(x,y,1);
        myboard.refreshboard();
    
        i++;
        }
    
        return 0;
    }
    Thanks for the help.

    I spend most of the day drawing hexagons and finding a pattern or formula to the number of new tiles to be drawn which is

    ((2*(2*levels)-1) + (2*(1+ levels)));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variable auto resets itself?
    By thestien in forum Game Programming
    Replies: 3
    Last Post: 08-15-2008, 02:23 PM
  2. List iteration problem, it resets values
    By Mike Borozdin in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2006, 09:42 AM
  3. I've got the initial part but....
    By MB1 in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2005, 03:15 PM
  4. usleep resets clock()
    By vasanth in forum C Programming
    Replies: 4
    Last Post: 09-24-2004, 12:30 PM
  5. post count resets
    By Gedi in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-25-2004, 05:53 PM