From afc238af6230d8ef267e77711d53b3030a334bd5 Mon Sep 17 00:00:00 2001
From: Jan Grewe <jan@faked.org>
Date: Thu, 20 Jul 2023 23:31:20 +0200
Subject: [PATCH] tweak stuck evolution detection

---
 src/gameoflife.cpp | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/src/gameoflife.cpp b/src/gameoflife.cpp
index 3fc18d2..8925112 100644
--- a/src/gameoflife.cpp
+++ b/src/gameoflife.cpp
@@ -18,7 +18,7 @@ uint16_t gameEra = 0;
 uint16_t currentTick;
 uint16_t finalTicks;
 uint16_t cellsAliveNow;
-uint16_t cellsAliveBefore;
+uint8_t cellsAliveBeforeList[9];
 uint8_t noEvolutionTicks;
 uint8_t noEvolutionTicksLimit = 100;
 
@@ -31,7 +31,7 @@ void setupGame()
   gameEra++;
   currentTick = 0;
   finalTicks = 0;
-  cellsAliveBefore = 0;
+  memset(cellsAliveBeforeList, 0, sizeof(cellsAliveBeforeList));
   noEvolutionTicks = 0;
   randomSeed(analogRead(34));
   createRandomMatrix(currentGame);
@@ -128,6 +128,25 @@ void addGlider()
   createGlider(random(SCREEN_HEIGHT), random(SCREEN_WIDTH), currentGame);
 }
 
+bool isGameEvolving()
+{
+  bool isEvolving = true;
+
+  if (
+      // check for 2-tick oscillators
+      cellsAliveNow == cellsAliveBeforeList[1] ||
+      // check for 3-tick oscillators
+      cellsAliveNow == cellsAliveBeforeList[2])
+  {
+    isEvolving = false;
+  }
+
+  memcpy(cellsAliveBeforeList + 1, cellsAliveBeforeList, sizeof(cellsAliveBeforeList) - sizeof(cellsAliveBeforeList[8]));
+  cellsAliveBeforeList[0] = cellsAliveNow;
+
+  return isEvolving;
+}
+
 void gameLoop()
 {
   if (runGame == false)
@@ -151,18 +170,14 @@ void gameLoop()
   else // not gameOver
   {
 
-    if (cellsAliveNow >= cellsAliveBefore - 5 &&
-        cellsAliveNow <= cellsAliveBefore + 5)
+    if (isGameEvolving())
     {
-      noEvolutionTicks++;
+      noEvolutionTicks = 0;
     }
     else
     {
-      noEvolutionTicks = 0;
+      noEvolutionTicks++;
     }
-    
-    cellsAliveBefore = cellsAliveNow;
-      
 
     if (noEvolutionTicks > noEvolutionTicksLimit)
     {
-- 
GitLab