forked from bext-lang/b
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlangtons_ants.b
More file actions
94 lines (82 loc) · 1.32 KB
/
langtons_ants.b
File metadata and controls
94 lines (82 loc) · 1.32 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* https://en.wikipedia.org/wiki/Langton%27s_ant */
width;
height;
board;
x;
y;
r;
mod(n, b) return ((n%b + b)%b);
get(b, xn, yn) {
xn = mod(xn, width);
yn = mod(yn, height);
return (b[xn+yn*width]);
}
set(b, xn, yn, v) {
xn = mod(xn, width);
yn = mod(yn, height);
b[xn+yn*width] = v;
}
print() {
extrn printf;
auto xn, yn;
xn = 0; while (xn <= width) {
printf("##");
xn += 1;
}
printf("\n");
yn = 0; while (yn < height) {
printf("#");
xn = 0; while (xn < width) {
if ((xn == mod(x, width)) & (yn == mod(y, height))) {
printf("▒▒");
} else {
if (get(board, xn,yn)) {
printf("██");
} else {
printf(" ");
}
}
xn += 1;
}
printf("#\n");
yn += 1;
}
xn = 0; while (xn <= width) {
printf("##");
xn += 1;
}
printf("\n");
}
step() {
extrn printf;
auto c;
c = get(board, x, y);
if (c) r++; else r--;
set(board, x, y, !c);
switch mod(r, 4) {
case 0: y++; goto out;
case 1: x++; goto out;
case 2: y--; goto out;
case 3: x--; goto out;
}
out:
}
main() {
extrn malloc, memset, printf, usleep;
auto size;
width = 25;
height = 15;
size = width*height*(&0[1]);
board = malloc(size);
memset(board, 0, size);
r = 0;
x = 15;
y = 7;
while (1) {
print();
step();
printf("%c[%dA", 27, height+2);
/* TODO: does not work on Windows */
usleep(50000);
}
}