| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,182 @@ | |||
| 1 | + /** | ||
| 2 | + * A Processing implementation of Game of Life | ||
| 3 | + * By Joan Soler-Adillon | ||
| 4 | + * | ||
| 5 | + * Press SPACE BAR to pause and change the cell's values with the mouse | ||
| 6 | + * On pause, click to activate/deactivate cells | ||
| 7 | + * Press R to randomly reset the cells' grid | ||
| 8 | + * Press C to clear the cells' grid | ||
| 9 | + * | ||
| 10 | + * The original Game of Life was created by John Conway in 1970. | ||
| 11 | + */ | ||
| 12 | + | ||
| 13 | + // Size of cells | ||
| 14 | + int cellSize = 5; | ||
| 15 | + | ||
| 16 | + // How likely for a cell to be alive at start (in percentage) | ||
| 17 | + float probabilityOfAliveAtStart = 15; | ||
| 18 | + | ||
| 19 | + // Variables for timer | ||
| 20 | + int interval = 100; | ||
| 21 | + int lastRecordedTime = 0; | ||
| 22 | + | ||
| 23 | + // Colors for active/inactive cells | ||
| 24 | + color alive = color(0, 200, 0); | ||
| 25 | + color dead = color(0); | ||
| 26 | + | ||
| 27 | + // Array of cells | ||
| 28 | + int[][] cells; | ||
| 29 | + // Buffer to record the state of the cells and use this while changing the others in the interations | ||
| 30 | + int[][] cellsBuffer; | ||
| 31 | + | ||
| 32 | + // Pause | ||
| 33 | + boolean pause = false; | ||
| 34 | + | ||
| 35 | + void setup() { | ||
| 36 | + size (640, 360); | ||
| 37 | + | ||
| 38 | + // Instantiate arrays | ||
| 39 | + cells = new int[width/cellSize][height/cellSize]; | ||
| 40 | + cellsBuffer = new int[width/cellSize][height/cellSize]; | ||
| 41 | + | ||
| 42 | + // This stroke will draw the background grid | ||
| 43 | + stroke(48); | ||
| 44 | + | ||
| 45 | + noSmooth(); | ||
| 46 | + | ||
| 47 | + // Initialization of cells | ||
| 48 | + for (int x=0; x<width/cellSize; x++) { | ||
| 49 | + for (int y=0; y<height/cellSize; y++) { | ||
| 50 | + float state = random (100); | ||
| 51 | + if (state > probabilityOfAliveAtStart) { | ||
| 52 | + state = 0; | ||
| 53 | + } | ||
| 54 | + else { | ||
| 55 | + state = 1; | ||
| 56 | + } | ||
| 57 | + cells[x][y] = int(state); // Save state of each cell | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + background(0); // Fill in black in case cells don't cover all the windows | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + | ||
| 64 | + void draw() { | ||
| 65 | + | ||
| 66 | + //Draw grid | ||
| 67 | + for (int x=0; x<width/cellSize; x++) { | ||
| 68 | + for (int y=0; y<height/cellSize; y++) { | ||
| 69 | + if (cells[x][y]==1) { | ||
| 70 | + fill(alive); // If alive | ||
| 71 | + } | ||
| 72 | + else { | ||
| 73 | + fill(dead); // If dead | ||
| 74 | + } | ||
| 75 | + rect (x*cellSize, y*cellSize, cellSize, cellSize); | ||
| 76 | + } | ||
| 77 | + } | ||
| 78 | + // Iterate if timer ticks | ||
| 79 | + if (millis()-lastRecordedTime>interval) { | ||
| 80 | + if (!pause) { | ||
| 81 | + iteration(); | ||
| 82 | + lastRecordedTime = millis(); | ||
| 83 | + } | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + // Create new cells manually on pause | ||
| 87 | + if (pause && mousePressed) { | ||
| 88 | + // Map and avoid out of bound errors | ||
| 89 | + int xCellOver = int(map(mouseX, 0, width, 0, width/cellSize)); | ||
| 90 | + xCellOver = constrain(xCellOver, 0, width/cellSize-1); | ||
| 91 | + int yCellOver = int(map(mouseY, 0, height, 0, height/cellSize)); | ||
| 92 | + yCellOver = constrain(yCellOver, 0, height/cellSize-1); | ||
| 93 | + | ||
| 94 | + // Check against cells in buffer | ||
| 95 | + if (cellsBuffer[xCellOver][yCellOver]==1) { // Cell is alive | ||
| 96 | + cells[xCellOver][yCellOver]=0; // Kill | ||
| 97 | + fill(dead); // Fill with kill color | ||
| 98 | + } | ||
| 99 | + else { // Cell is dead | ||
| 100 | + cells[xCellOver][yCellOver]=1; // Make alive | ||
| 101 | + fill(alive); // Fill alive color | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + else if (pause && !mousePressed) { // And then save to buffer once mouse goes up | ||
| 105 | + // Save cells to buffer (so we opeate with one array keeping the other intact) | ||
| 106 | + for (int x=0; x<width/cellSize; x++) { | ||
| 107 | + for (int y=0; y<height/cellSize; y++) { | ||
| 108 | + cellsBuffer[x][y] = cells[x][y]; | ||
| 109 | + } | ||
| 110 | + } | ||
| 111 | + } | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + | ||
| 115 | + | ||
| 116 | + void iteration() { // When the clock ticks | ||
| 117 | + // Save cells to buffer (so we opeate with one array keeping the other intact) | ||
| 118 | + for (int x=0; x<width/cellSize; x++) { | ||
| 119 | + for (int y=0; y<height/cellSize; y++) { | ||
| 120 | + cellsBuffer[x][y] = cells[x][y]; | ||
| 121 | + } | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + // Visit each cell: | ||
| 125 | + for (int x=0; x<width/cellSize; x++) { | ||
| 126 | + for (int y=0; y<height/cellSize; y++) { | ||
| 127 | + // And visit all the neighbours of each cell | ||
| 128 | + int neighbours = 0; // We'll count the neighbours | ||
| 129 | + for (int xx=x-1; xx<=x+1;xx++) { | ||
| 130 | + for (int yy=y-1; yy<=y+1;yy++) { | ||
| 131 | + if (((xx>=0)&&(xx<width/cellSize))&&((yy>=0)&&(yy<height/cellSize))) { // Make sure you are not out of bounds | ||
| 132 | + if (!((xx==x)&&(yy==y))) { // Make sure to to check against self | ||
| 133 | + if (cellsBuffer[xx][yy]==1){ | ||
| 134 | + neighbours ++; // Check alive neighbours and count them | ||
| 135 | + } | ||
| 136 | + } // End of if | ||
| 137 | + } // End of if | ||
| 138 | + } // End of yy loop | ||
| 139 | + } //End of xx loop | ||
| 140 | + // We've checked the neigbours: apply rules! | ||
| 141 | + if (cellsBuffer[x][y]==1) { // The cell is alive: kill it if necessary | ||
| 142 | + if (neighbours < 2 || neighbours > 3) { | ||
| 143 | + cells[x][y] = 0; // Die unless it has 2 or 3 neighbours | ||
| 144 | + } | ||
| 145 | + } | ||
| 146 | + else { // The cell is dead: make it live if necessary | ||
| 147 | + if (neighbours == 3 ) { | ||
| 148 | + cells[x][y] = 1; // Only if it has 3 neighbours | ||
| 149 | + } | ||
| 150 | + } // End of if | ||
| 151 | + } // End of y loop | ||
| 152 | + } // End of x loop | ||
| 153 | + } // End of function | ||
| 154 | + | ||
| 155 | + void keyPressed() { | ||
| 156 | + if (key=='r' || key == 'R') { | ||
| 157 | + // Restart: reinitialization of cells | ||
| 158 | + for (int x=0; x<width/cellSize; x++) { | ||
| 159 | + for (int y=0; y<height/cellSize; y++) { | ||
| 160 | + float state = random (100); | ||
| 161 | + if (state > probabilityOfAliveAtStart) { | ||
| 162 | + state = 0; | ||
| 163 | + } | ||
| 164 | + else { | ||
| 165 | + state = 1; | ||
| 166 | + } | ||
| 167 | + cells[x][y] = int(state); // Save state of each cell | ||
| 168 | + } | ||
| 169 | + } | ||
| 170 | + } | ||
| 171 | + if (key==' ') { // On/off of pause | ||
| 172 | + pause = !pause; | ||
| 173 | + } | ||
| 174 | + if (key=='c' || key == 'C') { // Clear all | ||
| 175 | + for (int x=0; x<width/cellSize; x++) { | ||
| 176 | + for (int y=0; y<height/cellSize; y++) { | ||
| 177 | + cells[x][y] = 0; // Save all to zero | ||
| 178 | + } | ||
| 179 | + } | ||
| 180 | + } | ||
| 181 | + } | ||
| 182 | + | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments