The game of life

Hi, I’ve been working hard the past week trying to program a 3D version of the game of life: http://www.bitstorm.org/gameoflife/ . So far, my results haven’t been good because of my lack of knowledge in python programming.

What I’ve done successfully is set up two object types, cubes and empties. The empties make up the grid in which the Game of Life will take place. The empty uses a near sensor to detect if 3 or more cubes has spawned beside it. If this is true that empty will spawn a cube and delete itself, becoming “alive”. If an empty detects less than 3 cubes nearby, it does nothing. In turn, if a cube detects less than 2 cubes, it deletes itself and spawns an empty in its place. If the cube detects 2 or more cubes beside it it will do nothing. Thus the game of life can be played in 3D.

Unfortunately as you may have already guessed, the game engine has a rough time spawning all those objects with near sensors, and eventually slows to a dead stop on some setups.

My solution would be to simplify everything by simply having cubes that turn their visibility on and off accruing to the same rules above. This would eliminate the need for constant spawning and the use of empties.

I can’t figure out how to program the cubes to detect a nearby cube’s visibility then adjust its own according to the Game of Life rules. Any help on this would be most appreciated.

Any other ideas on how to make this more efficient, or how I can do this would be awesome. Thanks a ton.

The simplest would be just keeping track of ‘living’ cubes by a 3D python list. a 100x100x100 list of all 0 is made:

world = [[[0 for k in range(100)] for j in range(100) ] for i in range(100)]

Edit: But You might be better of with a byte array of 1000000 bytes.

Then apply Your rules of life to that list in pure python.

Finally have a step just presenting the result. Have a list, cubes, with all added cubes - loop trough the world, placing a cube at every living position, if the cubes list is empty, add new cubes - if there cubes left in cubes after world is done - end them or move them behind the camera.

That way You only need as many cube object that is actually used. A good thing as 100100100 cubes is allot of cubes.

I did something rather similar recently, but with more focus on making 2D cave-like maps.
If you alter conways game of life rules slightly that is what you can get. I did it with python, and because collision can be found even on diagonals with the function “collision_sensor.getObjectList” you can find how many are alive.

My solution would be to simplify everything by simply having cubes that turn their visibility on and off accruing to the same rules above. This would eliminate the need for constant spawning and the use of empties.
It is the best way to do it. Then your code does:

get collision list
for each cell in the list (the neighbouring ones) is is visible?
If visible then add 1 to a value of how many are “alive”

Then you come across the rules. I just did it with plain old if-then commands.

To find the property of an object that isn’t the one you are running the script you just need to tell the game engine how to identify it.
So when you iterate through the list of touching objects you can do something like:


col_s = cont.sensors["Collision"]
collisionList = coll_s.getObjectList

value = 0

for touch in collisionList:
    if touch["Visible"] = True:
        value = value + 1

if value > ???:
    obj["Visible"] = False
elif value < ???:
    obj["Visible"] = True

(this code is untested, there may be errors)
So that is when the visibility property is called “visible”

The problem with this method is like lah says, you have 100100100 cubes, or 1000000 cubes total, and it has to run this script for each and every one of them. If you wanted you could have an object that runs this script for every object in the scene
something like:


scene = bge.logic.getCurrentScene()
for cube in scene.object:

and then the code I provided earlier (with indent)

You may have to do some more thinking about what happens in 3D, where each cube has 27 neighboring cubes. Thus 3 is not a great live/die ratio here anymore, 7 may do better as a lower limit, and maybe 11 as an upper.
Also, think about how you are going to tell the cubes whether they are alive or dead at the beginning of the “round”

Hope that gave you a kick start

Thanks for your quick replies. They are extremely helpful and I’ve already learned a ton. I’ll let you know when I have something working.

I’d be interested

I’ve gotten pretty far with the scripts you provided but I’ve run into a wall. I seem to be having a detection problem between the visible and invisible cubes. If you are familiar with the 2D version of The Game of Life you will instantly see the problem with my current setup. Feel free to play around with the file. I’ll be working on the problem too.

FinalProjectConDots5.blend (417 KB)