diff --git a/src/display.cpp b/src/display.cpp index 9f122cd29b134e2ade456e1c88c4b806e62a602f..b4fd813888a9a7ebf1ef5c3370dcb512fd64051d 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -16,6 +16,7 @@ const uint8_t kIndexedLayerOptions = (SM_INDEXED_OPTIONS_NONE); rgb24 gameColor = {colorR, colorG, colorB}; rgb24 textColor = {255, 255, 255}; +rgb24 pixelColor = {255, 255, 255}; SMARTMATRIX_ALLOCATE_BUFFERS(matrix, kMatrixWidth, kMatrixHeight, kRefreshDepth, kDmaBufferRows, kPanelType, kMatrixOptions); SMARTMATRIX_ALLOCATE_BACKGROUND_LAYER(backgroundLayer, kMatrixWidth, kMatrixHeight, COLOR_DEPTH, kBackgroundLayerOptions); @@ -38,13 +39,17 @@ void displayLoop() if (runGame) { backgroundLayer.fillScreen({0, 0, 0}); - for (int i = 0; i < SCREEN_HEIGHT; i++) + for (int row = 0; row < SCREEN_HEIGHT; row++) { - for (int j = 0; j < SCREEN_WIDTH; j++) + for (int col = 0; col < SCREEN_WIDTH; col++) { - if (currentGame[i][j]) + if (currentGame[row][col] > 0) { - backgroundLayer.drawPixel(j, i, gameColor); + int age = colorMap[row][col]; + pixelColor.red = (uint8_t)(age == 1 ? 255 : 0); + pixelColor.green = (uint8_t)(age == 2 ? 255 : 0); + pixelColor.blue = (uint8_t)(age > 2 ? 255 : 0); + backgroundLayer.drawPixel(row, col, pixelColor); } } } diff --git a/src/gameoflife.cpp b/src/gameoflife.cpp index d863734d4ad39958179cc6271f5a85755c9ce3ab..68a29bac2966a212f5e7090e8470e87371ef6b01 100644 --- a/src/gameoflife.cpp +++ b/src/gameoflife.cpp @@ -7,8 +7,9 @@ // https://github.com/Stavrosfil/game-of-life-esp32 -int currentGame[SCREEN_HEIGHT][SCREEN_WIDTH]; -int nextGame[SCREEN_HEIGHT][SCREEN_WIDTH]; +bool currentGame[SCREEN_HEIGHT][SCREEN_WIDTH]; +bool nextGame[SCREEN_HEIGHT][SCREEN_WIDTH]; +int colorMap[SCREEN_HEIGHT][SCREEN_WIDTH]; bool runGame = true; bool gameOver = false; @@ -41,18 +42,18 @@ void setupGameOfLife() logLine("Starting Game " + String(gameEra)); } -void createRandomMatrix(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) +void createRandomMatrix(bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) { - for (int i = 0; i < SCREEN_HEIGHT; i++) + for (int row = 0; row < SCREEN_HEIGHT; row++) { - for (int j = 0; j < SCREEN_WIDTH; j++) + for (int col = 0; col < SCREEN_WIDTH; col++) { - a[i][j] = random(100) < 25 ? 1 : 0; + a[row][col] = random(100) < 25 ? 1 : 0; } } } -void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) +void gameOfLife(bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) { cellsAliveNow = 0; currentTick++; @@ -64,45 +65,55 @@ void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) for (int col = 0; col < SCREEN_WIDTH; col++) { 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]); + 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 (a[row][col]) { // too few/many neighbors -> die if (neighbors < 2 || neighbors > 3) { nextGame[row][col] = 0; + colorMap[row][col] = 0; } // else: right amount of neighbors -> stay alive + else + { + colorMap[row][col]++; + } } // cell was dead, but has right amount of neighbors now -> come alive - else if (neighbors == 3) + else if (neighbors == 3) { nextGame[row][col] = 1; + colorMap[row][col] = 1; cellsAliveNow++; } // cell was dead, too few/many neighbors -> stay dead + else + { + colorMap[row][col] = 0; + } } } memcpy(a, nextGame, sizeof(nextGame)); // copy nextGame to currentGame } -void createGlider(int i1, int j1, int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) +void createGlider(int row, int col, bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]) { // 010 // 001 // 111 int glider[3][3] = {{0, 0, 1}, {1, 0, 1}, {0, 1, 1}}; - for (int i = 0; i < 3; i++) + for (int y = 0; y < 3; y++) { - for (int j = 0; j < 3; j++) + for (int x = 0; x < 3; x++) { - a[i1 + i][j1 + j] = glider[i][j]; + a[row + y][col + x] = glider[y][x]; } } } diff --git a/src/gameoflife.h b/src/gameoflife.h index 12855422fa586a5da168d51d918382ae9df6605c..6697727c4fb994c18b9c9872624bf304310394f9 100644 --- a/src/gameoflife.h +++ b/src/gameoflife.h @@ -6,17 +6,18 @@ #define SCREEN_WIDTH 64 #define SCREEN_HEIGHT 64 -extern int currentGame[SCREEN_HEIGHT][SCREEN_WIDTH]; -extern int nextGame[SCREEN_HEIGHT][SCREEN_WIDTH]; +extern bool currentGame[SCREEN_HEIGHT][SCREEN_WIDTH]; +extern bool nextGame[SCREEN_HEIGHT][SCREEN_WIDTH]; +extern int colorMap[SCREEN_HEIGHT][SCREEN_WIDTH]; extern bool runGame; extern int noEvolutionTicksLimit; extern int defaultBrightness; void setupGameOfLife(); -void createRandomMatrix(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]); -void gameOfLife(int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]); -void createGlider(int i1, int j1, int (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]); +void createRandomMatrix(bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]); +void gameOfLife(bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]); +void createGlider(int row, int col, bool (&a)[SCREEN_HEIGHT][SCREEN_WIDTH]); void addGlider(); void gameLoop(); void endGame();