-
Notifications
You must be signed in to change notification settings - Fork 0
/
CA.cpp
168 lines (155 loc) · 4.32 KB
/
CA.cpp
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
// Conway's game of life
// Contributed by: Vliedel
#include "./matrix/include/led-matrix.h"
#include "./matrix/include/threaded-canvas-manipulator.h"
using namespace rgb_matrix;
class GameLife : public ThreadedCanvasManipulator {
public:
GameLife(Canvas *m, int delay_ms=500, bool torus=true)
: ThreadedCanvasManipulator(m), delay_ms_(delay_ms), torus_(torus) {
width_ = canvas()->width();
height_ = canvas()->height();
// Allocate memory
values_ = new int*[width_];
for (int x=0; x<width_; ++x) {
values_[x] = new int[height_];
}
newValues_ = new int*[width_];
for (int x=0; x<width_; ++x) {
newValues_[x] = new int[height_];
}
// Init values randomly
srand(time(NULL));
for (int x=0; x<width_; ++x) {
for (int y=0; y<height_; ++y) {
values_[x][y]=rand()%2;
}
}
r_ = rand()%255;
g_ = rand()%255;
b_ = rand()%255;
if (r_<150 && g_<150 && b_<150) {
int c = rand()%3;
switch (c) {
case 0:
r_ = 200;
break;
case 1:
g_ = 200;
break;
case 2:
b_ = 200;
break;
}
}
}
~GameLife() {
for (int x=0; x<width_; ++x) {
delete [] values_[x];
}
delete [] values_;
for (int x=0; x<width_; ++x) {
delete [] newValues_[x];
}
delete [] newValues_;
}
void Run() {
while (running() && !interrupt_received) {
updateValues();
for (int x=0; x<width_; ++x) {
for (int y=0; y<height_; ++y) {
if (values_[x][y])
canvas()->SetPixel(x, y, r_, g_, b_);
else
canvas()->SetPixel(x, y, 0, 0, 0);
}
}
usleep(delay_ms_ * 1000); // ms
}
}
private:
int numAliveNeighbours(int x, int y) {
int num=0;
if (torus_) {
// Edges are connected (torus)
num += values_[(x-1+width_)%width_][(y-1+height_)%height_];
num += values_[(x-1+width_)%width_][y ];
num += values_[(x-1+width_)%width_][(y+1 )%height_];
num += values_[(x+1 )%width_][(y-1+height_)%height_];
num += values_[(x+1 )%width_][y ];
num += values_[(x+1 )%width_][(y+1 )%height_];
num += values_[x ][(y-1+height_)%height_];
num += values_[x ][(y+1 )%height_];
}
else {
// Edges are not connected (no torus)
if (x>0) {
if (y>0)
num += values_[x-1][y-1];
if (y<height_-1)
num += values_[x-1][y+1];
num += values_[x-1][y];
}
if (x<width_-1) {
if (y>0)
num += values_[x+1][y-1];
if (y<31)
num += values_[x+1][y+1];
num += values_[x+1][y];
}
if (y>0)
num += values_[x][y-1];
if (y<height_-1)
num += values_[x][y+1];
}
return num;
}
void updateValues() {
// Copy values to newValues
for (int x=0; x<width_; ++x) {
for (int y=0; y<height_; ++y) {
newValues_[x][y] = values_[x][y];
}
}
// update newValues based on values
for (int x=0; x<width_; ++x) {
for (int y=0; y<height_; ++y) {
int num = numAliveNeighbours(x,y);
if (values_[x][y]) {
// cell is alive
if (num < 2 || num > 3)
newValues_[x][y] = 0;
}
else {
// cell is dead
if (num == 3)
newValues_[x][y] = 1;
}
}
}
// copy newValues to values
for (int x=0; x<width_; ++x) {
for (int y=0; y<height_; ++y) {
values_[x][y] = newValues_[x][y];
}
}
}
int** values_;
int** newValues_;
int delay_ms_;
int r_;
int g_;
int b_;
int width_;
int height_;
bool torus_;
};
// cout << "(" << x << "," << y << ")" << endl;
// {cout << " N: (" << ((((x%h)-1) % h + h) % h) << "," << y << endl;}
// {cout << "NE: (" << ((((x%h)-1) % h + h) % h) << "," << ((y+1) % w) << endl;}
// {cout << " E: (" << x << "," << ((y+1)%w) << endl;}
// {cout << "SE: (" << ((((x%h)+1)%h)) << "," << ((y+1) % w) << endl;}
// {cout << " S: (" << (((x%h)+1)%h) << "," << y << endl;}
// {cout << "SW: (" << ((((x%h)+1)%h)) << "," << ((y-1) % w) << endl;}
// {cout << " W: (" << x << "," << ((y-1) % w) << endl;}
// {cout << "NW: (" << ((((x%h)-1) % h + h) % h) << "," << ((y-1) % w) << endl;}