4 function CHAR_BORDER
() {return "-"}
5 function CHAR_ALIVE
() {return "o"}
6 function CHAR_DEAD
() {return " "}
9 function get_random_state
() {
10 return int
(2 * rand
())
14 function get_init_board
(n
) {
17 for (i=
1; i
<= n
; i
++) {
18 board =
sprintf("%s%d", board
, get_random_state
())
25 function get_char_of_state
(state
) {
28 } else if (state ==
0) {
34 function do_print_border
(x
) {
35 for (i=
1; i
<= x
; i
++) {
42 function do_print_board
(x
, n
, board
) {
45 for (i=
1; i
<= n
; i
++) {
46 printf "%s", get_char_of_state
(substr(board
, i
, 1));
57 function get_new_state
(state
, live_neighbors
) {
58 if (state ==
1 && live_neighbors
< 2) {
60 } else if (state ==
1 && live_neighbors
< 4) {
62 } else if (state ==
1 && live_neighbors
> 3) {
64 } else if (state ==
0 && live_neighbors ==
3) {
72 function get_new_generation
(x
, n
, board
) {
74 offsets
["NE"] =
-(x
- 1);
76 offsets
["SE"] = x
+ 1 ;
78 offsets
["SW"] = x
- 1 ;
80 offsets
["NW"] =
-(x
+ 1);
84 for (cell_id=
1; cell_id
<= n
; cell_id
++) {
85 cell_state =
substr(board
, cell_id
, 1);
88 for (direction in offsets
) {
89 neighbor_id = offsets
[direction
] + cell_id
;
91 # -----------------------------------------------------------------
92 # Real neighbors within boundaries, ghosts beyond that!
93 # -----------------------------------------------------------------
94 if ((neighbor_id
>=
1) && (neighbor_id
<= n
)) {
95 neighbor_state =
substr(board
, neighbor_id
, 1)
97 neighbor_state = get_random_state
()
100 live_neighbors
+= neighbor_state
103 new_cell_state = get_new_state
(cell_state
, live_neighbors
);
104 new_board =
sprintf("%s%d", new_board
, new_cell_state
)
112 "stty size" | getline stty_size_out
;
113 split(stty_size_out
, stty_size
);
116 y = stty_size
[1] - 3; # Minus 1 char for each: border, border, cursor
119 board = get_init_board
(n
);
122 do_print_board
(x
, n
, board
);
123 board = get_new_generation
(x
, n
, board
)