Hi all,

For my java class I need to make the game of life. So far I have 2 main classes that drive the program (ignoring GUI class for now.)

My main classes:

Cell(x, y) - where x = the x coordinate and y = the y coordinate of the 2D Array. Cell also has an int life = 0 in the constructor stating that it starts dead.

Cell has all get/set functions for x, y, and life variables

ArrayBoard() - which holds a 2D Array of Cells.

To start I loop through the array adding new cells:

//run loop to fill board with Cells with life = 0
        for(int i = 0; i < 10; i++){
            for(int j = 0; j < 10; j++){
                board[i][j] = new Cell(i,j);

My problem lies within checking whether the Cell's (at that given point of the loop) life is = 1.

So, I've created a for loop to loop through, but I'm lost as to how to reference that specific cell and its life variable. I know, in the past I've looped through a 1D array and referencing the function by calling something like: board.get(i).getLife(); however, this confuses me as to how to reference the object and its functions in a 2D Array.

Any help would be much appreciated!
Thanks in advance

Hmm... Does the Cell object ever move its location? From what you said, you could simply check the Cell at board[x][y]. What it means is that the value inside the array is the "Cell" object itself. Therefore calling board[x][y].getLive() would return the value of Cell itself.

Yes, you're right. After posting this I took a 2 minute break, came back and realized I could call it like that ><

Thanks for the help though!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.