1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
import "libc.hff";
def W = 60, H = 40;
typedef Board [(W * H)/8]u8;
static board Board = {};
fn get(b *Board, x uint, y uint) bool {
x %= W; y %= H;
let idx = x + (y * W);
return (*b)[idx / 8] & (1 << (idx % 8)) != 0;
}
fn set(b *Board, x uint, y uint, set bool) void {
x %= W; y %= H;
let idx = x + (y * W);
(*b)[idx / 8] &= ~(1 << (idx % 8));
if set {
(*b)[idx / 8] |= (1 << (idx % 8));
}
}
fn next(b *Board) void {
let temp Board = *b;
for let y = 0; y < H; ++y {
for let x = 0; x < W; ++x {
defmacro C(ox,oy) [ (as(int)get(&temp, x + ox, y + oy)) ]
let 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, get(&temp, x, y) ? n == 3 : n == 2 or n == 3);
}
}
}
fn draw(b *Board) void {
printf("\x1B[H\n");
printf("\n");
for let y = 0; y < H; ++y {
printf(" ");
for let x = 0; x < W; ++x {
if get(b, x, y) {
printf("W");
} else {
printf(" ");
}
}
printf(" \n");
}
}
extern fn time(*void) int;
fn init(b *Board) void {
let rnd u32 = time(#null);
for let y = 0; y < H; ++y {
for let x = 0; x < W; ++x {
set(b, x, y, as(bool)((rnd >> 31) & 1));
rnd = (rnd * 134775813) + 1;
}
}
}
extern fn usleep(u32) void;
extern fn main() void {
init(&board);
do {
draw(&board);
usleep(100);
next(&board);
} while #t;
}
|