Initial Commit

This commit is contained in:
plane000
2018-04-20 10:15:15 +01:00
parent 49150ccfe4
commit 62101e8e61
2870 changed files with 520122 additions and 0 deletions

View File

@@ -0,0 +1,182 @@
/**
* A Processing implementation of Game of Life
* By Joan Soler-Adillon
*
* Press SPACE BAR to pause and change the cell's values with the mouse
* On pause, click to activate/deactivate cells
* Press R to randomly reset the cells' grid
* Press C to clear the cells' grid
*
* The original Game of Life was created by John Conway in 1970.
*/
// Size of cells
int cellSize = 5;
// How likely for a cell to be alive at start (in percentage)
float probabilityOfAliveAtStart = 15;
// Variables for timer
int interval = 100;
int lastRecordedTime = 0;
// Colors for active/inactive cells
color alive = color(0, 200, 0);
color dead = color(0);
// Array of cells
int[][] cells;
// Buffer to record the state of the cells and use this while changing the others in the interations
int[][] cellsBuffer;
// Pause
boolean pause = false;
void setup() {
size (640, 360);
// 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);
noSmooth();
// 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); // Fill in black in case cells don't cover all the windows
}
void draw() {
//Draw grid
for (int x=0; x<width/cellSize; x++) {
for (int y=0; y<height/cellSize; y++) {
if (cells[x][y]==1) {
fill(alive); // If alive
}
else {
fill(dead); // If dead
}
rect (x*cellSize, y*cellSize, cellSize, cellSize);
}
}
// 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 < 2 || neighbours > 3) {
cells[x][y] = 0; // Die unless it has 2 or 3 neighbours
}
}
else { // The cell is dead: make it live if necessary
if (neighbours == 3 ) {
cells[x][y] = 1; // Only if it has 3 neighbours
}
} // End of if
} // End of y loop
} // End of x loop
} // End of function
void keyPressed() {
if (key=='r' || key == 'R') {
// 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
}
}
}
}

View File

@@ -0,0 +1,128 @@
/**
* Spore 1
* by Mike Davis.
*
* A short program for alife experiments. Click in the window to restart.
* Each cell is represented by a pixel on the display as well as an entry in
* the array 'cells'. Each cell has a run() method, which performs actions
* based on the cell's surroundings. Cells run one at a time (to avoid conflicts
* like wanting to move to the same space) and in random order.
*/
World w;
int numcells = 0;
int maxcells = 6700;
Cell[] cells = new Cell[maxcells];
color spore_color;
// set lower for smoother animation, higher for faster simulation
int runs_per_loop = 10000;
color black = color(0, 0, 0);
void setup() {
size(640, 360);
frameRate(24);
reset();
}
void reset() {
clearScreen();
w = new World();
spore_color = color(172, 255, 128);
seed();
}
void seed() {
// Add cells at random places
for (int i = 0; i < maxcells; i++)
{
int cX = (int)random(width);
int cY = (int)random(height);
if (w.getpix(cX, cY) == black) {
w.setpix(cX, cY, spore_color);
cells[numcells] = new Cell(cX, cY);
numcells++;
}
}
}
void draw() {
// Run cells in random order
for (int i = 0; i < runs_per_loop; i++) {
int selected = min((int)random(numcells), numcells - 1);
cells[selected].run();
}
}
void clearScreen() {
background(0);
}
class Cell {
int x, y;
Cell(int xin, int yin) {
x = xin;
y = yin;
}
// Perform action based on surroundings
void run() {
// Fix cell coordinates
while(x < 0) {
x+=width;
}
while(x > width - 1) {
x-=width;
}
while(y < 0) {
y+=height;
}
while(y > height - 1) {
y-=height;
}
// Cell instructions
if (w.getpix(x + 1, y) == black) {
move(0, 1);
} else if (w.getpix(x, y - 1) != black && w.getpix(x, y + 1) != black) {
move((int)random(9) - 4, (int)random(9) - 4);
}
}
// Will move the cell (dx, dy) units if that space is empty
void move(int dx, int dy) {
if (w.getpix(x + dx, y + dy) == black) {
w.setpix(x + dx, y + dy, w.getpix(x, y));
w.setpix(x, y, color(0));
x += dx;
y += dy;
}
}
}
// The World class simply provides two functions, get and set, which access the
// display in the same way as getPixel and setPixel. The only difference is that
// the World class's get and set do screen wraparound ("toroidal coordinates").
class World {
void setpix(int x, int y, int c) {
while(x < 0) x+=width;
while(x > width - 1) x-=width;
while(y < 0) y+=height;
while(y > height - 1) y-=height;
set(x, y, c);
}
color getpix(int x, int y) {
while(x < 0) x+=width;
while(x > width - 1) x-=width;
while(y < 0) y+=height;
while(y > height - 1) y-=height;
return get(x, y);
}
}
void mousePressed() {
numcells = 0;
reset();
}

View File

@@ -0,0 +1,169 @@
/**
* Spore 2
* by Mike Davis.
*
* A short program for alife experiments. Click in the window to restart.
* Each cell is represented by a pixel on the display as well as an entry in
* the array 'cells'. Each cell has a run() method, which performs actions
* based on the cell's surroundings. Cells run one at a time (to avoid conflicts
* like wanting to move to the same space) and in random order.
*/
World w;
int maxcells = 8000;
int numcells;
Cell[] cells = new Cell[maxcells];
color spore1, spore2, spore3, spore4;
color black = color(0, 0, 0);
// set lower for smoother animation, higher for faster simulation
int runs_per_loop = 10000;
void setup()
{
size(640, 360);
frameRate(24);
reset();
}
void reset() {
clearScreen();
w = new World();
spore1 = color(128, 172, 255);
spore2 = color(64, 128, 255);
spore3 = color(255, 128, 172);
spore4 = color(255, 64, 128);
numcells = 0;
seed();
}
void seed()
{
// Add cells at random places
for (int i = 0; i < maxcells; i++)
{
int cX = int(random(width));
int cY = int(random(height));
int c;
float r = random(1);
if (r < 0.25) c = spore1;
else if (r < 0.5) c = spore2;
else if (r < 0.75) c = spore3;
else c = spore4;
if (w.getpix(cX, cY) == black)
{
w.setpix(cX, cY, c);
cells[numcells] = new Cell(cX, cY);
numcells++;
}
}
}
void draw() {
// Run cells in random order
for (int i = 0; i < runs_per_loop; i++) {
int selected = min((int)random(numcells), numcells - 1);
cells[selected].run();
}
}
void clearScreen() {
background(0);
}
class Cell {
int x, y;
Cell(int xin, int yin) {
x = xin;
y = yin;
}
// Perform action based on surroundings
void run() {
// Fix cell coordinates
while(x < 0) {
x+=width;
}
while(x > width - 1) {
x-=width;
}
while(y < 0) {
y+=height;
}
while(y > height - 1) {
y-=height;
}
// Cell instructions
int myColor = w.getpix(x, y);
if (myColor == spore1) {
if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1);
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1);
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1);
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1);
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1);
else move((int)random(3) - 1, 0);
} else if (myColor == spore2) {
if (w.getpix(x - 1, y + 1) == black && w.getpix(x + 1, y + 1) == black && w.getpix(x, y + 1) == black) move(0, 1);
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) != black) move(0, -1);
else if (w.getpix(x + 1, y) == spore1 && w.getpix(x + 1, y - 1) == black) move(1, -1);
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) != black) move(0, -1);
else if (w.getpix(x - 1, y) == spore2 && w.getpix(x - 1, y - 1) == black) move(-1, -1);
else move((int)random(3) - 1, 0);
}
else if (myColor == spore3)
{
if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -1);
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) != black) move(0, 1);
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) == black) move(-1, 1);
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 1);
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(1, 1);
else move((int)random(3) - 1, 0);
}
else if (myColor == spore4)
{
if (w.getpix(x - 1, y - 1) == black && w.getpix(x + 1, y - 1) == black && w.getpix(x, y - 1) == black) move(0, -1);
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) != black) move(0, 1);
else if (w.getpix(x + 1, y) == spore3 && w.getpix(x + 1, y + 1) == black) move(1, 1);
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) != black) move(0, 1);
else if (w.getpix(x - 1, y) == spore4 && w.getpix(x - 1, y + 1) == black) move(-1, 1);
else move((int)random(3) - 1, 0);
}
}
// Will move the cell (dx, dy) units if that space is empty
void move(int dx, int dy) {
if (w.getpix(x + dx, y + dy) == black) {
w.setpix(x + dx, y + dy, w.getpix(x, y));
w.setpix(x, y, color(0));
x += dx;
y += dy;
}
}
}
// The World class simply provides two functions, get and set, which access the
// display in the same way as getPixel and setPixel. The only difference is that
// the World class's get and set do screen wraparound ("toroidal coordinates").
class World {
void setpix(int x, int y, int c) {
while(x < 0) x+=width;
while(x > width - 1) x-=width;
while(y < 0) y+=height;
while(y > height - 1) y-=height;
set(x, y, c);
}
color getpix(int x, int y) {
while(x < 0) x+=width;
while(x > width - 1) x-=width;
while(y < 0) y+=height;
while(y > height - 1) y-=height;
return get(x, y);
}
}
void mousePressed() {
reset();
}

View File

@@ -0,0 +1,92 @@
class CA {
int[] cells; // An array of 0s and 1s
int generation; // How many generations?
int scl; // How many pixels wide/high is each cell?
int[] rules; // An array to store the ruleset, for example {0,1,1,0,1,1,0,1}
CA(int[] r) {
rules = r;
scl = 1;
cells = new int[width/scl];
restart();
}
// Set the rules of the CA
void setRules(int[] r) {
rules = r;
}
// Make a random ruleset
void randomize() {
for (int i = 0; i < 8; i++) {
rules[i] = int(random(2));
}
}
// Reset to generation 0
void restart() {
for (int i = 0; i < cells.length; i++) {
cells[i] = 0;
}
cells[cells.length/2] = 1; // We arbitrarily start with just the middle cell having a state of "1"
generation = 0;
}
// The process of creating the new generation
void generate() {
// First we create an empty array for the new values
int[] nextgen = new int[cells.length];
// For every spot, determine new state by examing current state, and neighbor states
// Ignore edges that only have one neighor
for (int i = 1; i < cells.length-1; i++) {
int left = cells[i-1]; // Left neighbor state
int me = cells[i]; // Current state
int right = cells[i+1]; // Right neighbor state
nextgen[i] = executeRules(left,me,right); // Compute next generation state based on ruleset
}
// Copy the array into current value
for (int i = 1; i < cells.length-1; i++) {
cells[i] = nextgen[i];
}
//cells = (int[]) nextgen.clone();
generation++;
}
// This is the easy part, just draw the cells, fill 255 for '1', fill 0 for '0'
void render() {
for (int i = 0; i < cells.length; i++) {
if (cells[i] == 1) {
fill(255);
} else {
fill(0);
}
noStroke();
rect(i*scl,generation*scl, scl,scl);
}
}
// Implementing the Wolfram rules
// Could be improved and made more concise, but here we can explicitly see what is going on for each case
int executeRules (int a, int b, int c) {
if (a == 1 && b == 1 && c == 1) { return rules[0]; }
if (a == 1 && b == 1 && c == 0) { return rules[1]; }
if (a == 1 && b == 0 && c == 1) { return rules[2]; }
if (a == 1 && b == 0 && c == 0) { return rules[3]; }
if (a == 0 && b == 1 && c == 1) { return rules[4]; }
if (a == 0 && b == 1 && c == 0) { return rules[5]; }
if (a == 0 && b == 0 && c == 1) { return rules[6]; }
if (a == 0 && b == 0 && c == 0) { return rules[7]; }
return 0;
}
// The CA is done if it reaches the bottom of the screen
boolean finished() {
if (generation > height/scl) {
return true;
} else {
return false;
}
}
}

View File

@@ -0,0 +1,37 @@
/**
* Wolfram Cellular Automata
* by Daniel Shiffman.
*
* Simple demonstration of a Wolfram 1-dimensional cellular automata
* When the system reaches bottom of the window, it restarts with a new ruleset
* Mouse click restarts as well.
*/
CA ca; // An instance object to describe the Wolfram basic Cellular Automata
void setup() {
size(640, 360);
int[] ruleset = {0,1,0,1,1,0,1,0}; // An initial rule system
ca = new CA(ruleset); // Initialize CA
background(0);
}
void draw() {
ca.render(); // Draw the CA
ca.generate(); // Generate the next level
if (ca.finished()) { // If we're done, clear the screen, pick a new ruleset and restart
background(0);
ca.randomize();
ca.restart();
}
}
void mousePressed() {
background(0);
ca.randomize();
ca.restart();
}