-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlife.awk
More file actions
executable file
·37 lines (34 loc) · 844 Bytes
/
life.awk
File metadata and controls
executable file
·37 lines (34 loc) · 844 Bytes
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
#!/usr/bin/awk -f
function neighbours(TABLE, x, y) {
return TABLE[x-1, y-1] + TABLE[x, y-1] + TABLE[x+1, y-1] + \
TABLE[x-1, y] + TABLE[x+1, y] + \
TABLE[x-1, y+1] + TABLE[x, y+1] + TABLE[x+1, y+1]
}
function calculate(OLD, NEW) {
for (x=1; x<=width; x++)
for(y=1; y<=height; y++)
if (OLD[x, y])
NEW[x, y] = (int(neighbours(OLD, x, y)/2)==1) ? 1 : 0
else
NEW[x, y] = (neighbours(OLD, x, y)==3) ? 1 : 0
}
function print_table(TABLE) {
for (y=1; y<=height; y++) {
for (x=1; x<=width; x++)
printf TABLE[x, y] ? "[]" : " "
print
}
printf "\033[" height "A"
}
BEGIN {
width = 39
height = 22
srand()
for (x=1; x<=width; x++)
for (y=1; y<=height; y++)
T1[x, y] = rand()<0.5 ? 1 : 0
while (1) {
print_table(T1); calculate(T1, T2)
print_table(T2); calculate(T2, T1)
}
}