From c220362d11e328c0be6d1fc1b107b08b9d2bc3bc Mon Sep 17 00:00:00 2001
From: P. J. McDermott <pjm@nac.net>
Date: Tue, 13 Dec 2011 00:34:56 -0500
Subject: Use global variables and generate the seed grid.

---
(limited to 'src')

diff --git a/src/cgol.c b/src/cgol.c
index 2d4ffe2..dbac628 100644
--- a/src/cgol.c
+++ b/src/cgol.c
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
+#include <time.h>
 #include <unistd.h>
 #include <curses.h>
 
@@ -30,7 +31,9 @@ void init_game();
 void fini_game();
 void fini_curses();
 
-static int width, height, seedprob, genrate, gens;
+static int width, height;
+static double seedprob;
+static int genrate, gens;
 static bool *grid_cur, *grid_next;
 
 int
@@ -38,6 +41,12 @@ main(int argc, char **argv)
 {
 	int opt;
 
+	width = 0;
+	height = 0;
+	seedprob = 0.5;
+	genrate = 1; /* TODO: Units. */
+	gens = 0;
+
 	while ((opt = getopt(argc, argv, "w:h:s:r:n:")) != -1) {
 		switch (opt) {
 			case 'w':
@@ -47,7 +56,7 @@ main(int argc, char **argv)
 				height = atoi(optarg);
 				break;
 			case 's':
-				seedprob = atoi(optarg);
+				seedprob = atof(optarg);
 				break;
 			case 'r':
 				genrate = atoi(optarg);
@@ -61,6 +70,12 @@ main(int argc, char **argv)
 		}
 	}
 
+	/* Sanity.  Let's check it. */
+	if (seedprob <= 0 || seedprob > 1) {
+		fprintf(stderr, "Error: Ensure 0 < SEEDPROB <= 1\n");
+		return 2;
+	}
+
 	init_curses();
 	init_game();
 
@@ -79,18 +94,42 @@ init_curses()
 	initscr();
 	noecho();
 	raw();
+
+	if (width == 0) {
+		width = COLS;
+	}
+	if (height == 0) {
+		height = LINES;
+	}
 }
 
 void
 init_game()
 {
 	size_t size;
+	int i, n;
+	double r;
 
-	size = sizeof(bool) * width * height;
+	n = width * height;
+
+	size = sizeof(bool) * n;
 	grid_cur = malloc(size);
 	memset(grid_cur, 0, size);
 	grid_next = malloc(size);
 	memset(grid_next, 0, size);
+
+	srand(time(NULL));
+
+	for (i = 0; i < n; ++i) {
+		r = rand() / (double) RAND_MAX;
+		if (r > seedprob) {
+			/* Dead. */
+			grid_cur[i] = false;
+		} else {
+			/* Alive. */
+			grid_cur[i] = true;
+		}
+	}
 }
 
 void
--
cgit v0.9.1