conways game of life in 3d. how to go about codeing the rules in c#

ive been thinking bout creating a game that uses the 4 simple rules of conways game of life

  1. any cell that has 2 or less live cells adjacent is dead. underpopulation
  2. any cell that has 2 or 3 live cells adjacent is kept alive for the next generation
    3 any cell that has 3 or more live cells adjacent is dead.
  3. any dead cell with 3 live cells adjacent is brought to life. reproduction
    death and birth happen at the same tick or second
    and a tick equals a generation.
    the problem i ran into was simple. does unity have a function thats built in to know the objects that surround an object?
    i was thinking of useing an array but then how would i be able to know if the object in the array thats next to the object in the array is actualy next to eachother in the game when played?
    so i did more thinking and relised that essitaly the gameboard is just dead cells. when the player clicks the cell it becomes alive. so i thought what i really need is an array for the dead cells cordinates and when a player clicks the cell it removes the cordinates of the dead cell from the dead cell array and adds it to the live cell array. does that make any since?

would their be an easier way?

I do not believe Unity has a direct built in way to get all objects around another object. However, you can use GameObject.FIndGameObjectsByTag(“tagName”); or similar functions. You can put all these objects into and array and do some checks with Vector3.Distance(cell1,cell2) < distance.

So it would look something like this:

GameObject[] Cells = GameObject.FindGameObjectsByTag("tagname");

for(int i; i < Cells.length; i++){
if(Vector3.Distance(somecell,Cells[i]) < distance){
//do stuff
}
}

Don’t do this, the game will be absurdly slow, and will get drastically slower the larger the play field gets.

Set up a three-dimensional array of Cell’s (a MonoBehaviour-derived class). Each cell should have a script which contains references to all its neighbors, probably as a small array, or maybe as 6 different cell variables (or both). After you instantiate all your cells from your master script, loop through the array again and set its neighbors using the index in each dimension. That is:

for (int x=0;x<xSize;x++) {
for (int y=0;y<ySize;y++) {
for (int z=0;z<zSize;z++) {
Cell thisCell = allCells[x,y,z];
if (x >= 1) thisCell.leftNeighbor = allCells[x-1,y,z];
if (x<xSize - 1) thisCell.rightNeighbor = allCells[x+1,y,z];
//and so on
}
}
}

Now each cell can set its own state by checking its own leftNeighbor, rightNeighbor, etc. You’ll want your manager class to loop through its array twice every “frame” (actually every step) - you should probably have two state variables (isAlive and toBeAlive) - in phase 1, check your neighbors’ .isAlive and set your own toBeAlive, and then in phase 2, set each Cell’s isAlive to its own toBeAlive. In your cells’ LateUpdate, set its visual appearance based on whether it’s alive or not.

1 Like
for (int x=0;x<xSize;x++) {
for (int y=0;y<ySize;y++) {
for (int z=0;z<zSize;z++) {
Cell thisCell = allCells[x,y,z];
if (x >= 1) thisCell.leftNeighbor = allCells[x-1,y,z];
if (x<xSize - 1) thisCell.rightNeighbor = allCells[x+1,y,z];
//and so on
}
}
}[/code written by starmanta]

thank you this way makes alot more sense to me ill try it out tonight