Skip to content

Commit 65378e9

Browse files
committed
added broken game-of-life
1 parent a8cc45b commit 65378e9

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

software/conway/conway.ino

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
//Game of Life with MAX7219
2+
3+
// BROKEN SOMEHOW
4+
5+
/*
6+
7+
* dataPin pin on the Arduino where data gets shifted out
8+
* clockPin pin for the clock
9+
* csPin pin for selecting the device
10+
* numDevices maximum number of devices that can be controled
11+
LedControl(int dataPin, int clkPin, int csPin, int numDevices=1);
12+
*/
13+
/*******************************************************************************/
14+
15+
#include "LedControl.h" //Imports the library
16+
17+
static const int DATA_PIN = 20;
18+
static const int CLK_PIN = 5;
19+
static const int CS_PIN = 21;
20+
21+
LedControl lc = LedControl(DATA_PIN, CLK_PIN, CS_PIN, 1);
22+
23+
#define SIZEX 8 //Sets the X axis size
24+
#define SIZEY 8 //Sets the Y axis size
25+
#define reseedrate 30 //Sets the rate the world is re-seeded
26+
#define brightness 15 //Values from 1 to 15 to set the brightness
27+
long density = 35; //Sets density % during seeding
28+
unsigned long delaytime = 150; //Sets the time each generation is shown
29+
int generation = 0; //Counter for re-seeding
30+
int analogPin = 0; //analogPin for the random seed data
31+
32+
unsigned char world1[8][8] = {
33+
{0, 0, 0, 1, 0, 0, 0, 0},
34+
{0, 0, 1, 0, 1, 0, 0, 0},
35+
{0, 1, 0, 1, 1, 0, 0, 0},
36+
{1, 0, 1, 0, 0, 1, 1, 0},
37+
{0, 1, 1, 0, 0, 1, 0, 1},
38+
{0, 0, 0, 1, 1, 0, 1, 0},
39+
{0, 0, 0, 1, 0, 1, 0, 0},
40+
{0, 0, 0, 0, 1, 0, 0, 0}
41+
};
42+
unsigned char world2[8][8];
43+
44+
/*******************************************************************************/
45+
/* display cells */
46+
47+
void display_cells(unsigned char world1[8][8])
48+
{
49+
int i, j;
50+
for (i = 0; i < SIZEX; i++)
51+
{
52+
for (j = 0; j < SIZEY; j++)
53+
{
54+
lc.setLed(0, i, j, world1[i][j]);
55+
56+
}
57+
}
58+
}
59+
60+
/*******************************************************************************/
61+
/* compute previous generation*/
62+
63+
void previous_generation(unsigned char world1[8][8], unsigned char world2[8][8])
64+
{
65+
int i, j;
66+
67+
for (i = 0; i < SIZEX; i++)
68+
{
69+
for (j = 0; j < SIZEY; j++)
70+
{
71+
world2[i][j] = world1[i][j];
72+
}
73+
}
74+
}
75+
76+
/*******************************************************************************/
77+
/* compute next generation */
78+
79+
void next_generation(unsigned char world1[8][8], unsigned char world2[8][8])
80+
{
81+
int i, j;
82+
83+
for (i = 0; i < SIZEX; i++)
84+
{
85+
for (j = 0; j < SIZEY; j++)
86+
{
87+
world1[i][j] = world2[i][j];
88+
}
89+
}
90+
}
91+
92+
93+
/*******************************************************************************/
94+
/* compute neighboring cells */
95+
96+
void neighbours(unsigned char world1[8][8], unsigned char world2[8][8])
97+
{
98+
int a = 0;
99+
100+
for (int i = 0; i < SIZEX; i++)
101+
{
102+
int above = (i + SIZEX - 1) % SIZEX;
103+
int below = (i + 1) % SIZEX;
104+
105+
for (int j = 0; j < SIZEY; j++)
106+
{
107+
int left = (j + SIZEY - 1) % SIZEY;
108+
int right = (j + 1) % SIZEY;
109+
110+
a += world1[above][left];
111+
a += world1[above][j];
112+
a += world1[above][right];
113+
a += world1[i][left];
114+
a += world1[i][right];
115+
a += world1[below][left];
116+
a += world1[below][j];
117+
a += world1[below][right];
118+
119+
if ((world1[i][j] == 0) && (a == 3)) {
120+
world2[i][j] = 1;
121+
}
122+
if ((world1[i][j] == 1) && ((a == 2) || (a == 3))) {
123+
world2[i][j] = 1;
124+
}
125+
if ((world1[i][j] == 1) && ((a == 1) || (a == 0) || (a > 3))) {
126+
world2[i][j] = 0;
127+
}
128+
}
129+
}
130+
}
131+
132+
/*******************************************************************************/
133+
/* randomize */
134+
135+
void seedWorld(unsigned char world1[8][8])
136+
{
137+
int i, j;
138+
randomSeed(analogRead(analogPin));
139+
for (i = 0; i < SIZEX; i++)
140+
{
141+
for (j = 0; j < SIZEY; j++)
142+
{
143+
if (random(100) < density); {
144+
world1[i][j] = random(2);
145+
}
146+
}
147+
}
148+
}
149+
150+
/*******************************************************************************/
151+
152+
void setup() {
153+
lc.shutdown(0, false);
154+
lc.setIntensity(0, brightness);
155+
lc.clearDisplay(0);
156+
seedWorld(world1);
157+
}
158+
159+
/*******************************************************************************/
160+
161+
void loop()
162+
{
163+
164+
if (generation++ > reseedrate)
165+
{
166+
seedWorld(world1);
167+
lc.clearDisplay(0);
168+
delay(300);
169+
generation = 0;
170+
}
171+
172+
previous_generation(world1, world2);
173+
neighbours(world1, world2);
174+
next_generation(world1, world2);
175+
display_cells(world1);
176+
delay(delaytime);
177+
178+
179+
}

0 commit comments

Comments
 (0)