forked from skooter500/OOP-2021-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorfulLife.java
303 lines (270 loc) · 7.39 KB
/
ColorfulLife.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package ie.tudublin;
import processing.core.PApplet;
public class ColorfulLife extends PApplet {
int size = 200;
float cellSize;
float[][] board = new float[size][size];
float[][] next = new float[size][size];
public void clear()
{
for(int row = 0 ; row < size ; row ++)
{
for (int col = 0 ; col < size ; col ++)
{
setCell(board, row, col, -1);
}
}
}
public int countNeighbours(int row, int col)
{
int count = 0;
for(int r = row -1 ; r <= row + 1; r ++)
{
for(int c = col -1 ; c <= col + 1; c ++)
{
if (! (r == row && c == col))
{
if (getCell(board, r, c) > 0)
{
count ++;
}
}
}
}
// OR Use 8 if statements
/*
if (getCell(board, row-1, col-1))
{
count ++;
}
if (getCell(board, row-1, col))
{
count ++;
}
if (getCell(board, row-1, col+1))
{
count ++;
}
if (getCell(board, row, col-1))
{
count ++;
}
if (getCell(board, row, col+1))
{
count ++;
}
if (getCell(board, row+1, col-1))
{
count ++;
}
if (getCell(board, row+1, col))
{
count ++;
}
if (getCell(board, row+1, col+1))
{
count ++;
}
*/
return count;
}
public void setCell(float[][] board, int row, int col, float b)
{
if (row >= 0 && row < size && col >= 0 && col < size)
{
board[row][col] = b;
}
}
public float getCell(float[][] board, int row, int col)
{
if (row >= 0 && row < size && col >= 0 && col < size)
{
return board[row][col];
}
else
{
return -1;
}
}
public void drawBoard(float[][] board)
{
// Use a nested loop
// Use map to calculate x and y
// Rect draw the cell
// Use the cell size variable
// Use some colours!
for(int row = 0 ; row < size ; row ++)
{
for (int col = 0 ; col < size ; col ++)
{
float x = map(col, 0, size, 0, width);
float y = map(row, 0, size, 0, height);
float c = getCell(board, row, col);
if (c > 0 )
{
noStroke();
fill(c, 255, 255);
rect(x, y, cellSize, cellSize);
}
}
}
}
private void printBoard(boolean[][] board)
{
for(int row = 0 ; row < size ; row ++)
{
for (int col = 0 ; col < size ; col ++)
{
print(board[row][col] ? 1 : 0);
}
println();
}
}
public void randomize()
{
for(int row = 0 ; row < size ; row ++)
{
for (int col = 0 ; col < size ; col ++)
{
float dice = random(0.0f, 1.0f);
/*
if (dice < 0.5)
{
board[row][col] = true;
}
else
{
board[row][col] = false;
}
*/
board[row][col] = (dice < 0.5f) ? random(255) : -1;
}
}
}
public void settings()
{
//size(800, 800);
fullScreen(P3D, SPAN);
}
int mode = 0;
boolean paused = false;
public void keyPressed() {
if (keyCode == ' ')
{
paused = ! paused;
}
if (keyCode == '1')
{
randomize();
}
if (keyCode == '2')
{
clear();
}
if (keyCode == '3')
{
drawCross();
}
}
public void drawCross()
{
for(int i = 0 ; i < size ; i ++)
{
setCell(board, size / 2, i, random(255));
setCell(board, i, size / 2, random(255));
}
}
public float averageAround(float[][] board, int row, int col)
{
float xsum = 0;
float ysum = 0;
for(int r = row - 1; r <= row + 1 ; r ++)
{
for(int c = col - 1 ; c <= col + 1 ; c++)
{
float color = getCell(board, r, c);
if (!(r == row && c == col) && color != -1)
{
float angle = map(color , 0, 255, -PI, PI);
xsum += cos(angle);
ysum += sin(angle);
}
}
}
xsum /= 3.0f;
ysum /= 3.0f;
return map(atan2(ysum, xsum), -PI, PI, 0, 255);
}
public void setup() {
colorMode(HSB);
randomize();
/*
board[0][1] = true;
board[1][2] = true;
board[3][2] = true;
*/
println(countNeighbours(0, 2));
cellSize = width / (float) size;
frameRate(20);
//printBoard(board);
}
private void updateBoard()
{
for(int row = 0 ; row < size ; row ++)
{
for(int col = 0 ; col < size ; col ++)
{
int count = countNeighbours(row, col);
float c = getCell(board, row, col);
if (c >= 0)
{
if (count == 2 || count == 3)
{
setCell(next, row, col, c);
}
else
{
setCell(next, row, col, -1);
}
}
else
{
if (count == 3)
{
setCell(next, row, col, averageAround(board, row, col));
}
else
{
setCell(next, row, col, -1);
}
}
}
}
// Swap board and next
float[][] temp = board;
board = next;
next = temp;
}
public void mouseDragged()
{
// This method gets called automatically when the mouse is dragged across the screen
int row = (int) map(mouseY, 0, height, 0, size);
int col = (int) map(mouseX, 0, width, 0, size);
setCell(board, row, col, random(255));
}
public void mousePressed()
{
// This method gets called automatically when the mouse is dragged across the screen
int row = (int) map(mouseY, 0, height, 0, size);
int col = (int) map(mouseX, 0, width, 0, size);
setCell(board, row, col, random(255));
}
public void draw() {
background(0);
drawBoard(board);
if (! paused)
{
updateBoard();
}
}
}