Created
June 21, 2022 13:41
-
-
Save kurtdekker/0097ae1269128ac023e338476edc2090 to your computer and use it in GitHub Desktop.
Conways Game of Life (GOL) cellular automata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
// @kurtdekker | |
// | |
// ultra dead-simple Conways Game Of Life (GOL) implementation in Unity | |
// | |
// To use: | |
// Make the default blank scene with a Camera / Light | |
// Pull the camera back to perhaps (0,0,-80); | |
// Make a blank GameObject at (0,0,0) | |
// Put this script on the blank GameObject | |
// Press PLAY | |
public class ConwaysGOL : MonoBehaviour | |
{ | |
public int Across = 100; | |
public int Down = 70; | |
public float StartingDensity = 0.5f; | |
bool[,] Generation; | |
int[,] WorkCounts; | |
GameObject[,] Presentation; | |
Vector3 CalculatePosition( int i, int j) | |
{ | |
float x = i - (Across - 1) * 0.5f; | |
float y = j - (Down - 1) * 0.5f; | |
return new Vector3( x, y); | |
} | |
void Start () | |
{ | |
Generation = new bool[ Across, Down]; | |
WorkCounts = new int[ Across, Down]; | |
Presentation = new GameObject[ Across, Down]; | |
// make presentation objects, place them appropriately | |
for (int j = 0; j < Down; j++) | |
{ | |
for (int i = 0; i < Across; i++) | |
{ | |
var critter = GameObject.CreatePrimitive( PrimitiveType.Sphere); | |
critter.transform.position = CalculatePosition(i,j); | |
Presentation[i,j] = critter; | |
} | |
} | |
// make a random starting generation | |
for (int j = 0; j < Down; j++) | |
{ | |
for (int i = 0; i < Across; i++) | |
{ | |
Generation[i,j] = Random.value < StartingDensity; | |
} | |
} | |
PresentGeneration(); | |
} | |
// 0 is up, going around clockwise in 45-degree steps | |
static int[] xtable8 = new int[] { 0, 1, 1, 1, 0, -1, -1, -1}; | |
static int[] ytable8 = new int[] { 1, 1, 0, -1, -1, -1, 0, 1}; | |
void CalculateOneGeneration() | |
{ | |
// clear tallies | |
for (int j = 0; j < Down; j++) | |
{ | |
for (int i = 0; i < Across; i++) | |
{ | |
WorkCounts[i,j] = 0; | |
} | |
} | |
// tally to our neighbors | |
for (int j = 0; j < Down; j++) | |
{ | |
for (int i = 0; i < Across; i++) | |
{ | |
if (Generation[i,j]) | |
{ | |
for (int dir8 = 0; dir8 < 8; dir8++) | |
{ | |
int i2 = i + xtable8[dir8]; | |
int j2 = j + ytable8[dir8]; | |
if (i2 >= 0 && i2 < Across) | |
{ | |
if (j2 >= 0 && j2 < Down) | |
{ | |
WorkCounts[i2,j2]++; | |
} | |
} | |
} | |
} | |
} | |
} | |
// create | |
for (int j = 0; j < Down; j++) | |
{ | |
for (int i = 0; i < Across; i++) | |
{ | |
int count = WorkCounts[i,j]; | |
if (Generation[i,j]) | |
{ | |
bool exists = (count == 2) || (count == 3); | |
Generation[i,j] = exists; | |
} | |
else | |
{ | |
Generation[i,j] = count == 3; | |
} | |
} | |
} | |
} | |
void PresentGeneration() | |
{ | |
for (int j = 0; j < Down; j++) | |
{ | |
for (int i = 0; i < Across; i++) | |
{ | |
Presentation[i,j].SetActive( Generation[i,j]); | |
} | |
} | |
} | |
void Update() | |
{ | |
CalculateOneGeneration(); | |
PresentGeneration(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment