-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfLife.h
91 lines (80 loc) · 2.98 KB
/
GameOfLife.h
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
#include <FastLED.h>
#include <Animations.h>
class GameOfLife : public Animation
{
private:
// We need 2 frames to write the changes each round
bool frame1[NUM_STRIPS][NUM_LEDS];
bool frame2[NUM_STRIPS][NUM_LEDS];
// Keep pointers to the frames so we can easily swap them each round instead of copying
bool (*lastRound)[NUM_STRIPS][NUM_LEDS] = &frame1;
bool (*nextRound)[NUM_STRIPS][NUM_LEDS] = &frame2;
void GoOneRound()
{
// swap last round with next round
bool (*placeholder)[NUM_STRIPS][NUM_LEDS] = lastRound;
lastRound = nextRound;
nextRound = placeholder;
// Calculate the next round based on the last one
for (int strip = 0; strip < NUM_STRIPS; strip++)
{
for (int led = 0; led < NUM_LEDS; led++)
{
int neighborsAlive = 0;
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
if (i == 0 && j == 0)
{
continue;
}
int neighborStrip = (strip + i + NUM_STRIPS) % NUM_STRIPS;
int neighborLED = (led + j + NUM_LEDS) % NUM_LEDS;
if ((*lastRound)[neighborStrip][neighborLED])
{
neighborsAlive++;
}
}
}
bool isAlive;
if (neighborsAlive < 2)
{
isAlive = false;
}
else if (neighborsAlive > 3)
{
isAlive = false;
}
else if (neighborsAlive == 3)
{
isAlive = true;
}
else
{
isAlive = (*lastRound)[strip][led];
}
(*nextRound)[strip][led] = isAlive;
}
}
}
public:
void draw(CRGB (*canvas)[NUM_STRIPS][NUM_LEDS])
{
GoOneRound();
for (int ledIndex = 0; ledIndex < NUM_LEDS; ledIndex++)
{
for (int stripIndex = 0; stripIndex < NUM_STRIPS; stripIndex++)
{
if ((*nextRound)[stripIndex][ledIndex])
{
(*canvas)[stripIndex][ledIndex] = CRGB::Amethyst;
}
else
{
(*canvas)[stripIndex][ledIndex] = CRGB::Black;
}
}
}
}
};