4 function CHAR_BORDER
() {return "-"}
5 function CHAR_ALIVE
() {return "o"}
6 function CHAR_DEAD
() {return " "}
10 return int
(2 * rand
())
14 function init_board
(n
) {
17 for (i=
1; i
<= n
; i
++) {
18 board =
sprintf("%s%d", board
, init_cell
())
25 function print_border
(x
) {
26 for (i=
1; i
<= x
; i
++) {
33 function char_of_state
(state
) {
36 } else if (state ==
0) {
42 function print_board
(x
, n
, board
) {
45 for (i=
1; i
<= n
; i
++) {
46 printf "%s", char_of_state
(substr(board
, i
, 1));
57 function 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 ensure_negative
(n
) {
81 function new_generation
(x
, n
, board
) {
82 offsets
["N" ] = ensure_negative
(x
);
83 offsets
["NE"] = ensure_negative
(x
- 1);
85 offsets
["SE"] = x
+ 1 ;
87 offsets
["SW"] = x
- 1 ;
89 offsets
["NW"] = ensure_negative
(x
+ 1);
93 for (cell_id=
1; cell_id
<= n
; cell_id
++) {
94 cell_state =
substr(board
, cell_id
, 1);
97 for (direction in offsets
) {
98 neighbor = offsets
[direction
] + cell_id
;
100 # Make sure we're within limmits of the board
101 if ( !
(neighbor
< 1) && !
(neighbor
> n
)) {
102 neighbor_state =
substr(board
, neighbor
, 1);
103 live_neighbors
+= neighbor_state
;
107 new_cell_state = new_state
(cell_state
, live_neighbors
);
108 new_board =
sprintf("%s%d", new_board
, new_cell_state
);
116 "stty size" | getline stty_size_out
;
117 split(stty_size_out
, stty_size
);
120 y = stty_size
[1] - 3; # Minus 1 char for each: border, border, cursor
123 board = init_board
(n
);
126 print_board
(x
, n
, board
);
127 board = new_generation
(x
, n
, board
);