diff --git a/src/display.cpp b/src/display.cpp index 217b2988afecc58a78dba5c38e0920700d08c733..9f122cd29b134e2ade456e1c88c4b806e62a602f 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -42,7 +42,7 @@ void displayLoop() { for (int j = 0; j < SCREEN_WIDTH; j++) { - if (g[i][j]) + if (currentGame[i][j]) { backgroundLayer.drawPixel(j, i, gameColor); } @@ -73,14 +73,12 @@ void showEndScreen(int ticks) void displayBrightness(int brightness) { - //logLine("Display Brightness: " + (String)brightness); // brightness = lightPowerMap8bit[brightness]; matrix.setBrightness(brightness); } void gameBrightness(int brightness) { - //logLine("Game Brightness: " + (String)brightness); // brightness = lightPowerMap8bit[brightness]; backgroundLayer.setBrightness(brightness); } diff --git a/src/gameoflife.cpp b/src/gameoflife.cpp index b15ea136d8765992b716c0afccbdc2dd7d300cf3..d863734d4ad39958179cc6271f5a85755c9ce3ab 100644 --- a/src/gameoflife.cpp +++ b/src/gameoflife.cpp @@ -7,8 +7,8 @@ // https://github.com/Stavrosfil/game-of-life-esp32 -int g[SCREEN_HEIGHT][SCREEN_WIDTH]; -int arrayCopy[SCREEN_HEIGHT][SCREEN_WIDTH]; +int currentGame[SCREEN_HEIGHT][SCREEN_WIDTH]; +int nextGame[SCREEN_HEIGHT][SCREEN_WIDTH]; bool runGame = true; bool gameOver = false; @@ -33,7 +33,7 @@ void setupGameOfLife() cellsAliveBefore = 0; noEvolutionTicks = 0; randomSeed(analogRead(34)); - createRandomMatrix(g); + createRandomMatrix(currentGame); for (int i = 0; i < 10; i++) { addGlider(); @@ -56,46 +56,40 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) { cellsAliveNow = 0; currentTick++; - for (int i = 0; i < SCREEN_HEIGHT; i++) - { - for (int j = 0; j < SCREEN_WIDTH; j++) - { - arrayCopy[i][j] = a[i][j]; - } - } - for (int i = 0; i < SCREEN_HEIGHT; i++) + memcpy(nextGame, a, sizeof(a)); // copy currentGame to nextGame + + for (int row = 0; row < SCREEN_HEIGHT; row++) { - for (int j = 0; j < SCREEN_WIDTH; j++) + for (int col = 0; col < SCREEN_WIDTH; col++) { - int total = (a[i][(j - 1) % SCREEN_WIDTH] + a[i][(j + 1) % SCREEN_WIDTH] + a[(i - 1) % SCREEN_HEIGHT][j] + - a[(i + 1) % SCREEN_HEIGHT][j] + a[(i - 1) % SCREEN_HEIGHT][(j - 1) % SCREEN_WIDTH] + - a[(i - 1) % SCREEN_HEIGHT][(j + 1) % SCREEN_WIDTH] + - a[(i + 1) % SCREEN_HEIGHT][(j - 1) % SCREEN_WIDTH] + - a[(i + 1) % SCREEN_HEIGHT][(j + 1) % SCREEN_WIDTH]); - - if (a[i][j] == 1) + int neighbors = (a[row][(col - 1) % SCREEN_WIDTH] + a[row][(col + 1) % SCREEN_WIDTH] + a[(row - 1) % SCREEN_HEIGHT][col] + + a[(row + 1) % SCREEN_HEIGHT][col] + a[(row - 1) % SCREEN_HEIGHT][(col - 1) % SCREEN_WIDTH] + + a[(row - 1) % SCREEN_HEIGHT][(col + 1) % SCREEN_WIDTH] + + a[(row + 1) % SCREEN_HEIGHT][(col - 1) % SCREEN_WIDTH] + + a[(row + 1) % SCREEN_HEIGHT][(col + 1) % SCREEN_WIDTH]); + + // cell was alive + if (a[row][col] == 1) { - if (total < 2 || total > 3) + // too few/many neighbors -> die + if (neighbors < 2 || neighbors > 3) { - arrayCopy[i][j] = 0; + nextGame[row][col] = 0; } + // else: right amount of neighbors -> stay alive } - else if (total == 3) + // cell was dead, but has right amount of neighbors now -> come alive + else if (neighbors == 3) { - arrayCopy[i][j] = 1; + nextGame[row][col] = 1; cellsAliveNow++; } + // cell was dead, too few/many neighbors -> stay dead } } - for (int i = 0; i < SCREEN_HEIGHT; i++) - { - for (int j = 0; j < SCREEN_WIDTH; j++) - { - a[i][j] = arrayCopy[i][j]; - } - } + memcpy(a, nextGame, sizeof(nextGame)); // copy nextGame to currentGame } void createGlider(int i1, int j1, int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) @@ -113,8 +107,9 @@ void createGlider(int i1, int j1, int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) } } -void addGlider() { - createGlider(random(SCREEN_HEIGHT), random(SCREEN_WIDTH), g); +void addGlider() +{ + createGlider(random(SCREEN_HEIGHT), random(SCREEN_WIDTH), currentGame); } void gameLoop() @@ -124,7 +119,7 @@ void gameLoop() return; } - gameOfLife(g); + gameOfLife(currentGame); if (gameOver) { @@ -142,9 +137,6 @@ void gameLoop() if (currentTick % 2) { - // char msg[80]; - // sprintf(msg, "Tick: %4d, Cells now: %4d, before: %4d - No evolution since: %3d", currentTick, cellsAliveNow, cellsAliveBefore, noEvolutionTicks); - // logLine(msg); cellsAliveBefore = cellsAliveNow; } @@ -163,7 +155,8 @@ void gameLoop() finalTicks = currentTick - noEvolutionTicksLimit; currentMillis = millis(); showEndScreen(finalTicks); - if(updateHighscores(gameEra, finalTicks, cellsAliveNow)) { + if (updateHighscores(gameEra, finalTicks, cellsAliveNow)) + { sendHighscores(); } sendGameStats(gameEra, finalTicks, cellsAliveNow); diff --git a/src/gameoflife.h b/src/gameoflife.h index 2af799f335e033ec3a929d1ed25c176cfa7d0240..12855422fa586a5da168d51d918382ae9df6605c 100644 --- a/src/gameoflife.h +++ b/src/gameoflife.h @@ -6,8 +6,8 @@ #define SCREEN_WIDTH 64 #define SCREEN_HEIGHT 64 -extern int g[SCREEN_HEIGHT][SCREEN_WIDTH]; -extern int arrayCopy[SCREEN_HEIGHT][SCREEN_WIDTH]; +extern int currentGame[SCREEN_HEIGHT][SCREEN_WIDTH]; +extern int nextGame[SCREEN_HEIGHT][SCREEN_WIDTH]; extern bool runGame; extern int noEvolutionTicksLimit;