Created
January 31, 2021 16:07
-
-
Save xen0bit/a12c3f5b6b6c0d4abec0988a7dc59b79 to your computer and use it in GitHub Desktop.
arduino logic GOL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//General Program flow. | |
//Replace lines 34/35 with something to log to STDOUT with like "X" for dead and "O" for alive to debug | |
//Shouldn't require any external dependencies | |
//Setup | |
//initGrid(); | |
//drawGrid(); | |
//LOOP | |
// computeCA(); | |
// drawGrid(); | |
#define GRIDX 160 | |
#define GRIDY 120 | |
#define CELLXY 2 | |
#define GEN_DELAY 0 | |
//Current grid | |
uint8_t grid[GRIDX][GRIDY]; | |
//The new grid for the next generation | |
uint8_t newgrid[GRIDX][GRIDY]; | |
uint16_t genCount = 0; | |
//Draws the grid on the display | |
void drawGrid(void) { | |
uint16_t color = TFT_WHITE; | |
for (int16_t x = 1; x < GRIDX - 1; x++) { | |
for (int16_t y = 1; y < GRIDY - 1; y++) { | |
if ((grid[x][y]) != (newgrid[x][y])) { | |
if (newgrid[x][y] == 1) color = 0x0F00; //random(0xFFFF); | |
else color = 0; | |
//M5.Lcd.fillRect(CELLXY * x, CELLXY * y, CELLXY, CELLXY, color); | |
//Replace this with something to write to STDOUT | |
} | |
} | |
} | |
} | |
//Initialise Grid | |
void initGrid(void) { | |
for (int16_t x = 0; x < GRIDX; x++) { | |
for (int16_t y = 0; y < GRIDY; y++) { | |
newgrid[x][y] = 0; | |
if (x == 0 || x == GRIDX - 1 || y == 0 || y == GRIDY - 1) { | |
grid[x][y] = 0; | |
} | |
else { | |
if (random(3) == 1) | |
grid[x][y] = 1; | |
else | |
grid[x][y] = 0; | |
} | |
} | |
} | |
} | |
//Compute the CA. Basically everything related to CA starts here | |
void computeCA() { | |
for (int16_t x = 1; x < GRIDX; x++) { | |
for (int16_t y = 1; y < GRIDY; y++) { | |
int neighbors = getNumberOfNeighbors(x, y); | |
if (grid[x][y] == 1 && (neighbors == 2 || neighbors == 3 )) | |
{ | |
newgrid[x][y] = 1; | |
} | |
else if (grid[x][y] == 1) newgrid[x][y] = 0; | |
if (grid[x][y] == 0 && (neighbors == 3)) | |
{ | |
newgrid[x][y] = 1; | |
} | |
else if (grid[x][y] == 0) newgrid[x][y] = 0; | |
} | |
} | |
} | |
// Check the Moore neighborhood | |
int getNumberOfNeighbors(int x, int y) { | |
return grid[x - 1][y] + grid[x - 1][y - 1] + grid[x][y - 1] + grid[x + 1][y - 1] + grid[x + 1][y] + grid[x + 1][y + 1] + grid[x][y + 1] + grid[x - 1][y + 1]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment