-let board_new = ({rows, columns} : {rows: number, columns: number}) : Board => {
- let b = [];
- for (let r = 0; r < rows; r++) {
- b[r] = [];
- for (let k = 0; k < columns; k++) {
- let zero_or_one = Math.round(Math.random());
- b[r][k] = state_of_integer(zero_or_one);
- };
- };
- return b
-};
-
-let board_neighbors = (b : Board, origin : GridLocation) : States => {
- let rows = b.length;
- let cols = b[0].length;
- let offsets =
- [ {r: -1, k: -1}, {r: -1, k: 0}, {r: -1, k: 1}
- , {r: 0, k: -1}, {r: 0, k: 1}
- , {r: 1, k: -1}, {r: 1, k: 0}, {r: 1, k: 1}
- ];
- let offset_to_location =
- (offset) => {return {r: origin.r + offset.r, k: origin.k + offset.k}};
- let locations = offsets.map(offset_to_location);
- let is_location_within_bounds =
- ({r, k}) => {
- return r >= 0 && k >= 0 && r < rows && k < cols
- };
- let locations_within_bounds = locations.filter(is_location_within_bounds);
- return locations_within_bounds.map(({r, k}) => b[r][k]);
+const board_new = ({rows, columns} : {rows: number, columns: number}) : Board => {
+ const init = (_) => state_of_integer(Math.round(Math.random()));
+ return new Grid({rows: rows, columns: columns, init: init});