#include "display.h"
#define DEBUG false
#define TICK_SPEED 50
// uint8_t?
bool current[WIDTH][HEIGHT];
bool successor[WIDTH][HEIGHT];
//generation
int tickNumber = 0;
Display *display;
// encap grid stuff into Grid? Universe? Environment? Board?
void setup() {
Serial.begin(115200);
// Serial.begin(57600);
// clear storage buffers
for (byte x = 0; x < WIDTH; ++x) {
for (byte y = 0; y < HEIGHT; ++y) {
current[x][y] = false;
successor[x][y] = false;
}
}
drawGlider(0, 0);
// for (byte i = 0; i < 16; ++i) {
// current[i][i] = true;
// current[16+i][15-i] = true;
// }
// fb[0] = 1;
display = new Display();
display->Render(current);
}
void loop() {
delay(TICK_SPEED);
tick();
display->Render(current);
}
bool debug = false;
void tick() {
// @ TICK 50+
log("TICK %03i: ", ++tickNumber);
for (byte x = 0; x < WIDTH; ++x) for (byte y = 0; y < HEIGHT; ++y) if (current[x][y]) log("(%i,%i) ", x, y);
log("\n");
// _nextBit();
for (byte x = 0; x < WIDTH; ++x) {
for (byte y = 0; y < HEIGHT; ++y) {
if (x == 15 && y == 0) debug = true;
byte regionSum = region(x, y);
debug = false;
// if (x >= 14 && x <= 16 && y == 0) {
// log(" (%02i, %02i): %d\n", x, y, regionSum);
// }
if (regionSum == 3) {
successor[x][y] = true;
} else if (regionSum != 4) {
successor[x][y] = false;
}
}
}
// swap arrays 🤮
for (byte x = 0; x < WIDTH; ++x) {
for (byte y = 0; y < HEIGHT; ++y) {
current[x][y] = successor[x][y];
}
}
}
byte region(byte x, byte y) {
return cell(x-1, y-1) +
cell(x, y-1) +
cell(x+1, y-1) +
cell(x-1, y) +
cell(x, y) +
cell(x+1, y) +
cell(x-1, y+1) +
cell(x, y+1) +
cell(x+1, y+1);
}
bool cell(int i, int j) {
if (debug) log(" (%02i,%02i) -> (%02i,%02i): %d\n", i, j, x(i), y(j), current[x(i)][y(j)]);
return current[x(i)][y(j)];
}
byte x(int i) {
if (i < 0) {
return i + WIDTH;
}
if (i >= WIDTH) {
return i - WIDTH;
}
return i;
}
byte y(int j) {
if (j < 0) {
return j + HEIGHT;
}
if (j >= HEIGHT) {
return j - HEIGHT;
}
return j;
}
void drawGlider(byte x, byte y) {
current[x+1][y] = true;
current[x+2][y+1] = true;
current[x][y+2] = true;
current[x+1][y+2] = true;
current[x+2][y+2] = true;
successor[x+1][y] = true;
successor[x+2][y+1] = true;
successor[x][y+2] = true;
successor[x+1][y+2] = true;
successor[x+2][y+2] = true;
}
template<typename... Args> void log(const char* fmt, Args... args) {
if (!DEBUG) return;
char buffer[100];
sprintf(buffer, fmt, args...);
Serial.print(buffer);
}