3 type GridLocation = {r: number, k: number};
5 interface GridInterface<A> {
6 get : (location: GridLocation) => A;
7 map : (f : (location: GridLocation) => A) => Grid<A>;
8 moore_neighbors: (origin : GridLocation) => Array<GridLocation>;
9 print : (toString : (A: A) => string) => void;
12 class Grid<A> implements GridInterface<A> {
13 private rows : number;
14 private columns : number;
15 private cells : Array<Array<A>>;
18 {rows, columns, init} :
21 , init : (location: GridLocation) => A
26 this.columns = columns;
28 for (let r = 0; r < rows; r++) {
30 for (let k = 0; k < columns; k++) {
31 cells[r][k] = init({r: r, k: k})
37 get({r, k}: GridLocation) : A {
38 return this.cells[r][k]
41 moore_neighbors(origin : GridLocation) : Array<GridLocation> {
43 [ {r: -1, k: -1}, {r: -1, k: 0}, {r: -1, k: 1}
44 , {r: 0, k: -1}, {r: 0, k: 1}
45 , {r: 1, k: -1}, {r: 1, k: 0}, {r: 1, k: 1}
47 const offset_to_location =
48 (offset) => {return {r: origin.r + offset.r, k: origin.k + offset.k}};
49 const locations = offsets.map(offset_to_location);
50 const is_location_within_bounds =
51 ({r, k}) => r >= 0 && k >= 0 && r < this.rows && k < this.columns;
52 return locations.filter(is_location_within_bounds)
55 map(f : (location: GridLocation) => A) {
57 for (let r = 0; r < this.rows; r++) {
59 for (let k = 0; k < this.columns; k++) {
60 const location = {r: r, k: k};
61 cells[r][k] = f(location);
64 const init = ({r, k}) => cells[r][k];
65 const grid = new Grid({rows: this.rows, columns: this.columns, init: init});
69 private print_border(): void {
70 process.stdout.write("+");
71 for (let k = 0; k < this.columns; k++) {
72 process.stdout.write("-");
74 process.stdout.write("+");
75 process.stdout.write("\n");
78 print(to_string) : void {
80 for (let r = 0; r < this.rows; r++) {
81 process.stdout.write("|");
82 for (let k = 0; k < this.columns; k++) {
83 const element = this.cells[r][k];
84 const element_string = to_string(element);
85 process.stdout.write(element_string);
87 process.stdout.write("|");
88 process.stdout.write("\n");
95 export const clear = () : void => {
96 process.stdout.write("\x1B[2J");
99 export const reset = () : void => {
100 process.stdout.write("\x1B[1;1H");
106 export type T = "Dead" | "Alive";
108 export const of_integer = (i : number) : T => {
110 { case 0 : return "Dead"
111 ; case 1 : return "Alive"
112 ; default: throw new RangeError("No known State for integer: " + i)
116 export const to_string = (t : T) : string => {
118 { case "Dead" : return " "
119 ; case "Alive": return "o"
120 ; default : throw new TypeError("Illegal member of Life.State.T: " + t)
124 export const is_alive = (t : T) : boolean => {
126 { case "Dead" : return false
127 ; case "Alive": return true
128 ; default : throw new TypeError("Illegal member of Life.State.T: " + t)
132 export const next = (t : T, neighbors_alive : number) : T => {
133 const is_cell_alive = is_alive(t);
134 if (is_cell_alive && neighbors_alive < 2) {
136 } else if (is_cell_alive && neighbors_alive < 4) {
138 } else if (is_cell_alive && neighbors_alive > 3) {
140 } else if (!is_cell_alive && neighbors_alive === 3) {
149 private grid: Grid<State.T>;
158 const init = (_) => State.of_integer(Math.round(Math.random()));
159 this.grid = new Grid({rows: rows, columns: columns, init: init});
163 const grid = this.grid.map(
165 const neighbor_locations = this.grid.moore_neighbors(location);
166 const neighbor_states = neighbor_locations.map((l) => this.grid.get(l));
167 const state_0 = this.grid.get(location);
168 const neighbors_alive = neighbor_states.filter(State.is_alive).length;
169 const state_1 = State.next(state_0, neighbors_alive);
177 this.grid.print(State.to_string)
182 const board_loop = (b : Life.Board) : void => {
186 setTimeout(() => board_loop(b), 250)
189 const main = () : void => {
190 const height = parseInt(process.argv[2])
191 const width = parseInt(process.argv[3])
192 const b = new Life.Board({rows: height - 3, columns: width - 2});