Commit a440919

mo khan <mo@mokhan.ca>
2014-10-28 02:39:59
read width from environment variable.
1 parent a78a059
Changed files (3)
src/gol.h
@@ -1,9 +1,9 @@
 static const char ALIVE='x';
 static const char DEAD=' ';
 
-extern const int WIDTH;
-extern const int HEIGHT;
-extern const int NUMBER_OF_CELLS;
+extern int WIDTH;
+extern int HEIGHT;
+extern int NUMBER_OF_CELLS;
 
 int living_neighbours_for(char* world, int index);
 char* evolve(char* world);
src/gol_test.c
@@ -3,9 +3,9 @@
 #include "gol.h"
 
 int tests_run = 0;
-const int WIDTH = 3;
-const int HEIGHT = 3;
-const int NUMBER_OF_CELLS=WIDTH*HEIGHT;
+int WIDTH = 3;
+int HEIGHT = 3;
+int NUMBER_OF_CELLS;
 
 static char* test_foo() {
   int foo = 7;
@@ -106,6 +106,8 @@ static char* all_tests() {
 }
 
 int main(int argc, char **argv) {
+  NUMBER_OF_CELLS=WIDTH*HEIGHT;
+
   char *result = all_tests();
   if (result != 0) {
     printf("FAILED: %s\n", result);
src/main.c
@@ -4,9 +4,9 @@
 #include <time.h>
 #include "gol.h"
 
-const int WIDTH = 5;
-const int HEIGHT = 5;
-const int NUMBER_OF_CELLS=WIDTH*HEIGHT;
+int WIDTH = 5;
+int HEIGHT = 5;
+int NUMBER_OF_CELLS;
 
 int random_life() {
   return rand() % 2 == 0 ? ALIVE : DEAD;
@@ -20,7 +20,17 @@ char* random_world(){
   return world;
 }
 
+int from_env(char* env_name, int default_value) {
+  char* value = getenv(env_name);
+  if(value != NULL) { return atoi(value); }
+  return default_value;
+}
+
 int main(int argc, char **argv) {
+  WIDTH=from_env("WIDTH", 5);
+  HEIGHT=from_env("HEIGHT", WIDTH);
+  NUMBER_OF_CELLS=WIDTH*HEIGHT;
+
   srand(time(NULL));
   system("clear");