@@ 1,24 1,24 @@
+#define _GNU_SOURCE // for nanosleep..
#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
-#include <unistd.h> /* usleep */
#define TAIL /* tail call (declaration of intent) */
#define W 64
#define H 32
static char board[H][W], boardtmp[H][W];
-static int gen;
+static unsigned gen;
#define neighbours(b,y,x) \
( b[(y-1)%H][(x-1)%W] + b[(y-1)%H][(x)%W] + b[(y-1)%H][(x+1)%W] \
+ b[(y+0)%H][(x-1)%W] + b[(y+0)%H][(x+1)%W] \
+ b[(y+1)%H][(x-1)%W] + b[(y+1)%H][(x)%W] + b[(y+1)%H][(x+1)%W] ) \
-void life(int i, int y, int x, int n, char *);
-void life(int i, int y, int x, int n, char _[(
+static void life(size_t i, size_t y, size_t x, size_t n, char *);
+static void life(size_t i, size_t y, size_t x, size_t n, char _[(
y = i/W, x = i%W,
n = neighbours(board, y, x),
boardtmp[y][x] = (board[y][x] ? (n == 2 || n == 3) : (n == 3)),
@@ 28,21 28,21 @@ void life(int i, int y, int x, int n, char _[(
memcpy(board, boardtmp, sizeof board),
1)]) {}
-void randini(int i, char *);
-void randini(int i, char _[(
+static void randini(size_t i, char *);
+static void randini(size_t i, char _[(
board[i/W][i%W] = rand() < RAND_MAX / 2,
(++i < W*H) ?
TAIL randini(i, "")
: (void)0,
1)]) {}
-void clear(char _[(
+static void clear(char _[(
printf("%s", "\x1b[2J"),
fflush(stdout),
1)]) {}
-void draw(int i, char *);
-void draw(int i, char _[(
+static void draw(size_t i, char *);
+static void draw(size_t i, char _[(
(i && i%W == 0) ? putchar('\n') : (void)0,
putchar(board[i/W][i%W] ? '#' : ' '),
(++i < W*H) ?
@@ 50,15 50,15 @@ void draw(int i, char _[(
: putchar('\n'),
1)]) {}
-void loop(char *);
-void loop(char _[(
+static void loop(char *);
+static void loop(char _[(
clear(""),
- printf("Generation %d\n", gen++),
+ printf("Generation %u\n", gen++),
draw(0, ""),
life(0, 0,0,0,""),
puts("Enter: step ^D: auto ^C: quit"),
getchar() == EOF ?
- usleep(30000)
+ nanosleep(&(struct timespec){ .tv_nsec = 30000000 }, NULL)
: (void)0,
TAIL loop(""),
1)]) {}