How to write fast Game of Life?

6 months ago by gera to c/learn_programming

I like writing the Game of Life as a helloworld program when learning graphics libraries because the naive algorithm is very easy to implement. It is however pretty slow. I want to try writing something more efficient than doing a whole pass through the board each frame, so I wonder which "fast" algorithm is more or less easy to understand and implement?

CameronDev 6 points 6 months ago

Rather than parsing the whole board, keep a hashset or list of all the cells that are live. Then apply the rules to all of those cells and their neighbours. Should avoid needlessly checking dead cells.

path: 0 22130617, hotness: undefined, score: 6, children: 2
gera 3 points 6 months ago

thanks for the idea, that was easy to implement! (odin, sdl)

look at it go:

path: 0 22130617 22132136, hotness: undefined, score: 3, children: 1
CameronDev 1 point 6 months ago path: 0 22130617 22132136 22132746, hotness: undefined, score: 1, children: 0
learn_programming
learn_programming

@programming.dev

login for more options
2216
155
26

Posting Etiquette

  1. Ask the main part of your question in the title. This should be concise but informative.

  2. Provide everything up front. Don't make people fish for more details in the comments. Provide background information and examples.

  3. Be present for follow up questions. Don't ask for help and run away. Stick around to answer questions and provide more details.

  4. Ask about the problem you're trying to solve. Don't focus too much on debugging your exact solution, as you may be going down the wrong path. Include as much information as you can about what you ultimately are trying to achieve. See more on this here: https://xyproblem.info/

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

go to feed...