Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Last active January 10, 2018 14:03
Show Gist options
  • Select an option

  • Save KrabCode/931a17d6719ce1009a6454935b15c098 to your computer and use it in GitHub Desktop.

Select an option

Save KrabCode/931a17d6719ce1009a6454935b15c098 to your computer and use it in GitHub Desktop.
Cycling through rulesets in 1-dimensional games of life, (see signature 90 for the sierpinski triangle) in Processing according to Shiffman's Nature of Code
int signature = 90;
int[] ruleset = new int[]{0,1,0,1,1,0,1,0};
void setup(){
fullScreen();
}
void draw(){
if(frameCount%60==0){
background(0);
generateNewRuleset();
loadPixels();
int[][] grid = new int[width][height];
grid[width/2][0] = 255;
for(int y = 1; y < height; y++){
for(int x = 1; x < width-1; x++){
grid[x][y] = applyRules(x, y, grid);
setPixel(x, y, color(grid[x][y]));
}
}
updatePixels();
}
}
void generateNewRuleset(){
int input = signature++;
if(signature == 256) {signature = 0;}
boolean[] bits = new boolean[8];
for (int i = 7; i >= 0; i--) {
bits[i] = (input & (1 << i)) != 0;
}
int index = 0;
for(boolean value : bits){
int val = 0;
if(value) val = 1;
ruleset[index++] = val;
}
}
int applyRules(int x, int y, int[][] grid){
boolean left = grid[x-1][y-1]>0;
boolean mid = grid[x][y-1]>0;
boolean right = grid[x+1][y-1]>0;
if(left&&mid&&right) return ruleset[0]*255;
if(left&&mid&&!right) return ruleset[1]*255;
if(left&&!mid&&right) return ruleset[2]*255;
if(left&&!mid&&!right) return ruleset[3]*255;
if(!left&&mid&&right) return ruleset[4]*255;
if(!left&&mid&&!right) return ruleset[5]*255;
if(!left&&!mid&&right) return ruleset[6]*255;
if(!left&&!mid&&!right) return ruleset[7]*255;
return 0;
}
public void setPixel(int x, int y, int clr){
pixels[x + y * width] = clr;
}
@KrabCode
Copy link
Author

2018-01-11
2018-01-10

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