-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolscript.babel
366 lines (334 loc) · 9.13 KB
/
golscript.babel
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
var playback={
"paused":"no",
"anim": null,
"restart":"no"
};
var Button =React.createClass({
render: function(){
var iconClass;
switch(this.props.id){
case "play" : iconClass= "ion-play"; break;
case "pause" : iconClass= "ion-pause"; break;
case "clear" : iconClass= "ion-android-delete"; break;
case "random" : iconClass= "ion-shuffle"; break;
case "grid" : iconClass= "ion-grid"; break;
case "fade" : iconClass= "ion-star"; break;
}
return(
<button {...this.props} className="panel-button" title={this.props.id.toUpperCase()}>
<i className={iconClass} id={this.props.id}/>
</button>
)
}
});
var Panel= React.createClass({
render: function(){
var list= ["play", "pause", "clear", "random", "grid", "fade"];
var gen= this.props.gen;
return(
<div {...this.props} className="control-panel">
<div className="timer" >
<span className="timerDescription">Generation: </span>
<span className="timerNumber">{gen}</span>
<div className="ConwayName"><span className="actualtitle"> Conway's Game Of Life : React.js</span></div>
</div>
{
list.map(function(data){
return(
<Button id={data}>
</Button>
)
})
}
</div>
)
}
});
var Cell= React.createClass({
render: function(){
//color stuff
//var arr = ["#96F751", "#16DB93", "#EFEA5A", "#F29E4C"],
var ind = +this.props.id.slice(4),
colors = {
"background":"hsl("+ (50+Math.floor((Math.floor(ind/40)+ (ind%60))*1.5))+", 80%, 70%)"
};
return(
<div {...this.props} style={ this.props.randomness=="on" ? ( this.props.alive == "alive" ? colors : null) : null}>
</div>
)
}
});
var GameBox= React.createClass({
//create random cells in list
getInitialState: function(){
return({
list: this.createCells(),
grid: "off",
fade: "off"
})
},
//initial rows and columns
getDefaultProps: function(){
return({
rows: 40,
cols: 60,
})
},
//for converting cartesian coordinates
//to the relevant index in cell array
Num: function(r, c){
return (r*this.props.cols + c)
},
//returns number of live neighbors of
//a cell at [row, col] in the grid
checkalive: function(row, col){
var cellarr = this.state.list.slice(),
_cols = this.props.cols,
_rows = this.props.rows,
ind = this.Num(row, col),
top = ind-_cols,
bottom = ind+_cols,
S = ind+_cols,
N = ind-_cols,
W = ind-1,
E = ind+1,
NW = top-1,
NE = top+1,
SW = bottom-1,
SE = bottom+1,
neighbors = [ ];
//corner cases--literally
//top row
if(row===0){
top = ind+((_rows-1)*_cols),
N = top,
NE = top+1,
NW = top-1;
//top-left corner
if(col===0){
SW = _rows+_cols-1,
W = (_cols-1),
NW = (_rows*_cols)-1;
}
//top-right corner
else if(col==_cols-1){
SE = ind+1,
E = 0,
NE = (_rows-1)*_cols;
}
}
//bottom row
else if(row ==_rows-1){
S = ind - ((_rows-1)*_cols),
SW = S-1,
SE = S+1;
//bottom-left corner
if(col===0){
SW = (_cols-1),
W = (_rows*_cols) -1,
NW = (ind-1);
}
//bottom-right corner
else if(col==_cols-1){
E = ind-_cols+1,
NE = ind-2*_cols+1,
SE = 0;
}
}
//all cells on left edge except those on bottom
//and top
else if((row!==0 && row!=_rows) &&col===0){
W = ind + _cols - 1,
SW = ind + 2*_cols-1,
NW = ind - 1;
}
//all cells on right edge except those on
//bottom and top
else if((row!==0 && row!=_rows) && col==_cols-1){
E = ind - _cols + 1,
NE = ind - 2*_cols + 1,
SE = ind + 1;
}
//get indices of all 8 neighbors
neighbors.push(N, NW, NE, S, SW, SE, W, E);
var countlive=0;
for(var jj in neighbors){
if(cellarr[neighbors[jj]]=="alive"){
countlive++;
}
}
return countlive;
},
//will only be called once, at the beginning
//creates random cells
createCells: function(option){
var choice;
var arr=[];
for(var i=0; i<this.props.rows; i++){
for(var j=0; j<this.props.cols; j++){
if(option=="dead" || option=="alive"){
choice = option;
}
else{
choice= Math.floor(Math.random()*10) == 1? "alive": "dead";
}
arr.push(choice);
}
}
return arr;
},
//this is the main animation function
//it changes every time props are recieved
//the time prop changes every X seconds and
//thus the animation is started
componentWillReceiveProps: function(){
var ls = this.state.list.slice(),
deadcnt = 0;
for(var r=0; r<this.props.rows; r++){
for(var c=0; c<this.props.cols; c++){
var ind = this.checkalive(r,c),
actind = this.Num(r,c);
status = ls[actind];
//Conway's Game of Life:
//A dead cell becomes alive iff it has 3 live neighbors
if(status=="dead"){
if(ind==3) ls[actind]="alive";
}
//a live cell dies when surrounded by less than 2 neighbors
//or more than 3 neighbors
else{
if(ind<2) ls[actind] ="dead";
else if(ind>3) ls[actind]="dead";
}
if(ls[actind]=="dead") deadcnt++;
}
}
//change the state
//this causes us to have only one state component
//all cells are stateless
if(deadcnt == this.props.rows*this.props.cols) playback.paused="yes",playback.restart="yes";
this.setState(function(){
return(
{list:ls}
);
})
},
handleClick: function(e){
//panel options
switch(e.target.id){
case "play":
if(playback.paused=="yes"){
playback.paused="no";
window.requestAnimationFrame(playback.anim);
}
return;
case "pause":
if(playback.paused=="no"){
playback.paused="yes";
}
return;
case "clear":
playback.paused="yes";
playback.restart="yes";
this.setState(function(){
return({
list: this.createCells("dead")
})
});
return;
case "random":
playback.restart="yes";
this.setState(function(){
return({list: this.createCells()});
});
return;
case "grid":
this.setState(function(){
if(this.state.grid=="off")
{
var ans= "on";
document.getElementById("grid").classList.add("activebutt");
}
else {
ans= "off";
document.getElementById("grid").classList.remove("activebutt");
}
return ({grid: ans})
})
return;
case "fade":
if(this.state.fade=="off")
{
var fadeval="on";
document.getElementById("fade").classList.add("activebutt");
}
else {
fadeval="off";
document.getElementById("fade").classList.remove("activebutt");
}
this.setState(function(){
return({fade:fadeval})
});
return;
}
//for clicking cells
var ls = e.target.classList;
var cop= this.state.list.slice();
var ind= +e.target.id.slice(4);
for(var i in ls){
if(ls[i]=="cell-alive"){
e.target.classList.remove("cell-alive");
e.target.classList.add("cell-dead");
cop[ind]="dead";
break;
}
else if(ls[i]=="cell-dead"){
e.target.classList.remove("cell-dead");
e.target.classList.add("cell-alive");
cop[ind]="alive";
break;
}
}
this.setState(function(){
return({
list: cop,})
});
},
//render the cells
render: function(){
var rows = this.props.rows,
cols = this.props.cols,
gridval = this.state.grid,
fadeval = this.state.fade,
over = {
overflow : gridval=="off"? "visible" : "hidden"
};
return(
<div className={this.props.className} onClick={this.handleClick} style={over}>
<Panel id="panel" gen = {playback.restart=="no"? this.props.generation: 0}/>
{this.state.list.map(function(data, index){
return(
<Cell className={"cell cell-"+data+" grid-"+gridval+" fade-"+fadeval} id={"val-"+index} key={index} randomness={gridval} alive={data}/>
)
})}
</div>
)
}
});
(function(){
//time: to keep the animation going and get the generation
var time=0;
playback.anim = function (){
if(playback.restart=="yes"){
time=0;
playback.restart="no";
}
ReactDOM.render(
<GameBox className="outerbox" generation={time}/> , document.getElementById("Board"));
if(playback.paused=="no"){
window.requestAnimationFrame(playback.anim);
}
time+=1;
}
window.requestAnimationFrame(playback.anim);
}).call(this);