Created
December 31, 2013 04:57
-
-
Save dbernar1/8192810 to your computer and use it in GitHub Desktop.
A couple of hours of implementing Conway's Game of Life with Processing
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
int cell_size = 4; | |
int canvas_size = cell_size * 200; | |
StringList coordinates_of_alive_cells; | |
void setup () { | |
size( canvas_size, canvas_size ); | |
// Initial pattern | |
coordinates_of_alive_cells = new StringList(); | |
coordinates_of_alive_cells.append( "0,0" ); | |
coordinates_of_alive_cells.append( "0,1" ); | |
coordinates_of_alive_cells.append( "1,0" ); | |
coordinates_of_alive_cells.append( "2,3" ); | |
coordinates_of_alive_cells.append( "3,2" ); | |
coordinates_of_alive_cells.append( "3,3" ); | |
coordinates_of_alive_cells.append( "7,1" ); | |
coordinates_of_alive_cells.append( "7,2" ); | |
coordinates_of_alive_cells.append( "7,3" ); | |
frameRate( 2 ); | |
} | |
void draw() { | |
display_current_generation(); | |
coordinates_of_alive_cells = calculate_next_generation( coordinates_of_alive_cells ); | |
} | |
void display_current_generation() { | |
clear_canvas(); | |
display_alive_cells( coordinates_of_alive_cells ); | |
} | |
void clear_canvas() { | |
background( 255 ); | |
} | |
void display_alive_cells( StringList coordinates_of_alive_cells ) { | |
stroke( 0 ); | |
for ( String coordinates_of_one_alive_cell : coordinates_of_alive_cells ) { | |
// get coordinates in a usable format | |
String[] split_coords = split( coordinates_of_one_alive_cell, ',' ); | |
int x_coord_of_alive_cell = int( split_coords[ 0 ] ); | |
int y_coord_of_alive_cell = int( split_coords[ 1 ] ); | |
// display the cell | |
for ( int j = 0; j < cell_size; j++ ) { | |
for ( int k = 0; k < cell_size; k++ ) { | |
point( | |
x_coord_of_alive_cell * cell_size + j + x_coord_of_alive_cell, | |
y_coord_of_alive_cell * cell_size + k+ y_coord_of_alive_cell | |
); | |
} | |
} | |
} | |
} | |
StringList calculate_next_generation( StringList coordinates_of_alive_cells ) { | |
StringList next_generation = new StringList(); | |
int num_alive_neighbours; | |
boolean the_cell_is_alive_in_current_generation; | |
IntDict cells_with_alive_neighbours = figure_out_cells_with_alive_neighbours( coordinates_of_alive_cells ); | |
for ( String cell_coords : cells_with_alive_neighbours.keys() ) { | |
num_alive_neighbours = cells_with_alive_neighbours.get( cell_coords ); | |
the_cell_is_alive_in_current_generation = coordinates_of_alive_cells.hasValue( cell_coords ); | |
if ( | |
( the_cell_is_alive_in_current_generation && ( 2 == num_alive_neighbours || 3 == num_alive_neighbours ) ) | |
|| ( ! the_cell_is_alive_in_current_generation && 3 == num_alive_neighbours ) | |
) { | |
next_generation.append( cell_coords ); | |
} | |
} | |
return next_generation; | |
} | |
IntDict figure_out_cells_with_alive_neighbours( StringList coordinates_of_alive_cells ) { | |
IntDict cells_with_alive_neighbours = new IntDict(); | |
boolean this_cell_has_previously_appeared_as_a_neighbour_of_an_alive_cell; | |
int num_times_this_cell_has_previously_appeared_as_a_neighbour_of_an_alive_cell; | |
for ( String alive_cell : coordinates_of_alive_cells ) { | |
for ( String one_neighbour : get_neighbours_of_cell( alive_cell ) ) { | |
if ( this_cell_has_previously_appeared_as_a_neighbour_of_an_alive_cell = cells_with_alive_neighbours.hasKey( one_neighbour ) ) { | |
num_times_this_cell_has_previously_appeared_as_a_neighbour_of_an_alive_cell = cells_with_alive_neighbours.get( one_neighbour ); | |
cells_with_alive_neighbours.set( one_neighbour, num_times_this_cell_has_previously_appeared_as_a_neighbour_of_an_alive_cell+1 ); | |
} else { | |
cells_with_alive_neighbours.set( one_neighbour, 1 ); | |
} | |
} | |
} | |
return cells_with_alive_neighbours; | |
} | |
StringList get_neighbours_of_cell( String coords ) { | |
StringList neighbours = new StringList(); | |
String[] split_coords = split( coords, ',' ); | |
int x = int( split_coords[ 0 ] ); | |
int y = int( split_coords[ 1 ] ); | |
neighbours.append( str( x-1 )+','+str( y-1 ) ); | |
neighbours.append( str( x )+','+str( y-1 ) ); | |
neighbours.append( str( x+1 )+','+str( y-1 ) ); | |
neighbours.append( str( x-1 )+','+str( y ) ); | |
neighbours.append( str( x+1 )+','+str( y ) ); | |
neighbours.append( str( x-1 )+','+str( y+1 ) ); | |
neighbours.append( str( x )+','+str( y+1 ) ); | |
neighbours.append( str( x+1 )+','+str( y+1 ) ); | |
return neighbours; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment