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
2725long density = 35 ; // Sets density % during seeding
2826unsigned long delaytime = 150 ; // Sets the time each generation is shown
2927int generation = 0 ; // Counter for re-seeding
3028int 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 ];
4232unsigned 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