-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeotiles.pde
247 lines (216 loc) · 5.02 KB
/
geotiles.pde
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
// Jeffrey J Guy
// 4 Oct 09
//
// Conway's Game of Life in Processing
// based on the game of life demo at http://ephexi.com/noc/gameoflife2/
// extended to allow user to create initial conditions and run the sim
//
// r to randomly initialize
// p to pause
// g to go
// s to single sTep
// c to clear
// click/drag to modify the board
//
int cellsize = 100;
int COLS, ROWS;
//game of life board
int[][] old_board, new_board;
int bGo = 65535;
int bgColor = 30;
PFont font24;
String outString;
int fade = 0;
int last = 0;
int iteration = 0;
void setup()
{
size(500, 500);
smooth();
//initialize rows, columns and set-up arrays
COLS = width/cellsize;
ROWS = height/cellsize;
old_board = new int[COLS][ROWS];
new_board = new int[COLS][ROWS];
colorMode(RGB,255,255,255,100);
background(bgColor);
//call function to fill array with random values 0 or 1
initBoard(false);
frameRate(15);
//font24 = loadFont("AppleGothic-24.vlw");
textMode(SCREEN);
textAlign(LEFT, TOP);
}
void draw()
{
grid();
//textFont(font24);
text("Iteration " + iteration, 10, 475);
if (keyPressed) checkKeys();
fill(30,40);
rectMode(CORNER);
rect(0,0,width, height);
if (bGo > 0)
{
check();
iteration++;
bGo--;
}
render();
if (fade > 0) {
fill(255, fade);
//textFont(font24);
text(outString, 10, 10);
}
fade -= (millis() - last) / 7;
last = millis();
}
void grid() {
for (int a=0; a<=COLS; a++) {
for (int b=0; b<=ROWS; b++) {
stroke(45);
noFill();
rectMode(CORNER);
rect(a*cellsize, b*cellsize, cellsize, cellsize);
}
}
}
void check() {
//loop through every spot in our 2D array and check spots neighbors
for (int x = 0; x < COLS;x++) {
for (int y = 0; y < ROWS;y++) {
int nb = 0;
//Note the use of mod ("%") below to ensure that cells on the edges have "wrap-around" neighbors
//above row
if (old_board[(x+COLS-1) % COLS ][(y+ROWS-1) % ROWS ] == 1) { nb++; }
if (old_board[ x ][(y+ROWS-1) % ROWS ] == 1) { nb++; }
if (old_board[(x+1) % COLS ][(y+ROWS-1) % ROWS ] == 1) { nb++; }
//middle row
if (old_board[(x+COLS-1) % COLS ][ y ] == 1) { nb++; }
if (old_board[(x+1) % COLS ][ y ] == 1) { nb++; }
//bottom row
if (old_board[(x+COLS-1) % COLS ][(y+1) % ROWS ] == 1) { nb++; }
if (old_board[ x ][(y+1) % ROWS ] == 1) { nb++; }
if (old_board[(x+1) % COLS ][(y+1) % ROWS ] == 1) { nb++; }
//RULES OF "LIFE" HERE
if ((old_board[x][y] == 1) && (nb < 2)) { new_board[x][y] = 0; }
else if ((old_board[x][y] == 1) && (nb > 3)) { new_board[x][y] = 0; }
else if ((old_board[x][y] == 0) && (nb == 3)) { new_board[x][y] = 1; }
else { new_board[x][y] = old_board[x][y]; }
}
}
int[][] tmp = old_board;
old_board = new_board;
new_board = tmp;
}
void render()
{
//RENDER game of life based on "new_board" values
for ( int i = 0; i < COLS;i++)
{
for ( int j = 0; j < ROWS;j++)
{
if ((new_board[i][j] == 1))
{
fill(255);
noStroke();
ellipse(i*cellsize,j*cellsize,cellsize,cellsize);
}
}
}
//swap old and new game of life boards
}
//init board with random "alive" squares
void initBoard(boolean bClear)
{
background(bgColor);
for (int i =0;i < COLS;i++)
{
for (int j =0;j < ROWS;j++)
{
if (random(5) <= 1.5 && !bClear)
{
old_board[i][j] = 1;
new_board[i][j] = 1;
}
else
{
old_board[i][j] = 0;
new_board[i][j] = 0;
}
}
}
}
void updateText(String s)
{
fade = 255;
last = millis();
outString = s;
return;
}
void checkKeys()
{
if(key == 'p' || key == 'P')
{
updateText("Paused.");
bGo = 0;
}
if (key == 'g' || key == 'G')
{
updateText("Go!");
bGo = 65535;
}
if (key == 'r' || key == 'R')
{
updateText("Random init...");
initBoard(false);
iteration = 0;
grid();
}
if (key == 'c' || key == 'C')
{
updateText("Cleared.");
initBoard(true);
iteration = 0;
grid();
}
if (key == 's' || key == 'S')
{
updateText("Single step.");
bGo = 1;
}
}
void mousePressed()
{
if (mouseX<width && mouseX >0 && mouseY <height && mouseY > 0)
{
int new_x;
int new_y;
new_x = mouseX/cellsize;
new_y = mouseY/cellsize;
if (new_board[new_x][new_y] == 0)
{
old_board[new_x][new_y] = 1;
new_board[new_x][new_y] = 1;
}
else
{
old_board[new_x][new_y] = 0;
new_board[new_x][new_y] = 0;
}
render();
}
}
void mouseDragged() {
if (mouseX<width && mouseX >0 && mouseY <height && mouseY > 0)
{
int new_x;
int new_y;
new_x = mouseX/cellsize;
new_y = mouseY/cellsize;
old_board[new_x][new_y] = 1;
new_board[new_x][new_y] = 1;
render();
}
}