-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.ts
144 lines (128 loc) · 3.87 KB
/
grid.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
namespace Jason {
export enum Direction {
Left = 0, Right = 4, Up = 6, Down = 2,
UpLeft = 7, UpRight = 5, DownLeft = 1, DownRight = 3
}
/**
* Get an array of each direction.
*/
export function directions() {
return [Direction.Left, Direction.Right, Direction.Up, Direction.Down,
Direction.UpLeft, Direction.UpRight, Direction.DownLeft, Direction.DownRight];
};
export function notNull<T>(val: T | null | undefined): val is T {
return val !== null && val !==undefined;
}
/**
* An immutable, 2-D, rectangular grid container.
*
* The grid is 0-indexed, with the top left corner being (0,0) and the
* bottom-right corner being (length-1, width-1)
*/
export class Grid<T> {
// Store a list of rows.
private grid: T[][] = [];
/**
* Create a new grid based on a 2-D array of data elements.
* The arrays will be checked for rectangularity.
*/
constructor(grid: T[][]) {
let width = grid[0].length;
for(let y = 0; y < grid.length; y++) {
if(grid[y].length != width)
throw "Non-rectangular grid";
}
this.grid = grid;
}
/**
* Get the width of the grid.
*/
width() {
return this.grid[0].length;
}
/**
* Get the height of the grid.
*/
height() {
return this.grid.length;
}
/**
* Get whether an x/y coordinate is in the grid.
*/
inGrid(x: number, y: number) {
return x < this.width() && x >= 0 && y < this.height() && y >= 0;
}
/**
* Get the value stored at a point in the grid. Returns null if out of grid.
*
* The grid is 0-indexed, with the top left corner being (0,0) and the
* bottom-right corner being (length-1, width-1)
*/
at(x: number, y: number): T | null {
if(this.inGrid(x,y))
return this.grid[y][x];
else
return null;
}
/**
* Get the value and coordinates of a cell in the given direction of the coords.
* Returns null if out of grid.
*/
toThe(dir: Direction, x: number, y: number): [T,number,number] | null {
let x2 = x;
let y2 = y;
switch(dir) {
case Direction.Left: x2 -= 1; break;
case Direction.Right: x2 += 1; break;
case Direction.Up: y2 -= 1; break;
case Direction.Down: y2 += 1; break;
case Direction.DownLeft: y2 += 1; x2 -=1; break;
case Direction.DownRight: y2 += 1; x2 +=1; break;
case Direction.UpLeft: y2 -= 1; x2 -=1; break;
case Direction.UpRight: y2 -= 1; x2 +=1; break;
}
if(this.inGrid(x2,y2))
return [this.grid[y2][x2],x2,y2];
else
return null;
}
/**
* Get an array of the cell in each direction from the point.
* Will not include off-of-grid coordinates.
*/
neighbors(x: number, y: number): Array<[T,number,number]> {
// Note: The filter call at the end will drop the null values.
return directions().map(dir => this.toThe(dir,x,y)).filter(notNull)
}
/**
* Get a cloned copy of the grid.
*/
gridCopy(): T[][] {
return this.grid.slice().map( (row) => row.slice() );
}
/**
* Execute a side-effectful function for each member of the grid.
* Executes in a left-to-right first, top-to-bottom second order.
*/
each(fun: (x: number, y: number, val: T) => void): void {
this.grid.forEach( (row,y) => {
row.forEach( (val,x) => fun(x,y,val) )
}
);
}
/**
* Apply a function to each cell in the grid, getting a [[T]] value back.
*/
map(fun: (x: number, y: number, val: T) => T): T[][] {
return this.grid.map( (row,y) => row.map( (val,x) => fun(x,y,val) ) );
}
/**
* Return a copy of the grid with a value changed.
*/
alter(x: number, y: number, val: T): Grid<T> {
let newGrid = this.gridCopy();
newGrid[y][x] = val;
return new Grid(newGrid);
}
}
}