diff --git a/src/gameoflife.cpp b/src/gameoflife.cpp index 3fc18d2dc2ebf258135cb76018456918a1727dad..89251126d6bbbb475bf0d3fe06cd196498b92e4c 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) {