3 type GridLocation = {r: number, k: number};
5 interface GridInterface<A> {
6 get : (location: GridLocation) => A;
7 map : (f : (location: GridLocation) => A) => void;
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);
67 private print_border(): void {
68 process.stdout.write("+");
69 for (let k = 0; k < this.columns; k++) {
70 process.stdout.write("-");
72 process.stdout.write("+");
73 process.stdout.write("\n");
76 print(to_string) : void {
78 for (let r = 0; r < this.rows; r++) {
79 process.stdout.write("|");
80 for (let k = 0; k < this.columns; k++) {
81 const element = this.cells[r][k];
82 const element_string = to_string(element);
83 process.stdout.write(element_string);
85 process.stdout.write("|");
86 process.stdout.write("\n");
93 export const clear = () : void => {
94 process.stdout.write("\x1B[2J");
97 export const reset = () : void => {
98 process.stdout.write("\x1B[1;1H");
104 export type T = "Dead" | "Alive";
106 export const of_integer = (i : number) : T => {
108 { case 0 : return "Dead"
109 ; case 1 : return "Alive"
110 ; default: throw new RangeError("No known State for integer: " + i)
114 export const to_string = (t : T) : string => {
116 { case "Dead" : return " "
117 ; case "Alive": return "o"
118 ; default : throw new TypeError("Illegal member of Life.State.T: " + t)
122 export const is_alive = (t : T) : boolean => {
124 { case "Dead" : return false
125 ; case "Alive": return true
126 ; default : throw new TypeError("Illegal member of Life.State.T: " + t)
130 export const next = (t : T, neighbors_alive : number) : T => {
131 const is_cell_alive = is_alive(t);
132 if (is_cell_alive && neighbors_alive < 2) {
134 } else if (is_cell_alive && neighbors_alive < 4) {
136 } else if (is_cell_alive && neighbors_alive > 3) {
138 } else if (!is_cell_alive && neighbors_alive === 3) {
147 private grid: Grid<State.T>;
156 const init = (_) => State.of_integer(Math.round(Math.random()));
157 this.grid = new Grid({rows: rows, columns: columns, init: init});
163 const neighbor_locations = this.grid.moore_neighbors(location);
164 const neighbor_states = neighbor_locations.map((l) => this.grid.get(l));
165 const state_0 = this.grid.get(location);
166 const neighbors_alive = neighbor_states.filter(State.is_alive).length;
167 const state_1 = State.next(state_0, neighbors_alive);
174 this.grid.print(State.to_string)
179 const board_loop = (b : Life.Board) : void => {
183 setTimeout(() => board_loop(b), 250)
186 const main = () : void => {
187 const height = parseInt(process.argv[2])
188 const width = parseInt(process.argv[3])
189 const b = new Life.Board({rows: height - 3, columns: width - 2});