/
gl.js
190 lines (156 loc) · 4.31 KB
/
gl.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var Gl = exports;
var vodevil = require('vodevil');
Gl.cell = function () {
return { live: false };
};
Gl.grid = function ( lines, cols ) {
var grid = new Array( lines * cols );
// Insert cells in undefined slots
grid = vodevil.intersect(
grid,
function ( slot ) {
return Gl.cell();
}
);
return grid;
};
Gl.loneliness = function ( neighbors, cell ) {
neighbors = vodevil.intersect(
neighbors,
function ( neighbor ) {
if ( neighbor['live'] ) {
return neighbor;
}
}
);
if ( cell['live'] && neighbors.length < 2 ) {
cell['live'] = !cell['live'];
}
return cell;
};
Gl.overpopulation = function ( neighbors, cell ) {
neighbors = vodevil.intersect(
neighbors,
function ( neighbor ) {
if ( neighbor['live'] ) {
return neighbor;
}
}
);
if ( cell['live'] && neighbors.length > 3 ) {
cell['live'] = !cell['live'];
}
return cell;
};
Gl.neighborsAccurate = function ( neighbors, cell ) {
neighbors = vodevil.intersect(
neighbors,
function ( neighbor ) {
if ( neighbor['live'] ) {
return neighbor;
}
}
);
if ( !cell['live'] && neighbors.length === 3 ) {
cell['live'] = !cell['live'];
}
return cell;
};
Gl.remainState = function ( neighbors, cell ) {
neighbors = vodevil.intersect(
neighbors,
function ( neighbor ) {
if ( neighbor['live'] ) {
return neighbor['live'];
}
}
);
if ( neighbors.length === 2 || neighbors.length === 3 ) {
return cell;
}
return {};
};
Gl.filter = function ( grid, lines ) {
var _grid = {};
if ( ( grid && lines ) && lines <= grid.length ) {
// Line manipulation
var _lineCounter = 0,
_gridLines = [],
_lines = lines,
line = [];
// Get lines
for ( ;; ) {
if ( _lineCounter === grid.length ) {
break;
} else {
line.push( grid[ _lineCounter ] );
_lines -= 1;
if ( _lines === 0 ) {
_gridLines.push( line );
_lines = lines;
line = [];
}
}
_lineCounter += 1;
}
var _colCounter = 0,
_gridCols = [],
_cols = _gridLines.length,
col = [];
// Get cols
for ( ;; ) {
if ( _colCounter === _gridLines[0].length ) {
break;
} else {
for ( var c = 0; c < _cols; c++ ) {
col.push( _gridLines[ c ][ _colCounter ] );
}
_gridCols.push( col );
col = [];
}
_colCounter += 1;
}
_grid = { lines: _gridLines, cols: _gridCols };
}
return _grid;
};
Gl.aroundCell = function ( grid, index, lines ) {
var around = [],
_grid = Gl.filter( grid, lines );
// col sets
var _colsMin = ( index[1] - 1 ),
_colsMax = ( index[1] + 1 ),
// line sets
_linesMin = ( index[0] - 1 ),
_linesMax = ( index[0] + 1 );
around = vodevil.intersect(
_grid['lines'],
function ( line, lid ) {
if (
lid >= _linesMin &&
lid <= _linesMax
) {
var selection = vodevil.intersect(
line,
function ( col, cid ) {
if (
cid >= _colsMin &&
cid <= _colsMax
) {
if (
!(
index[0] === lid &&
index[1] === cid
)
) {
return col;
}
}
}
);
return selection;
}
}
);
return around;
};