public static int LiveNeighborCount( CellGrid input, int x, int y ) { int count = 0; foreach( int[] L in LOCALITY ) count += (Peek( input, x + L[1], y + L[0] ) != 0)? 1 : 0; return count; }
public static void ApplyRule( CellGrid input, CellGrid output, int x, int y ) { int count = LiveNeighborCount( input, x, y ); int value = Peek( input, x, y ); if( value == 0 ) { // dead switch( count ) { // Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. case 3: Poke( output, x, y, 1 ); break; } } else if( value == 1 ) { // alive switch( count ) { // Any live cell with fewer than two live neighbours dies, as if caused by under-population. case 1: Poke( output, x, y, 0 ); break; // Any live cell with two or three live neighbours lives on to the next generation. case 2: case 3: break; // Any live cell with more than three live neighbours dies, as if by overcrowding. case 4: case 5: case 6: case 7: case 8: Poke( output, x, y, 0 ); break; } } }
public static void Step( CellGrid input, CellGrid output ) { // assume output is same size as input for( int iy = 0; iy < input.grid.GetLength(0); ++iy ) { for( int ix = 0; ix < input.grid.GetLength(1); ++ix ) { ApplyRule( input, output, ix, iy ); } } }
public static int Peek( CellGrid input, int x, int y ) { if ( x < 0 || x > input.grid.GetLength(0) ) return 0; // off-grid: dead else if( y < 0 || y > input.grid.GetLength(1) ) return 0; // off-grid: dead else return input.grid[ x, y ]; }
public static void print_CellGrid( CellGrid input ) { // assume output is same size as input System.Console.WriteLine(""); for( int iy = 0; iy < input.grid.GetLength(0); ++iy ) { for( int ix = 0; ix < input.grid.GetLength(1); ++ix ) { System.Console.Write( (input.grid[ ix, iy ] == 1)? "█" : " " ); } System.Console.WriteLine(""); } }
public static void Main() { var frame0 = new CellGrid(); frame0.grid = new int[20,20]; var frame1 = new CellGrid(); frame1.grid = new int[20,20]; frame0.grid[0,1] = 1; frame0.grid[1,2] = 1; frame0.grid[2,0] = 1; frame0.grid[2,1] = 1; frame0.grid[2,2] = 1; print_CellGrid( frame0 ); for( var i = 0; i < 50; ++i ) { var f = (i % 2 == 0)? frame0 : frame1; var f_prime = (i % 2 == 0)? frame1 : frame0; print_CellGrid( f_prime ); } Console.ReadLine(); }
public static void Express( OrganismEncoding encoding, CellGrid grid, int origin_x, int origin_y, bool mirrored ) { }
public static void Poke( CellGrid output, int x, int y, int value ) { output.grid[ x, y ] = value; }