-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfLife2_optimized_Write.ino
270 lines (191 loc) · 5.14 KB
/
GameOfLife2_optimized_Write.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
//The screen is 1.77" diagonal, with 160 x 128 pixel resolution.
//Include Libs
#include <TFT.h> // Arduino LCD library
#include <SPI.h>
// pin definition for the DUE
#define cs 10
#define dc 9
#define rst 8
#define sd_cs 7
TFT TFTscreen = TFT(cs, dc, rst);
//Variables
#define Delay 200
//const byte SIZEX = 160;
//const byte SIZEY = 128;
const byte SIZEX = 80;
const byte SIZEY = 50;
bool grid[SIZEX][SIZEY];
bool nextGrid[SIZEX][SIZEY];
int difference = 0;
int lastdifference = 0;
int boring = 0;
int alive;
int lastalive = 0;
int lifecount;
int generation;
int highscore;
void setup() {
Serial.begin(9600);
// initialize the display
TFTscreen.begin();
// set the stroke color to white
TFTscreen.stroke(0,0,255);
randomSeed(analogRead(0)); //Initialize Random Number
clearScreen();
//displaystats();
initialize();
delay(Delay);
}
void loop() {
difference = 0;
alive = 0;
//clearScreen();
lifecycle();
drawPixels();
//displaystats();
fillnextgrid();
checksamegrid();
}
//Empty Screen
void clearScreen(){
TFTscreen.background(0, 0, 0);
}
//Initializes the very first screen with random cells
void initialize(){
clearScreen();
//displaystats();
if (generation > highscore)highscore = (generation - 25);
generation = 0;
//Turn ALL Led's on
TFTscreen.background(0, 0, 255);
delay(1000);
//Turn SOME Led's off counting up
for(int x = 0; x < SIZEX;x++){
for(int y = 0; y < SIZEY;y++){
grid[x][y] = random(2);
if(grid[x][y] == 1){
TFTscreen.stroke(0,255,0);
TFTscreen.point( x,y);
}
else{
TFTscreen.stroke(0,0,0);
TFTscreen.point( x,y);
}
}
}
}
void lifecycle(){
for(int x = 0; x < SIZEX; x++){
for(int y = 0; y < SIZEY; y++){
//Count all the neighboring cells
byte count = neighbours(x, y);
// The gamerules
// born
if(grid[x][y] == 0 && count == 3){
nextGrid[x][y] = 1;
difference++;
alive++;
}
// Alive and stay alive
if(grid[x][y] == 1 && count == 2 || count == 3){
nextGrid[x][y] = 1;
alive++;
}
// Alive and die
if(grid[x][y] == 1 && count < 2 || count > 3){
nextGrid[x][y] = 0;
difference++;
alive--;
}
}
}
}
//Writes back all the values that lifecycle() calculated into the temporary 'nextGrid-array' into 'grid-array'
void fillnextgrid(){
for(int x = 0; x < SIZEX; x++){
for(int y = 0; y < SIZEY; y++){
grid[x][y] = nextGrid[x][y];
}
}
}
//Counts the neighbours and gives back the number of neighbours
byte neighbours(byte x, byte y) {
return grid[(x + 1) % SIZEX][y] +
grid[x][(y + 1) % SIZEY] +
grid[(x + SIZEX - 1) % SIZEX][y] +
grid[x][(y + SIZEY - 1) % SIZEY] +
grid[(x + 1) % SIZEX][(y + 1) % SIZEY] +
grid[(x + SIZEX - 1) % SIZEX][(y + 1) % SIZEY] +
grid[(x + SIZEX - 1) % SIZEX][(y + SIZEY - 1) % SIZEY] +
grid[(x + 1) % SIZEX][(y + SIZEY - 1) % SIZEY];
}
//Checks if the grid is the same. It does so by ckecking if a cell is born or has died (difference),
//and by comparing the alive count to a previous value.
void checksamegrid(){
if (difference == lastdifference && alive == lastalive) boring++;
if (boring >= 25 || generation >= 3000){
delay(Delay * 3);
generation = generation + 25;
initialize();
boring = 0;
}
lastdifference = difference;
lastalive = alive;
//if the grid is completely empty, no need to wait for 'boring' to count up. initialize immediately.
lifecount = 0;
for(int x = 0; x < SIZEX; x++){
for(int y = 0; y < SIZEY; y++){
lifecount = lifecount + grid[x][y];
}
}
if (lifecount == 0){
clearScreen();
delay(Delay * 3);
initialize();
}
else generation++;
//Serial.println(alive);
}
/*isplays stats
void displaystats(){
//Normalstats
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0,33);
display.print("Alive:");
display.println(lifecount);
display.print("Gen:");
display.print(generation);
display.print(" ");
//Highscore rotated 90° to display even large numbers
display.setRotation(1); // rotate 90 degrees counter clockwise, can also use values of 2 and 3 to go further.
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(45,55);
display.println("MaxGen:");
display.print(highscore);
// revert back to no rotation
display.setRotation(0);
//COSMETICS
//Horizontalline
display.drawLine(0,30,59,30,BLACK);
//Verticalline
display.drawLine(59,0,59,48,BLACK);
//Box around the game
display.drawRoundRect(13,0,29,29,2, BLACK);
}*/
//Shows the gamescreen
void drawPixels(){
for(int x = 0; x < SIZEX; x++){
for(int y = 0; y < SIZEY; y++){
if(grid[x][y] == 1){
TFTscreen.stroke(0,255,0);
TFTscreen.point(x, y);
}
if(grid[x][y] == 0){
TFTscreen.stroke(0,0,0);
TFTscreen.point( x,y);
}
}
}
}