From a7a3a38c2f980aa79bd54b1233fbf5c7f932e5f6 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Tue, 13 Dec 2011 00:53:58 -0500 Subject: Cap the grid size and print the initial grid. --- diff --git a/src/cgol.c b/src/cgol.c index dbac628..b6bbd58 100644 --- a/src/cgol.c +++ b/src/cgol.c @@ -79,7 +79,6 @@ main(int argc, char **argv) init_curses(); init_game(); - mvprintw(1, 1, "Welcome to cgol!"); getch(); fini_game(); @@ -91,15 +90,22 @@ main(int argc, char **argv) void init_curses() { + int maxwidth, maxheight; + initscr(); noecho(); raw(); - - if (width == 0) { - width = COLS; + curs_set(0); + + /* Leave room for spaces between cells, but allow for one more cell on odd- + * width terminals. */ + maxwidth = (COLS - 2) / 2 + COLS % 1; + maxheight = (LINES - 2); + if (width == 0 || width > maxwidth) { + width = maxwidth; } - if (height == 0) { - height = LINES; + if (height == 0 || height > maxheight) { + height = maxheight; } } @@ -107,11 +113,12 @@ void init_game() { size_t size; - int i, n; + int i, j, n; double r; n = width * height; + /* Allocate and initialize the grids. */ size = sizeof(bool) * n; grid_cur = malloc(size); memset(grid_cur, 0, size); @@ -120,6 +127,7 @@ init_game() srand(time(NULL)); + /* Generate the seed pattern. */ for (i = 0; i < n; ++i) { r = rand() / (double) RAND_MAX; if (r > seedprob) { @@ -130,6 +138,13 @@ init_game() grid_cur[i] = true; } } + + /* Print the grid. */ + for (i = 0; i < height; ++i) { + for (j = 0; j < width; ++j) { + mvprintw(1 + i, 1 + j * 2, "%c", grid_cur[i * width + j] ? 'o' : ' '); + } + } } void -- cgit v0.9.1