From 6195e5b319ca9d3a0fc88209358417646380be40 Mon Sep 17 00:00:00 2001 From: Jan Grewe <jan@faked.org> Date: Tue, 30 May 2023 00:50:39 +0200 Subject: [PATCH] fix restart if alive cell count is flapping --- src/gameoflife.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/gameoflife.cpp b/src/gameoflife.cpp index a5da256..67c70ab 100644 --- a/src/gameoflife.cpp +++ b/src/gameoflife.cpp @@ -9,10 +9,9 @@ int arrayCopy[SCREEN_HEIGHT][SCREEN_WIDTH]; bool runGame = true; -int currentTick = 0; -int cellsAliveBefore = 0; -int cellsAliveNow = 0; -int noEvolutionTicks = 0; +int currentTick; +int cellsAliveBefore; +int noEvolutionTicks; void setupGameOfLife() { @@ -40,8 +39,8 @@ void createRandomMatrix(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) { currentTick++; - cellsAliveBefore = cellsAliveNow; - cellsAliveNow = 0; + int cellsAliveNow = 0; + for (int i = 0; i < SCREEN_HEIGHT; i++) { for (int j = 0; j < SCREEN_WIDTH; j++) @@ -82,7 +81,11 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) a[i][j] = arrayCopy[i][j]; } } - + + if (currentTick % 2) { + cellsAliveBefore = cellsAliveNow; + } + if (cellsAliveNow == cellsAliveBefore) { noEvolutionTicks++; -- GitLab