Last active
September 23, 2016 12:02
-
-
Save remcoder/6007361 to your computer and use it in GitHub Desktop.
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…
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
/* | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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?