forked from TechNerd1/gameoflifegame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameoflife.cc
274 lines (226 loc) · 6.2 KB
/
gameoflife.cc
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
// Written by Caleb LaFeve
//Conway's game of life
//modified for raspberrypi
//Rules
// Any live cell with fewer than two live neighbors dies, as if by underpopulation.
// Any live cell with two or three live neighbors lives on to the next generation.
// Any live cell with more than three live neighbors dies, as if by overpopulation.
// Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
#include "led-matrix.h"
#include "graphics.h"
#include <unistd.h>
#include <math.h>
#include <stdio.h>
#include <functional>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <ctime>
#include <signal.h>
#include <syslog.h>
#include <sys/time.h>
#include <random>
using namespace std;
#define CLK 8 // USE THIS ON ARDUINO UNO, ADAFRUIT METRO M0, etc.
//#define CLK A4 // USE THIS ON METRO M4 (not M0)
//#define CLK 11 // USE THIS ON ARDUINO MEGA
#define OE 9
#define LAT 10
#define A A0
#define B A1
#define C A2
#define D A3
#define crNum(x,y,z) ((x)+(y))%z
//Animation Speed
//How many loops before it checks if dead
#define resetTime 100
//Replace width, height, and both 2D arrays to the dimensions of your LED matrix
//I was working with a 32x32
#define WIDTH 32
#define HEIGHT 32
using rgb_matrix::GPIO;
using rgb_matrix::RGBMatrix;
using rgb_matrix::Canvas;
volatile bool interrupt_received = false;
static void InterruptHandler(int signo)
{
syslog( LOG_NOTICE, "interrupt handler ");
interrupt_received = true;
}
Canvas *canvas;
int r, g, b;
int counter = 0;
int cells[HEIGHT][WIDTH];
int newCells[HEIGHT][WIDTH];
int age[HEIGHT][WIDTH];
int sum1 = 0;
int animationSpeed=75;
// forward delarations
void setup();
void loop();
int checkSum();
void reset();
void update();
void writeNextGeneration();
int main(int argc, char *argv[])
{
srand (time(NULL));
setlogmask (LOG_UPTO (LOG_NOTICE));
openlog ("gameoflife", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1);
animationSpeed = rand() % 150 + 30;
cout << "speed " << animationSpeed << endl;
int maxtime = 0;
if ( argc > 1 )
{
string test(argv[1]);
syslog( LOG_NOTICE, "running for %s seconds then quitting\n", test.c_str());
maxtime = std::stoi( test );
}
syslog( LOG_NOTICE, "gameoflife started pid: %d", getuid());
GPIO io;
if (!io.Init())
return 1;
std::string font_type = "./pongnumberfont.bdf";
rgb_matrix::Font font;
if (!font.LoadFont(font_type.c_str()))
{
cout << "Couldn't load font " << font_type << std::endl;
return 1;
}
signal(SIGTERM, InterruptHandler);
signal(SIGINT, InterruptHandler);
canvas = new RGBMatrix(&io, 32, 1);
bool cont = true;
time_t t = time(0);
time_t startTime = time(0);
setup();
while( cont )
{
t = time(0);
loop();
if (maxtime > 0 )
{
if( difftime(t,startTime) > maxtime)
{
cont=false;
printf("stopping now\n");
}
}
if (interrupt_received )
{
cont = false;
}
}
canvas->Clear();
delete canvas;
}
void setup() {
//Color
r = 100;
g = 100;
b = 100;
//Create dead/alive cells
//0 == Dead
//1 == Alive
for(unsigned int row = 0; row < WIDTH; row++)
{
for(unsigned int col = 0; col < HEIGHT; col++)
{
cells[row][col] = rand()%2;
age[row][col] = 0;
}
}
}
void loop() {
counter++;
if(counter % resetTime == 0){
sum1 = checkSum();
}
if(counter % resetTime == 1){
if(checkSum() == sum1){
reset();
counter = 0;
}
sum1 = 0;
}//Just in case it still gets stuck
//10000 at 75 ms delay between frames is about 20 minutes
if(counter == 10000){
reset();
counter = 0;
sum1=0;
}
update();
usleep(1000*animationSpeed);
writeNextGeneration();
}
int checkSum(){
int sum = 0;
for(unsigned int row = 0; row < WIDTH; row++) for(unsigned int col = 0; col < HEIGHT; cells[row][col++]) sum += cells[row][col];
return sum;
}
//Resets 2D araay back to randomness
void reset()
{
for(unsigned int row = 0; row < WIDTH; row++) for(unsigned int col = 0; col < HEIGHT; cells[row][col++] = rand()%2);
counter=0;
}
//This method checks every cell(pixel) and check to see how many neighbors it has
//The amount of neighbors determines its future state
void writeNextGeneration(){
for(int row = 0; row < WIDTH; row++){
for(int col = 0; col < HEIGHT; col++){
newCells[row][col] = cells[row][col];
}
}
for(unsigned int row = 0; row < WIDTH; row++){
for(unsigned int col = 0; col < HEIGHT; col++){
int surroundingCells = 0;
bool isAlive=false;
cells[row][col]?isAlive = true:isAlive = false;
for(int i = -1; i < 2; i++) for(int j = -1; j < 2; j++) surroundingCells += cells[crNum(row,i,WIDTH)][crNum(col,j,HEIGHT)];
surroundingCells -= cells[row][col];
//Check neighboring cells and store its future state in a new 2D array
if((surroundingCells < 2 || surroundingCells > 3) && isAlive) newCells[row][col] = 0;
if(surroundingCells == 3 && !isAlive) newCells[row][col] = 1;
}
}
//copy new 2d Array to old
for(int row = 0; row < WIDTH; row++) for(int col = 0; col < HEIGHT; col++)
{
// keep track of the age of this pixel
if ( cells[row][col] == 1 && newCells[row][col] == 1 )
{
age[row][col] = age[row][col] + 1;
if ( age[row][col] > 255 )
age[row][col] = 255;
}
else
age[row][col] = 0;
cells[row][col] = newCells[row][col];
}
}
//Update pixels
void update(){
for(unsigned int row = 0; row < WIDTH; row++)
for(unsigned int col = 0; col < HEIGHT; col++)
if ( cells[row][col] )
{
float brightness = 0.5;
float r = 0;
float g = 255;
float b = 255;
float rn = 255;
float gn = 102;
float bn = 0;
float tmp = (255-age[row][col])/255.0;
float rt = (r-rn)* tmp;
float gt = (g-gn)* tmp;
float bt = (b-bn)* tmp;
float rf = (r-rt)*brightness;
float gf = (g-gt)*brightness;
float bf = (b-bt)*brightness;
canvas->SetPixel(row,col, rf , gf, bf);
}
else
canvas->SetPixel(row,col,0,0,0);
}