void life_event(seq_button_t button) { switch (button) { case BUTTON_A: life_init(); break; default: break; } }
int main ( ) /******************************************************************************/ /* Purpose: MAIN is the main program for LIFE_SERIAL. Licensing: This code is distributed under the GNU LGPL license. Modified: 08 September 2013 Author: John Burkardt Reference: Martin Gardner, Mathematical Games: The Fantastic Combinations of John Conway's new solitaire game "Life", Scientific American, Volume 223, Number 4, October 1970, pages 120-123. */ { char filename[] = "life_000.txt"; int it; int it_max; int m; int n; int *grid; double prob; int seed; timestamp ( ); printf ( "\n" ); printf ( "LIFE_SERIAL\n" ); printf ( " C version\n" ); printf ( " Carry out a few steps of John Conway's\n" ); printf ( " Game of Life.\n" ); printf ( "\n" ); it_max = 10; m = 10; n = 10; prob = 0.20; seed = 123456789; for ( it = 0; it <= it_max; it++ ) { if ( it == 0 ) { grid = life_init ( prob, m, n, &seed ); } else { life_update ( m, n, grid ); } life_write ( filename, m, n, grid ); printf ( " %s\n", filename ); filename_inc ( filename ); } /* Free memory. */ free ( grid ); /* Terminate. */ printf ( "\n" ); printf ( "LIFE_SERIAL\n" ); printf ( " Normal end of execution.\n" ); printf ( "\n" ); timestamp ( ); return 0; }
void life_frame(void) { int x, y; uint8_t sub_frame = life_frame_num++ & 0x1f; static bool reset_next = false; if (sub_frame == 0) { if (reset_next) { life_init(); reset_next = false; } for (y = 0; y < HEIGHT; y++) { for (x = 0; x < WIDTH; x++) { bool alive = life_is_alive(x, y); uint8_t age = life_get_age(x, y); int neighbors = life_calc_neighbors(x, y); // Life rules from: // https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life // 1. Any live cell with fewer than two live neighbours dies, as if // caused by under-population. // 2. Any live cell with two or three live neighbours lives on to // the next generation. // 3. Any live cell with more than three live neighbours dies, as // if by over-population. // 4. Any dead cell with exactly three live neighbours becomes a // live cell, as if by reproduction. if (alive) { if (neighbors == 2 || neighbors == 3) { life_set(x, y, true, age < 0xff ? age + 1 : age); } else { life_set(x, y, false, 0); } } else { if (neighbors == 3) { life_set(x, y, true, 0); } else { life_set(x, y, false, 0); } } } } if (!memcmp(life_alive, life_new_alive, sizeof(life_alive)) || !memcmp(life_last_alive, life_new_alive, sizeof(life_alive))) { reset_next = true; } memcpy(life_last_alive, life_alive, sizeof(life_alive)); memcpy(life_alive, life_new_alive, sizeof(life_alive)); } for (y = 0; y < HEIGHT; y++) { for (x = 0; x < WIDTH; x++) { uint32_t color; if (life_is_alive(x, y)) { int age = life_get_age(x, y); uint32_t index = hsv_inc(life_color_index, age * 0x20 * 4); color = hsv_pixel(index); } else { color = matrix_color(0, 0x00, 0x00); } matrix_set_pixel(x, y, color); } } life_color_index = hsv_inc(life_color_index, 4); }