Skip to content

Instantly share code, notes, and snippets.

@companje
Last active June 8, 2022 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save companje/7990b460ae571e9954fff5847e73a927 to your computer and use it in GitHub Desktop.
Save companje/7990b460ae571e9954fff5847e73a927 to your computer and use it in GitHub Desktop.
Conway Game of Life - 321 bytes in Processing - Code Golf submission
/*
Rick Companje, 2022-06-09, MIT
Code Golf submission: Shortest Game of Life
289 bytes in Processing/Java
https://codegolf.stackexchange.com/questions/3434/shortest-game-of-life
grid 100x100
grid wraps around
draw custom pattern with mouse
*/
void draw(){if(mousePressed){line(pmouseX,pmouseY,mouseX,mouseY);return;}
loadPixels();PImage p=get();for(int i=0,x=0,y=0,n=0,w=100,h=100;i<w*h;i++,
x=i%w,y=i/w,n=0){for(int j=0;j<9;j++)n+=p.get((x+j%3-1+w)%w,(y+j/3-1+h)%h)
/color(0);if(n==3|n!=4)pixels[i]=color(n==3?0:255);}updatePixels();}
//ungolfed:
void draw() {
if (mousePressed) {
line(pmouseX, pmouseY, mouseX, mouseY);
return;
}
loadPixels();
PImage p=get();
for (int i=0, x=0, y=0, n=0, w=100, h=100; i<w*h; i++,
x=i%w, y=i/w, n=0) {
for (int j=0; j<9; j++)n+=p.get((x+j%3-1+w)%w, (y+j/3-1+h)%h)/color(0);
if (n==3|n!=4)pixels[i]=color(n==3?0:255);
}
updatePixels();
}
@companje
Copy link
Author

companje commented Jun 8, 2022

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