Create a gist now

Instantly share code, notes, and snippets.

I recently got one of those 8x8 LED matrices and I was playing with some Game of Life patterns when I found this pretty repeating pattern. I found it by starting with some random patterns. If you look closely you can see the pattern becoming a mirrored version of itself halfway through. Apparently the pattern doesn't repeat like this on an infin…
/*
I recently got one of those 8x8 LED matrices and I was playing with some Game of Life patterns when I found this pretty repeating pattern. I found it by starting with some random patterns. If you look closely you can see the pattern becoming a mirrored version of itself halfway through. Apparently the pattern doesn't repeat like this on an infinite grid but on this wrapping 8x8 grid it does ;-)
FYI, the LED matrix is a bicolor one (green/red) and has an I2C interface (http://www.adafruit.com/products/902). I'm using the colors as follows:
- newly created cells are green
- cells that are at least 10 generations old are red
- other living cells are yellow (simultaneously green+red)
It's hookup up to my Arduino Uno r3.
here's a video: http://www.youtube.com/watch?v=Ee2hOaQ2RDI
*/
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
boolean cells[8][8];
Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
// game of life
int next[8][8];
void setup() {
Serial.begin(9600);
Serial.write("hello");
randomSeed(analogRead(0));
for (int r=0 ; r<8 ; r++) {
for (int c=0 ; c<8 ; c++) {
if (random(2) >0)
next[r][c] = 1;
}
}
matrix.begin(0x70); // pass in the address
}
void loop() {
game_of_life();
}
int current[8][8] =
{ {0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,1,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} };
int mod(int a) { return (a+8)%8; }
void game_of_life() {
matrix.clear();
// draw
for (int r=0 ; r<8 ; r++) {
for (int c=0 ; c<8 ; c++) {
int color;
if (next[r][c] == 0)
color = 0;
else if (next[r][c] == 1)
color = LED_GREEN;
else if (next[r][c] > 10)
color = LED_RED;
else
color = LED_YELLOW;
matrix.drawPixel(c,r,color);
}
}
matrix.writeDisplay();
// calc next state
for (int r=0 ; r<8 ; r++) {
for (int c=0 ; c<8 ; c++) {
// count alive neighbors
int alive = 0;
alive += current[mod(r+1)][mod(c) ] != 0;
alive += current[mod(r) ][mod(c+1)] != 0;
alive += current[mod(r-1)][mod(c) ] != 0;
alive += current[mod(r) ][mod(c-1)] != 0;
alive += current[mod(r+1)][mod(c+1)] != 0;
alive += current[mod(r-1)][mod(c-1)] != 0;
alive += current[mod(r+1)][mod(c-1)] != 0;
alive += current[mod(r-1)][mod(c+1)] != 0;
if (current[r][c])
if (alive < 2 || alive > 3)
next[r][c] = 0;
else
next[r][c] = current[r][c] + 1;
else
if (alive == 3)
next[r][c] = 1;
}
}
for (int r=0 ; r<8 ; r++) {
for (int c=0 ; c<8 ; c++) {
current[r][c] = next[r][c];
}
}
delay(100);
}
@ozett
ozett commented Sep 23, 2016 edited

wonderful clean code, i use it on my 8x8 matrix just fine.
as i am no mathematician, when the life runs out after some time,
how do you check the end of all iterations, when nothing changes/live anymore?

i like the changing on the matrix while life goes on and want to start it all over again,
but it seems a litte bit over my (coding-)head to find the right code to start the game after its end again?

do you mind bothering you to give me a little hint? 😄
thanks a lot (for sharing) 👍

@ozett
ozett commented Sep 23, 2016 edited

i tried a first approach to check if the iteration/generation of any cell is about > 100, than there was a stable pattern before we start new.
i all cells (or alternativly the sum-count of alive) is zero, than the matriy is empty and we start new.

any other ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment