C# – Conways Game of Life C#

algorithmscimplementations

Not sure if this is the correct place for this question or SO – mods please move if necessary.

I am going to have a go at creating GoL over the weekend as a little test project : http://en.wikipedia.org/wiki/Conway's_Game_of_Life

I understand the algorithm, however I just wanted to check regarding the implementation, from maybe somebody that has tried it. Essentially, my first (basic) implementation, will be a static grid at a set speed.

If I understand correctly, these are the steps I will need:

  1. Initial seed
  2. Create 2d array with initial set up
  3. Foreach iteration, create temporary array, calculating each cells new state based on the Game of Life algorithm
  4. Assign temp array to proper array.
  5. Redraw grid from proper array.

My concerns are over speed. When I am populating the grid from the array, would it simply be a case of looping through the array, assigning on or off to each grid cell and then redraw the grid?

Am I on the correct path?

Best Answer

Instead of creating a temporary array for use when you update the game grid, you can get a simpler algorithm and better speed if you create 2 grids and fill one grid using data from the other. That way you don't have to cencern yourself with not overwriting current data in the next pass.

I.e. the grids take turns being updated;

  1. fill grid1 with the initial setup
  2. update the display using grid1
  3. build grid2 using the data in grid1
  4. swap grid1 and grid2
  5. proceed to step 2
Related Topic