Stuff I do late at night - The Game of Life

So I have been doing some veeery simple stuff lately

  1. I adapted the “Game of Life” that is in the examples of Processing.org
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Size of cells
    int cellSize = 13;

// How likely for a cell to be alive at start (in percentage)
float probabilityOfAliveAtStart = 5.5;

// Variables for timer
float interval = 1;
float lastRecordedTime = 0;

// Colors for active/inactive cells
float R = 10;
float G = 100;
float B = 20;
float A = 1;
color alive = color(R, G, B, A);
color dead = color(0);

//Function y + F_lashing
float F = 1;
float Y = 0;

// Array of cells
int[][] cells;
// Buffer to record the state of the cells and use this while changing the others in the interactions
int[][] cellsBuffer;

// Pause
boolean pause = false;

void setup() {
size (1670, 880);

// Instantiate arrays
cells = new int[width/cellSize][height/cellSize];
cellsBuffer = new int[width/cellSize][height/cellSize];

// This stroke will draw the background grid
// stroke(48);

// Initialization of cells
for (int x=0; x<width/cellSize; x++) {
for (int y=0; y<height/cellSize; y++) {
float state = random (100);
if (state > probabilityOfAliveAtStart) {
state = 0;
}
else {
state = 1;
}
cells[x][y] = int(state); // Save state of each cell
}
}

background(0,0,0); // Fill in black in case cells don’t cover all the windows
}

void draw() {
R = R + 2.5;
G = G + 5;
B = B + 3.5;
Y += 1;
A = 45pow(2,(-Y+100)Y/1000);
F = Y
Y
Y/250-Y*Y/20;
alive = color(R, G, B, A);
dead = color(Y/1.1,Y/1.1,Y/1.1);
background(Y/1.01,Y/1.01,Y/1.01,1/1000000000000000000L);
//Draw grid
int alivecells = 0; //contador

for (int x=1; x< (width/cellSize) -1 ; x++) {
for (int y=1; y< (height/cellSize) -1; y++) {
if ((cells[x][y]==1) && ((cells[x+1][y]==1 && cells[x-1][y]==1)||(cells[x][y+1]==1 && cells[x][y-1]==1)) /if they are in a verticall or horizontall line/ && (random(0,F)>=1)) {
fill(alive); // If alive
alivecells += 1 ;
}
else {
fill(dead); // If dead
}
rect (xcellSize, ycellSize, cellSize, cellSize);
}
}

//DRAW 2
println("cells: "+alivecells);
println("millis: "+millis());
println("A: "+A);
// Iterate if timer ticks
if (millis()-lastRecordedTime>interval) {
if (!pause) {
iteration();
lastRecordedTime = millis();
}
}

// Create new cells manually on pause
if (pause && mousePressed) {
// Map and avoid out of bound errors
int xCellOver = int(map(mouseX, 0, width, 0, width/cellSize));
xCellOver = constrain(xCellOver, 0, width/cellSize-1);
int yCellOver = int(map(mouseY, 0, height, 0, height/cellSize));
yCellOver = constrain(yCellOver, 0, height/cellSize-1);

// Check against cells in buffer
if (cellsBuffer[xCellOver][yCellOver]==1) { // Cell is alive
  cells[xCellOver][yCellOver]=0; // Kill
  fill(dead); // Fill with kill color
}
else { // Cell is dead
  cells[xCellOver][yCellOver]=1; // Make alive
  fill(alive); // Fill alive color
}

}
else if (pause && !mousePressed) { // And then save to buffer once mouse goes up
// Save cells to buffer (so we opeate with one array keeping the other intact)
for (int x=0; x<width/cellSize; x++) {
for (int y=0; y<height/cellSize; y++) {
cellsBuffer[x][y] = cells[x][y];
}
}
}
}

void iteration() { // When the clock ticks

// Save cells to buffer (so we opeate with one array keeping the other intact)
for (int x=0; x<width/cellSize; x++) {
for (int y=0; y<height/cellSize; y++) {
cellsBuffer[x][y] = cells[x][y];
}
}

// Visit each cell:
for (int x=0; x<width/cellSize; x++) {
for (int y=0; y<height/cellSize; y++) {
// And visit all the neighbours of each cell
int neighbours = 0; // We’ll count the neighbours
for (int xx=x-1; xx<=x+1;xx++) {
for (int yy=y-1; yy<=y+1;yy++) {
if (((xx>=0)&&(xx<width/cellSize))&&((yy>=0)&&(yy<height/cellSize))) { // Make sure you are not out of bounds
if (!((xx==x)&&(yy==y))) { // Make sure to to check against self
if (cellsBuffer[xx][yy]==1){
neighbours ++; // Check alive neighbours and count them
}
} // End of if
} // End of if
} // End of yy loop
} //End of xx loop
// We’ve checked the neigbours: apply rules!
if (cellsBuffer[x][y]==1) { // The cell is alive: kill it if necessary
if (!(neighbours == 1 || neighbours == 2 || neighbours == 3 || neighbours == 4 || neighbours == 5)) {
cells[x][y] = 0; // survive bla bla bla
}
}
else { // The cell is dead: make it live if necessary
if (neighbours == 3) {
cells[x][y] = 1; // birth if bla bla bla
}
} // End of if
} // End of y loop
} // End of x loop
} // End of function

void keyPressed() {
if (key==‘r’ || key == ‘R’) {
R = 10;
G = 100;
B = 20;
A = 1;
F = 1;
Y = 0;
// Restart: reinitialization of cells
for (int x=0; x<width/cellSize; x++) {
for (int y=0; y<height/cellSize; y++) {
float state = random (100);
if (state > probabilityOfAliveAtStart) {
state = 0;
}
else {
state = 1;
}
cells[x][y] = int(state); // Save state of each cell
}
}
}
if (key==’ ') { // On/off of pause
pause = !pause;
}
if (key==‘c’ || key == ‘C’) { // Clear all
for (int x=0; x<width/cellSize; x++) {
for (int y=0; y<height/cellSize; y++) {
cells[x][y] = 0; // Save all to zero
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////The aim was to imitate a little the style of Ghost In The Shell (1995), more precisely the intro. With all those green paterns emergeing of the BIOS and solidifying the OS, to finally turn into the wite color screen that is more alike of the already booted computers.
Basically, a few color equations that change the RGB values and the alfa following polynomic curves. And I added a little of random flashing, just for fun.

The second thing I have done is more handmade, I mean, made from scratch. It is based on the Big Brother, 1984. And it has a “bug”, because when you raise the density of random lines, as it has problems calculating the angle step (I assume, I have not debugged it, yet), it ends by leaving a void sector. Anyway, it is not very complex, but I enjoy doing something useless to not be bored.

int density = 45;
float [] innerx = new float[density+1];
float [] innery = new float[density+1];
float [] outerx= new float[density+1];
float [] outery = new float[density+1];
float angle = 0;
float anglestep = 360/density;
void setup() {
frameRate(15);
size(1000,500);
for (int a = 0; a < density+1; a++) {
innerx[a] = cos(radians(angle))*100+500;
innery[a] = sin(radians(angle))*100+250;
outerx[a] = cos(radians(angle))*200+500;
outery[a] = sin(radians(angle))200+250;
angle = angle + anglestep;
println("inner: “+innerx[a]+” "+innery[a]);
println("outer: “+outerx[a]+” "+outery[a]);
}
background(0);
ellipseMode(CENTER);
stroke(0);
fill(43,85,170,75);
ellipse(500,600,1150,1150);
ellipse(500,-100,1150,1150);
stroke(85,170,255);
ellipse(500,250,400,400);
fill(0);
stroke(255);
ellipse(500,250,200,200);
}
void draw() {
strokeWeight(10);
ellipseMode(CENTER);
stroke(0);
fill(255,255,255,30);
ellipse(500,600,1150,1150);
ellipse(500,-100,1150,1150);
stroke(85,170,255);
fill(54, 138, 181,75);
ellipse(500,250,400,400);
fill(0,0,0);
stroke(255,255,255,60);
ellipse(500,250,200,200);
strokeCap(ROUND);
strokeWeight(5);
for (int a = 0; a < density; a++) {
int b = int(random(-1.5,1.5));
int c = a + b;
if (c<density+1){
if (-1<c){
line(outerx[c],outery[c],innerx[a],innery[a]);
}
else {
line(outerx[density],outery[density],innerx[a],innery[a]);
}
}
else {
line(outerx[0],outery[0],innerx[a],innery[a]);
}
}
/

fill(200);
for (int a = 0; a < density; a++) {
ellipse(innerx[a], innery[a], 10, 10);
ellipse(outerx[a], outery[a], 10, 10);
}
*/
}

It is a real shame that you did not format your code for the forums properly. Because of this, it is unlikely anyone is bothering to run it.

Edit your posts, select your code, and press the FORMAT CODE PROPERLY button. It looks like this: </>

1 Like

@Otimsis, you should really format your code.
I did run it but I also had to correct some typos and I don’t think anyone would make the extra effort.

Anyway, I like the first one.
I would let the green cells eat everything at the end though. Start the same way but when all the sides are almost reach, start to spread also in the missed cells.

The second one is so creepy :scream:
You did get the 1984 feeling! :+1:

You might also consider using Processing.js to deploy your work to a webpage we can visit.

Shameless self-promotion: here is a guide on using Processing.js:

2 Likes