function CHAR_DEAD() {return " "}
-function get_init_cell() {
+function get_random_state() {
return int(2 * rand())
}
board = "";
for (i=1; i <= n; i++) {
- board = sprintf("%s%d", board, get_init_cell())
+ board = sprintf("%s%d", board, get_random_state())
};
return board
for (i=1; i <= x; i++) {
printf CHAR_BORDER()
};
- print;
+ print
}
}
};
- do_print_border(x);
+ do_print_border(x)
}
live_neighbors = 0;
for (direction in offsets) {
- neighbor = offsets[direction] + cell_id;
-
- # Make sure we're within limmits of the board
- if ( !(neighbor < 1) && !(neighbor > n)) {
- neighbor_state = substr(board, neighbor, 1);
- live_neighbors += neighbor_state;
- }
+ neighbor_id = offsets[direction] + cell_id;
+
+ # -----------------------------------------------------------------
+ # Real neighbors within boundaries, ghosts beyond that!
+ # -----------------------------------------------------------------
+ if ((neighbor_id >= 1) && (neighbor_id <= n)) {
+ neighbor_state = substr(board, neighbor_id, 1)
+ } else {
+ neighbor_state = get_random_state()
+ };
+
+ live_neighbors += neighbor_state
}
new_cell_state = get_new_state(cell_state, live_neighbors);
- new_board = sprintf("%s%d", new_board, new_cell_state);
+ new_board = sprintf("%s%d", new_board, new_cell_state)
};
return new_board
while (1) {
do_print_board(x, n, board);
- board = get_new_generation(x, n, board);
+ board = get_new_generation(x, n, board)
}
}