Skip to content

Commit fa6ab30

Browse files
committed
Merge pull request #9 from FransFaase/master
Fixed and improved conway.ico. Added detection of stable pattern. Inc…
2 parents 7763627 + 4ffd8d1 commit fa6ab30

2 files changed

Lines changed: 130 additions & 89 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//After Komputerstrukturen 1969/70 by Peter Struycken with MAX7219
2+
3+
/*
4+
* dataPin pin on the Arduino where data gets shifted out
5+
* clockPin pin for the clock
6+
* csPin pin for selecting the device
7+
* numDevices maximum number of devices that can be controled
8+
LedControl(int dataPin, int clkPin, int csPin, int numDevices=1);
9+
*/
10+
11+
#include "LedControl.h" //Imports the library
12+
13+
static const int DATA_PIN = 20;
14+
static const int CLK_PIN = 5;
15+
static const int CS_PIN = 21;
16+
17+
LedControl lc = LedControl(DATA_PIN, CLK_PIN, CS_PIN, 1);
18+
19+
#define brightness 1 //Values from 1 to 15 to set the brightness
20+
int analogPin = 0; //analogPin for the random seed data
21+
22+
unsigned char world[8][8];
23+
unsigned char state[4][4];
24+
int prev_i;
25+
int prev_j;
26+
27+
void setWorld()
28+
{
29+
for (int i = 0; i < 8; i++)
30+
for (int j = 0; j < 8; j++)
31+
world[i][j] = 0;
32+
33+
for (int i = 0; i < 4; i++)
34+
for (int j = 0; j < 4; j++)
35+
{
36+
int i2 = i * 2;
37+
int j2 = j * 2;
38+
unsigned char s = state[i][j];
39+
if (s == 0)
40+
world[i2][j2] = 1;
41+
else if (s == 1)
42+
world[i2][j2+1] = 1;
43+
else if (s == 2)
44+
world[i2+1][j2+1] = 1;
45+
else
46+
world[i2+1][j2] = 1;
47+
}
48+
for (int i = 0; i < 8; i++)
49+
{
50+
int r = 0;
51+
for (int j = 0; j < 8; j++)
52+
r = (r << 1) | world[i][j];
53+
lc.setColumn(0, 7-i, r);
54+
}
55+
}
56+
57+
void setup() {
58+
lc.shutdown(0, false);
59+
lc.setIntensity(0, brightness);
60+
lc.clearDisplay(0);
61+
randomSeed(analogRead(analogPin));
62+
for (int i = 0; i < 4; i++)
63+
for (int j = 0; j < 4; j++)
64+
state[i][j] = random(4);
65+
setWorld();
66+
prev_i = 0;
67+
prev_j = 0;
68+
}
69+
70+
/*******************************************************************************/
71+
72+
void loop()
73+
{
74+
delay(300);
75+
76+
int i = random(3);
77+
if (i >= prev_i)
78+
i++;
79+
int j = random(3);
80+
if (j >= prev_j)
81+
j++;
82+
int old_s = state[i][j];
83+
state[i][j] = (old_s + (random(2) ? 1 : 3)) % 4;
84+
for (int i = 0; i < 3; i++)
85+
for (int j = 0; j < 3; j++)
86+
if ( state[i][j] == 2 && state[i][j+1] == 3
87+
&& state[i+1][j+1] == 0 && state[i+1][j] == 1)
88+
{
89+
state[i][j] = old_s;
90+
return;
91+
}
92+
setWorld();
93+
delay(1000);
94+
prev_i = i;
95+
prev_j = j;
96+
97+
}

software/conway/conway.ino

Lines changed: 33 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//Game of Life with MAX7219
22

3-
// BROKEN SOMEHOW
4-
53
/*
64
75
* dataPin pin on the Arduino where data gets shifted out
@@ -22,80 +20,58 @@ LedControl lc = LedControl(DATA_PIN, CLK_PIN, CS_PIN, 1);
2220

2321
#define SIZEX 8 //Sets the X axis size
2422
#define SIZEY 8 //Sets the Y axis size
25-
#define reseedrate 30 //Sets the rate the world is re-seeded
23+
#define reseedrate 100 //Sets the rate the world is re-seeded
2624
#define brightness 15 //Values from 1 to 15 to set the brightness
2725
long density = 35; //Sets density % during seeding
2826
unsigned long delaytime = 150; //Sets the time each generation is shown
2927
int generation = 0; //Counter for re-seeding
3028
int analogPin = 0; //analogPin for the random seed data
29+
bool stable = true;
3130

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-
};
31+
unsigned char world1[8][8];
4232
unsigned char world2[8][8];
4333

34+
4435
/*******************************************************************************/
45-
/* display cells */
36+
/* randomize */
4637

47-
void display_cells(unsigned char world1[8][8])
38+
void seedWorld(unsigned char world1[8][8])
4839
{
4940
int i, j;
5041
for (i = 0; i < SIZEX; i++)
5142
{
5243
for (j = 0; j < SIZEY; j++)
5344
{
54-
lc.setLed(0, i, j, world1[i][j]);
55-
45+
if (random(100) < density); {
46+
world1[i][j] = random(2);
47+
}
5648
}
5749
}
5850
}
5951

6052
/*******************************************************************************/
61-
/* compute previous generation*/
62-
63-
void previous_generation(unsigned char world1[8][8], unsigned char world2[8][8])
64-
{
65-
int i, j;
6653

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-
}
54+
void setup() {
55+
lc.shutdown(0, false);
56+
lc.setIntensity(0, brightness);
57+
lc.clearDisplay(0);
58+
randomSeed(analogRead(analogPin));
59+
seedWorld(world1);
7460
}
7561

7662
/*******************************************************************************/
77-
/* compute next generation */
7863

79-
void next_generation(unsigned char world1[8][8], unsigned char world2[8][8])
64+
void loop()
8065
{
81-
int i, j;
8266

83-
for (i = 0; i < SIZEX; i++)
67+
if (stable || generation++ > reseedrate)
8468
{
85-
for (j = 0; j < SIZEY; j++)
86-
{
87-
world1[i][j] = world2[i][j];
88-
}
69+
delay(1000);
70+
seedWorld(world1);
71+
lc.clearDisplay(0);
72+
delay(300);
73+
generation = 0;
8974
}
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;
9975

10076
for (int i = 0; i < SIZEX; i++)
10177
{
@@ -106,7 +82,7 @@ void neighbours(unsigned char world1[8][8], unsigned char world2[8][8])
10682
{
10783
int left = (j + SIZEY - 1) % SIZEY;
10884
int right = (j + 1) % SIZEY;
109-
85+
int a = 0;
11086
a += world1[above][left];
11187
a += world1[above][j];
11288
a += world1[above][right];
@@ -127,53 +103,21 @@ void neighbours(unsigned char world1[8][8], unsigned char world2[8][8])
127103
}
128104
}
129105
}
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++)
106+
stable = true;
107+
for (int i = 0; i < SIZEX; i++)
140108
{
141-
for (j = 0; j < SIZEY; j++)
109+
int r = 0;
110+
for (int j = 0; j < SIZEY; j++)
142111
{
143-
if (random(100) < density); {
144-
world1[i][j] = random(2);
112+
if (world1[i][j] != world2[i][j])
113+
{
114+
stable = false;
115+
world1[i][j] = world2[i][j];
145116
}
117+
r = (r << 1) | world2[i][j];
146118
}
119+
lc.setColumn(0, i, r);
147120
}
148-
}
149-
150-
/*******************************************************************************/
151121

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);
176122
delay(delaytime);
177-
178-
179123
}

0 commit comments

Comments
 (0)