| 1 |
#!/usr/bin/env stap |
| 2 |
# lifegame |
| 3 |
# Copyright (C) 2008 Masami Hiramatsu <[email protected]> |
| 4 |
# |
| 5 |
# This file is free software and is distributed under the terms of the GNU |
| 6 |
# General Public License (GPL); either version 2, or (at your option) any |
| 7 |
# later version. |
| 8 |
|
| 9 |
global X_MAX=15, Y_MAX=15 |
| 10 |
global map, tmp |
| 11 |
global t |
| 12 |
|
| 13 |
function showupdate() |
| 14 |
{ |
| 15 |
print("\033[1;1H\033[J") # clear screen |
| 16 |
delete map |
| 17 |
foreach([x,y] in tmp) { |
| 18 |
printf("\033[%d;%dH", y+1, x+1) # move cursor |
| 19 |
print("o") |
| 20 |
map[x,y] = 1 |
| 21 |
} |
| 22 |
delete tmp |
| 23 |
} |
| 24 |
|
| 25 |
function lookaround(x,y) |
| 26 |
{ |
| 27 |
return map[x-1,y-1] + map[x,y-1] + map[x+1,y-1] + |
| 28 |
map[x-1,y ] + map[x+1,y ] + |
| 29 |
map[x-1,y+1] + map[x,y+1] + map[x+1,y+1] |
| 30 |
} |
| 31 |
|
| 32 |
probe timer.ms(10) { |
| 33 |
t ++ |
| 34 |
if (t == Y_MAX) { |
| 35 |
showupdate() |
| 36 |
t = -1 |
| 37 |
next # skip to the next timer. |
| 38 |
} |
| 39 |
# updating just 1 line |
| 40 |
y = t |
| 41 |
for (x=0; x < X_MAX; x++) { |
| 42 |
c = lookaround(x,y) |
| 43 |
if (c == 3) tmp[x,y] = 1 |
| 44 |
else if (c == 2 && [x,y] in map) tmp[x,y] = map[x,y] |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
probe begin { |
| 49 |
tmp[6,5] = 1; tmp[5,6] = 1; tmp[4,7] = 1; tmp[5,7] = 1 |
| 50 |
tmp[6,7] = 1 |
| 51 |
showupdate() |
| 52 |
t = -1 |
| 53 |
} |
| 54 |
|