summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pjm@nac.net>2011-12-13 00:34:56 (EST)
committer P. J. McDermott <pjm@nac.net>2011-12-13 00:34:56 (EST)
commitc220362d11e328c0be6d1fc1b107b08b9d2bc3bc (patch)
treebedb0b41a069aa2c0de71b1c3e1c6d08ccc5f4dd
parent001291dc21c95400c512c12afa2a786b8fa83e49 (diff)
downloadcgol-c220362d11e328c0be6d1fc1b107b08b9d2bc3bc.zip
cgol-c220362d11e328c0be6d1fc1b107b08b9d2bc3bc.tar.gz
cgol-c220362d11e328c0be6d1fc1b107b08b9d2bc3bc.tar.bz2
Use global variables and generate the seed grid.
-rw-r--r--src/cgol.c45
1 files changed, 42 insertions, 3 deletions
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