diff options
author | P. J. McDermott <pjm@nac.net> | 2011-12-13 00:53:58 (EST) |
---|---|---|
committer | P. J. McDermott <pjm@nac.net> | 2011-12-13 00:53:58 (EST) |
commit | a7a3a38c2f980aa79bd54b1233fbf5c7f932e5f6 (patch) | |
tree | e013b34a075486116b9b7b9838d9795d3c2e353c | |
parent | c220362d11e328c0be6d1fc1b107b08b9d2bc3bc (diff) | |
download | cgol-a7a3a38c2f980aa79bd54b1233fbf5c7f932e5f6.zip cgol-a7a3a38c2f980aa79bd54b1233fbf5c7f932e5f6.tar.gz cgol-a7a3a38c2f980aa79bd54b1233fbf5c7f932e5f6.tar.bz2 |
Cap the grid size and print the initial grid.
-rw-r--r-- | src/cgol.c | 29 |
1 files changed, 22 insertions, 7 deletions
@@ -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 |