cgol

a cairo-based Game Of Life
git clone git://bvnf.space/cgol.git
Log | Files | Refs

commit 7a617e8e9692a496caf878cfdeac038bbf285bc1
parent 532da220dbb29fb0d09cdce2473634074c487812
Author: aabacchus <ben@bvnf.space>
Date:   Tue, 28 Sep 2021 12:35:51 +0100

add option to run until static

Diffstat:
Mcgol.1 | 8+++++++-
Mmain.c | 14++++++++++++++
2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/cgol.1 b/cgol.1 @@ -6,7 +6,7 @@ .Nd generate images of the game of life .Sh SYNOPSIS .Nm -.Op Ar width height Op Ar gens +.Op Ar width height Op Ar gens | 0 .Sh DESCRIPTION .Nm generates frames of the progression of the game of life as images, @@ -40,6 +40,12 @@ Options: the width and height of the images to be printed, in pixels. .It Ar gens the number of generations to evolve. Must be 0 < gens <= 999999. +However, if +.Ar gens +is 0, +.Nm +will run until there are 4 consecutive generations without a change +in population. .El .Ss Creating a video from the images Using a tool such as diff --git a/main.c b/main.c @@ -30,6 +30,7 @@ int main(int argc, char **argv) { double width, height; int gens; + int check_births = 0; if (argc > 1 && strncmp("-h", *(argv + 1), 2) == 0) { fprintf(stderr, "usage: %s [width height [gens]]\nthe defaults are \"800\" \"600\" \"200\"\n", *argv); @@ -44,6 +45,10 @@ main(int argc, char **argv) { if (argc > 3) { gens = atoi(*++argv); check(gens); + if (gens == 0) { + gens = 999999; + check_births = 1; + } if (gens < 1 || gens > 999999) { /* the upper limit is to ensure the gen number fits onto the * image filenames */ @@ -86,6 +91,8 @@ main(int argc, char **argv) { int gen, n_alive; char *fn = malloc(12 + 6); + int n_alive1, n_alive2, n_alive3; + n_alive3 = n_alive2 = n_alive1 = 0; for (gen = 0; gen < gens; gen++) { #ifdef __TEST printf("==> GEN %d <==\r", gen); @@ -98,6 +105,13 @@ main(int argc, char **argv) { return 1; } n_alive = evolve(&cells[0][0], NCOLS, NROWS); + if (check_births) { + if (n_alive == n_alive1 && n_alive == n_alive2 && n_alive == n_alive3) + break; + n_alive3 = n_alive2; + n_alive2 = n_alive1; + n_alive1 = n_alive; + } #ifndef __TEST printf("%d\n", n_alive); #endif