diff --git a/java/examples/Topics/Cellular Automata/Conway/Conway.pde b/java/examples/Topics/Cellular Automata/Conway/Conway.pde deleted file mode 100644 index 7540f5e85..000000000 --- a/java/examples/Topics/Cellular Automata/Conway/Conway.pde +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Conway's Game of Life - * by Mike Davis. - * - * This program is a simple version of Conway's - * game of Life. A lit point turns off if there - * are fewer than two or more than three surrounding - * lit points. An unlit point turns on if there - * are exactly three lit neighbors. The 'density' - * parameter determines how much of the board will - * start out lit. - */ - -int sx, sy; -float density = 0.5; -int[][][] world; - -void setup() { - size(640, 360); - frameRate(12); - sx = width; - sy = height; - world = new int[sx][sy][2]; - - // Set random cells to 'on' - for (int i = 0; i < sx * sy * density; i++) { - int x = int(random(sx)); - int y = int(random(sy)); - world[x][y][1] = 1; - } -} - -void draw() { - background(0); - - // Drawing and update cycle - for (int x = 0; x < sx; x=x+1) { - for (int y = 0; y < sy; y=y+1) { - //if (world[x][y][1] == 1) - // Change recommended by The.Lucky.Mutt - if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1)) { - world[x][y][0] = 1; - set(x, y, #FFFFFF); - } - if (world[x][y][1] == -1) { - world[x][y][0] = 0; - } - world[x][y][1] = 0; - } - } - // Birth and death cycle - for (int x = 0; x < sx; x=x+1) { - for (int y = 0; y < sy; y=y+1) { - int count = neighbors(x, y); - if (count == 3 && world[x][y][0] == 0) { - world[x][y][1] = 1; - } - if ((count < 2 || count > 3) && world[x][y][0] == 1) { - world[x][y][1] = -1; - } - } - } -} - -// Count the number of adjacent cells 'on' -int neighbors(int x, int y) { - return world[(x + 1) % sx][y][0] + - world[x][(y + 1) % sy][0] + - world[(x + sx - 1) % sx][y][0] + - world[x][(y + sy - 1) % sy][0] + - world[(x + 1) % sx][(y + 1) % sy][0] + - world[(x + sx - 1) % sx][(y + 1) % sy][0] + - world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] + - world[(x + 1) % sx][(y + sy - 1) % sy][0]; -} diff --git a/java/examples/Topics/Cellular Automata/GameOfLife/GameOfLife.pde b/java/examples/Topics/Cellular Automata/GameOfLife/GameOfLife.pde new file mode 100644 index 000000000..68646edfd --- /dev/null +++ b/java/examples/Topics/Cellular Automata/GameOfLife/GameOfLife.pde @@ -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 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; xinterval) { + 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=0)&&(xx=0)&&(yy 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 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