aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorlemon <lsof@mailbox.org>2022-08-24 18:03:02 +0200
committerlemon <lsof@mailbox.org>2022-08-24 18:03:02 +0200
commitee1bbd0204064a20cbf89003da15151f1214fb2a (patch)
tree40dd207ab44b0a8e833414e648ed8f35ba2c0497 /examples
parenta61dd9694409ef82fd24178bc99a794df55e1f04 (diff)
i don't know how to use phi nodes sorry
Diffstat (limited to 'examples')
-rw-r--r--examples/life.c84
-rw-r--r--examples/life.cff7
2 files changed, 85 insertions, 6 deletions
diff --git a/examples/life.c b/examples/life.c
new file mode 100644
index 0000000..212ece9
--- /dev/null
+++ b/examples/life.c
@@ -0,0 +1,84 @@
+#include <stdio.h>
+
+#define W 60
+#define H 40
+
+typedef unsigned char Board[W*H/8];
+static Board board;
+
+static _Bool
+get(Board *b, unsigned x, unsigned y) {
+ x %= W; y %= H;
+ unsigned idx = x + y * W;
+ return (*b)[idx/8] & (1 << (idx % 8));
+}
+
+static void
+set(Board *b, unsigned x, unsigned y, _Bool set) {
+ x %= W; y %= H;
+ unsigned idx = x + y * W;
+ (*b)[idx/8] &= ~(1 << (idx % 8));
+ if (set)
+ (*b)[idx/8] |= (1 << (idx % 8));
+}
+
+static void
+next(Board *b) {
+ Board temp;
+ memcpy(temp, &**b, sizeof temp);
+ for (int y = 0; y < H; ++y)
+ for (int x = 0; x < W; ++x) {
+ #define C(ox, oy) get(&temp, x + ox, y + oy)
+ int n = C(-1,-1) + C(0,-1) + C(1,-1)
+ + C(-1, 0) + C(1, 0)
+ + C(-1, 1) + C(0, 1) + C(1, 1);
+
+ set(b, x, y, 0);
+ if (get(&temp, x, y))
+ set(b, x, y, n == 3);
+ else if (n == 2)
+ set(b, x, y, 1);
+ else if (n == 3)
+ set(b, x, y, 1);
+ }
+}
+
+static
+void draw(Board *b) {
+ printf("\x1B[H\n");
+ printf("\n");
+ for (int y = 0; y < H; ++y) {
+ printf(" ");
+ for (int x = 0; x < W; ++x) {
+ if (get(b, x, y)) {
+ printf("W");
+ } else {
+ printf(" ");
+ }
+ }
+ printf(" \n");
+ }
+}
+
+
+static void
+init(Board *b) {
+ int time(void *);
+ unsigned rnd = time(0);
+ for (int y = 0; y < H; ++y) {
+ for (int x = 0; x < W; ++x) {
+ set(b, x, y, (_Bool)((rnd >> 31) & 1));
+ rnd = (rnd * 134775813) + 1;
+ }
+ }
+}
+
+void usleep(unsigned);
+int main () {
+ init(&board);
+ for (;;) {
+ draw(&board);
+ usleep(100);
+ next(&board);
+ }
+}
diff --git a/examples/life.cff b/examples/life.cff
index 791916f..e9c4dce 100644
--- a/examples/life.cff
+++ b/examples/life.cff
@@ -29,12 +29,7 @@ fn next(b *Board) void {
+ C(-1, 0) + C(1, 0)
+ C(-1, 1) + C(0, 1) + C(1, 1);
- set(b, x, y, #f);
- if get(&temp, x, y) {
- set(b, x, y, n == 3);
- } else if n == 2 or n == 3 {
- set(b, x, y, #t);
- }
+ set(b, x, y, get(&temp, x, y) ? n == 3 : (n == 2 or n == 3));
}
}
}