-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGameOfLife.ino
377 lines (346 loc) · 9.26 KB
/
GameOfLife.ino
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "LedControl.h"
// "DIN" data in pin
#define DIN_PIN 12
// "CLK" clock pin
#define CLK_PIN 11
// "CS" pin
#define CS_PIN 10
// grid dimensions. should not be larger than 8x8
#define MAX_Y 8
#define MAX_X 8
// how many custom starting grid patterns
#define MAX_C 15
// time to wait between turns
#define TURN_DELAY 800
// how many turns per game before starting a new game
// you can also use the reset button on the board
#define TURNS_MAX 60
// how many turns to wait if there are no changes before starting a new game
#define NO_CHANGES_RESET 4
int TURNS = 0; // counter for turns
int NO_CHANGES = 0; // counter for turns without changes
// game state. 0 is dead cell, 1 is live cell
boolean grid[MAX_Y][MAX_X] = {
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
};
// custom starting grid patterns
boolean cgrids[MAX_C][MAX_Y][MAX_X] = {
{
{1, 1, 0, 0, 1, 1, 0, 0},
{1, 1, 0, 0, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 0, 0, 1, 1},
{0, 0, 1, 1, 0, 0, 1, 1},
},
{
{1, 0, 1, 0, 0, 0, 1, 0},
{0, 1, 1, 0, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 1, 0},
{0, 1, 0, 0, 0, 1, 1, 0},
{0, 1, 0, 0, 0, 1, 0, 1},
},
{
{1, 0, 1, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 0},
{1, 0, 1, 0, 0, 0, 0, 0},
},
{
{1, 0, 1, 0, 0, 1, 0, 1},
{0, 1, 1, 0, 0, 1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 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
},
{
{1, 0, 0, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 1, 0, 1},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0},
{1, 0, 1, 0, 0, 1, 0, 0},
{0, 1, 0, 0, 0, 0, 1, 0},
{0, 0, 1, 0, 0, 0, 0, 1},
},
};
LedControl lc = LedControl(DIN_PIN, CS_PIN, CLK_PIN, 1);
void setup() {
// seed random from unused analog pin
randomSeed(analogRead(0));
// initialise the LED matrix
lc.shutdown(0, false);
lc.setIntensity(0, 0);
lc.clearDisplay(0);
reset_grid();
display_grid();
// Serial.begin(9600);
// debug_grid();
}
void loop() {
delay(TURN_DELAY);
play_gol();
TURNS++;
// reset the grid if no changes have occured recently
// for when the game enters a static stable state
if (NO_CHANGES > NO_CHANGES_RESET) {
reset_grid();
}
// reset the grid if the loop has been running a long time
// for when the game cycles between a few stable states
if (TURNS > TURNS_MAX) {
reset_grid();
}
display_grid();
}
// play game of life
void play_gol() {
/*
1. Any live cell with fewer than two neighbours dies, as if by loneliness.
2. Any live cell with more than three neighbours dies, as if by
overcrowding.
3. Any live cell with two or three neighbours lives, unchanged, to the next
generation.
4. Any dead cell with exactly three neighbours comes to life.
*/
boolean new_grid[MAX_Y][MAX_X] = {
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0},
};
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
int neighboughs = count_neighboughs(y, x);
if (grid[y][x] == 1) {
if ((neighboughs == 2) || (neighboughs == 3)) {
new_grid[y][x] = 1;
} else {
new_grid[y][x] = 0;
}
} else {
if (neighboughs == 3) {
new_grid[y][x] = 1;
} else {
new_grid[y][x] = 0;
}
}
}
}
// update the current grid from the new grid and count how many changes
// occured
int changes = 0;
for (int y = 0; y < MAX_Y; y++) {
for (int x = 0; x < MAX_X; x++) {
if (new_grid[y][x] != grid[y][x]) {
changes++;
}
grid[y][x] = new_grid[y][x];
}
}
// update global counter when no changes occured
if (changes == 0) {
NO_CHANGES++;
}
}
// count the number of neighbough live cells for a given cell
int count_neighboughs(int y, int x) {
int count = 0;
// -- Row above us ---
if (y > 0) {
// above left
if (x > 0) {
count += grid[y - 1][x - 1];
}
// above
count += grid[y - 1][x];
// above right
if ((x + 1) < 8) {
count += grid[y - 1][x + 1];
}
}
// -- Same row -------
// left
if (x > 0) {
count += grid[y][x - 1];
}
// right
if ((x + 1) < 8) {
count += grid[y][x + 1];
}
// -- Row below us ---
if ((y + 1) < 8) {
// below left
if (x > 0) {
count += grid[y + 1][x - 1];
}
// below
count += grid[y + 1][x];
// below right
if ((x + 1) < 8) {
count += grid[y + 1][x + 1];
}
}
return count;
}
// reset the grid
void reset_grid() {
NO_CHANGES = 0;
TURNS = 0;
int grid_type = random(0, 4);
int custom_grid_choice = random(0, MAX_C - 1);
for (int y = 0; y < MAX_Y; y++) {
for (int x = 0; x < MAX_X; x++) {
if (grid_type == 0) {
// use a custom starting grid pattern
grid[y][x] = cgrids[custom_grid_choice][y][x];
} else {
// create a random starting grid pattern
if (random(0, MAX_X) <= 1) {
grid[y][x] = 1;
}
}
}
}
}
// display the current grid to the LED matrix
void display_grid() {
for (int y = 0; y < MAX_Y; y++) {
for (int x = 0; x < MAX_X; x++) {
lc.setLed(0, y, x, grid[y][x]);
}
}
}
/*
// dump the state of the current grid to the serial connection
void debug_grid() {
for (int y = 0; y < MAX_Y; y++) {
Serial.print("y(");
Serial.print(y);
Serial.print("): ");
for (int x = 0; x < MAX_X; x++) {
Serial.print(grid[y][x]);
Serial.print(", ");
}
Serial.println("");
}
Serial.println("");
}
*/