Debian Codegraph @

examples/prism-processing.html

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