I'm trying to get Conway's Game of Life (Conway's Game of Life - Wikipedia) working on an Arduino over the serial monitor. Still lifes do what they should (at least blocks and loaves), but when I try to create a glider, it gives me this shape:
░ ░ ░ ░
░ ░ █ ░
░ █ █ ░
░ █ ░ ░
░ ░ ░ ░
This should turn into a beehive, but it remains this way forever. I'm not sure what part of my code is wrong:
bool matrix [10][10] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};
int currentX = 0;
int currentY = 0;
int liveNeighborCount = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
for (int i = 0; i < 99; i++) {
if (matrix[currentX][currentY] == 0) {
Serial.print("░ ");
}
else {
Serial.print("█ ");
}
currentX++;
if (currentX == 9) {
currentX = 0;
currentY++;
Serial.print("\n");
}
}
Serial.print("\n");
for (int i = 0; i < 99; i++) {
liveNeighborCount = 0;
if (matrix[currentX - 1][currentY - 1] == 1 && currentX != 0 && currentY != 0) { //check neighbor at the top left of current cell
liveNeighborCount++;
}
if (matrix[currentX][currentY - 1] == 1 && currentY != 0) { //check neighbor directly above current cell
liveNeighborCount++;
}
if (matrix[currentX + 1][currentY - 1] == 1 && currentX != 0 && currentY != 9) { //check neighbor at the top right of current cell
liveNeighborCount++;
}
if (matrix[currentX + 1][currentY] == 1 && currentX != 0) { //check neighbor directly right of current cell
liveNeighborCount++;
}
if (matrix[currentX + 1][currentY + 1] == 1 && currentX != 9 && currentY != 9) { //check neighbor at the bottom right of current cell
liveNeighborCount++;
}
if (matrix[currentX][currentY + 1] == 1 && currentY != 9) { //check neighbor directly below current cell
liveNeighborCount++;
}
if (matrix[currentX - 1][currentY + 1] == 1 && currentX != 0 && currentY != 9) { //check neighbor at the bottom left of current cell
liveNeighborCount++;
}
if (matrix[currentX - 1][currentY] == 1 && currentX != 0) {
liveNeighborCount++;
}
setCells();
currentX++;
if (currentX == 9) {
currentX = 0;
currentY++;
}
}
currentX = 0;
currentY = 0;
delay(1000);
}
void setCells() {
if (liveNeighborCount < 2 && matrix[currentX][currentY] == 1) {
matrix[currentX][currentY] = 0;
return;
}
else if (liveNeighborCount == 2 || liveNeighborCount == 3) {
if (matrix[currentX][currentY] == 1) {
matrix[currentX][currentY] = 1;
return;
}
return;
}
else if (liveNeighborCount > 3 && matrix[currentX][currentY] == 1) {
matrix[currentX][currentY] = 0;
return;
}
else if (liveNeighborCount == 3 && matrix[currentX][currentY] == 0) {
matrix[currentX][currentY] = 1;
return;
}
}
If you feel like trying to run it yourself, it should work on any Arduino compatible with a serial connection.