Results 1 to 5 of 5
  1. #1
    naxalyte is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default Newbie can't spot his mistake :(

    Hi all,

    Basically I am designing a virtual 2Dimensional grid, made up of cells, and an object that will be able to simulate moving from cell to cell.

    So far I've got four classes:
    1 main
    2 GridBot
    3 Grid
    4 Cell

    My question is how do I allow an instance of GridBot to set a variable in an instance of Cell. I have tried objectname.method() but it doesnt work.

    I've taken the time to slim down my coding so that you can see more easily what my problem is.

    Java Code:
    public class main(){
    	public gridBot aGridBot = new gridBot();
    	public Grid theGrid = new Grid();
    	
    	public static void main(String args[]){
    		main theMainProgram = new main();
    	}
    	
    	public main(){
    		//link gridBot to cell 1,1 and set its occupied state to true
    		aGridBot = new gridBot();
    		aGridBot.CellCoordinateX = 1;
    		aGridBot.CellCoordinateY = 1;
    		theGrid.setCell(1, 1, true);
    	}
    	
    }
    
    
    public class Grid{
    	public Cell aCell[][] = new Cell[4][4];
    	
    	public Grid(){
    		this.InitializeCells();
    	}
    	
    	public void InitializeCells(){
    		for (int x=0; x<4; x++){
    			for (int y = 0; y<4; y++){
    				aCell[x][y] = new Cell();
    			}
    		}
    	}
    	
    	public void setCell(int x, int y, boolean YesNoOccupied){
    		aCell[x][y].Occupied = YesNoOccupied;
    	}
    }
    	
    public class Cell{
    	boolean Occupied;
    	
    	public Cell(){
    		Occupied = false;
    	}
    }
    
    
    public class gridBot{
    	public int CellCoordinateX;
    	public int CellCoordinateY;
    	
    //this is the method I am trying to develop, currently not called by anything
    	public void VirtualMove(){
    		CellCoordinateX = CellCoordinateX + 1;
    		CellCoordinateY = CellCoordinateY + 1;
    		//I want to invoke method setCell in 'theGrid'
    	}
    
    }
    Last edited by naxalyte; 11-21-2007 at 03:00 AM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    how do I allow an instance of GridBot to set a variable in an instance of Cell
    Pass it a reference to the Grid instance.
    Java Code:
    public class main(){
        public Grid theGrid = new Grid();
        // Pass a copy of the reference theGrid to aGridBot.
        // This will instantiate the Grid instance/member
        // variable in that class and allow it to call
        // methods in the Grid class.
        public gridBot aGridBot = new gridBot(theGrid);
    
        public static void main(String args[]){
            main theMainProgram = new main();
        }
    
        public main(){
            //link gridBot to cell 1,1 and set its occupied state to true
            // the gridBot member variable was instantiated above so
            // we don't need to do it again here.
    //        aGridBot = new gridBot();
            aGridBot.CellCoordinateX = 1;
            aGridBot.CellCoordinateY = 1;
            theGrid.setCell(1, 1, true);
        }
    }
    
    public class gridBot{
        public int CellCoordinateX;
        public int CellCoordinateY;
        Grid theGrid;
    
        public gridBot(Grid theGrid) {
            this.theGrid = theGrid;
        }
    
        //this is the method I am trying to develop, currently not called by anything
        public void VirtualMove(){
            CellCoordinateX = CellCoordinateX + 1;
            CellCoordinateY = CellCoordinateY + 1;
            //I want to invoke method setCell in 'theGrid'
            theGrid.setCell(args...);
        }
    
    }

  3. #3
    naxalyte is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default

    Thanx for your reply hardwired. So if I have an array of gridBots, each one having a reference to theGrid, would they all be able to see the same grid or each their own individual copy of the grid? Is this the standard way to create 'relations' between classes?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    would they all be able to see the same grid or each their own individual copy of the grid?
    If each bot had their own grid it would be like each one was playing in a different game. If you make one instance of Grid and pass the reference to each bot then they can all play in the same grid together. So, one instance for all bots.
    Is this the standard way to create 'relations' between classes?
    Generally, yes. Of course it depends on what you're doing.

  5. #5
    naxalyte is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

Similar Threads

  1. Java Sun SPOT development kit for sale
    By theveg in forum Reviews / Advertising
    Replies: 2
    Last Post: 09-10-2009, 08:17 PM
  2. newbie needs help...
    By vicky08 in forum New To Java
    Replies: 2
    Last Post: 03-31-2008, 05:26 PM
  3. Newbie
    By CSnoob87 in forum Introductions
    Replies: 2
    Last Post: 02-18-2008, 09:49 AM
  4. PLEASE!!!help me to find mistake
    By sasha20 in forum New To Java
    Replies: 1
    Last Post: 01-11-2008, 11:50 AM
  5. Newbie reporting
    By nelsaez in forum Introductions
    Replies: 0
    Last Post: 11-05-2007, 07:39 PM

Posting Permissions

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