Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
740 lines (691 loc) · 23.8 KB
/
Copy pathApp.js
File metadata and controls
740 lines (691 loc) · 23.8 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
import React, { Component } from "react";
import "./App.css";
class App extends Component {
state = {
matrix: {}, //using this for the grid.
//grid set up
matrixMirror: {}, //using for clearing the grid will be set to a all dead cell grid.
matrixUsing: [], //using this to iterate over and show the grid on the screen.
mirror: [], // mirror for clearing the grid.
row_count: 15, //default amount of rows on the grid can be changed.
col_count: 15, //default amount of columns on the grid can be changed.
width: "330px", //width of the grid can be changed.
//presets
//cell choices
if_one_color: "black", // if one the cell is alive
if_zero_color: "white", // if zero the cell is dead
//game instructions
gameRunning: false, //used to show game has started.
generation: 0, // keeps count of generations
gameSpeed: 125 // default game speed can be changed.
};
componentWillMount() {
this.setMatrixUp();
}
reset = () => {
//resets gameRunning and generation count.
this.setState({ generation: 0, gameRunning: false });
// this.updateRowCol();
this.setMatrixUp();
};
generateRandom = () => {
//generates a random grid to start with.
const keys = Object.keys(this.state.matrix);
const matrix = { ...this.state.matrixMirror }; //creates a copy
for (let i = 0; i < keys.length; i++) {
for (let j = 0; j < matrix[i].length; j++) {
const grabRandom = Math.random();
const convertToNumber = grabRandom * 2;
const floorNumber = Math.floor(convertToNumber);
if (floorNumber === 1) {
matrix[i][j] = 1;
} else {
matrix[i][j] = 0;
}
}
}
this.setState({ matrix });
this.continueWithGame();
};
setMatrixUp = () => {
const matrixUsing = [];
let count = 0;
let beginRow = 0;
let beginColumn = 0;
const matrix = {};
while (beginRow < this.state.row_count) {
beginColumn = 0;
matrix[beginRow] = [];
while (beginColumn < this.state.col_count) {
matrix[beginRow].push(0);
// beginColumn++;
const temp_hash = {
row: 0,
position_in_row: 0,
actual_number: 0,
color: this.state.if_zero_color
};
temp_hash.row = beginRow;
temp_hash.position_in_row = beginColumn;
temp_hash.actual_number = count;
temp_hash.value = 0; //just to get rid of the warning.
matrixUsing.push(temp_hash);
count++;
beginColumn++;
}
beginRow++;
}
const width_size = 22 * this.state.col_count;
const width = `${width_size}px`;
this.setState({
matrix,
matrixUsing,
width,
matrixMirror: matrix,
mirror: matrixUsing
});
return true; // means its finished.
};
continueWithGame = () => {
const matrixUsing = [];
const matrix = { ...this.state.matrix }; //creates a copy
let count = 0;
Object.entries(matrix).forEach(entry => {
for (let x of entry[1]) {
const temp_hash = {
row: 0,
position_in_row: 0,
actual_number: 0,
color: this.state.if_zero_color
};
temp_hash.row = Number(entry[0]);
temp_hash.position_in_row = count % this.state.col_count;
temp_hash.actual_number = count;
temp_hash.value = x; //just to get rid of the warning.
matrixUsing.push(temp_hash);
count++;
}
});
this.setState({ matrix, matrixUsing });
};
manualTurnOnOrOff = (row, position_in_row) => {
//Just add 1 to the % of 2 it will provide 0 or 1. The conditional is already set up on the div to set the div to the correct class based off the value
if (this.state.gameRunning === false) {
const matrix = this.state.matrix;
// matrix[row][position_in_row] = matrix[row][position_in_row] === 0 ? this.state.if_one_color: this.state.if_zero_color;
matrix[row][position_in_row] = matrix[row][position_in_row] === 0 ? 1 : 0;
this.setState({ matrix });
}
};
// Game functions being declared below this line.
handleChange = event => {
//Will handle changing the columns. and columns
//function allows user to change the amount of columns that is being used.
this.setState({ [event.target.name]: event.target.value });
};
runGamne = () => {
this.nextGeneration();
};
startTheGame = () => {
//Function will start the game.
console.log(this.state.gameRunning);
if (this.state.gameRunning === false) {
// let intervalRef = setInterval(this.runGamne(), 1000); will only run one time. Not a loop.
let intervalRef = setInterval(
() => this.runGamne(),
this.state.gameSpeed
);
this.setState({
gameRunning: true,
intervalRef
});
}
};
stopTheGame = () => {
// //Function will stop the game.
clearInterval(this.state.intervalRef);
this.setState(prevState => ({ gameRunning: !prevState.gameRunning }));
};
nextGeneration = () => {
//functionaly for gameplay goes here
const matrix = this.state.matrixUsing.slice();
const state_matrix = { ...this.state.matrix }; //creates a copy
for (let i = 0; i < matrix.length; i++) {
let aliveNeighbors = this.findLiveNeighbors({
row: matrix[i].row,
position_in_row: matrix[i].position_in_row
});
let current_cell_alive =
this.state.matrix[matrix[i].row][matrix[i].position_in_row] === 1
? true
: false;
if (current_cell_alive) {
if (aliveNeighbors < 2 || aliveNeighbors > 3) {
//kill the cell that is currently alive.
state_matrix[matrix[i].row][matrix[i].position_in_row] = 0;
}
} else {
if (aliveNeighbors === 3) {
//resurrect the currently dead cell.
state_matrix[matrix[i].row][matrix[i].position_in_row] = 1;
}
}
}
this.setState(prevState => ({
matrix: state_matrix,
generation: prevState.generation + 1
}));
this.continueWithGame();
};
handleColorChangeIfZero = color => {
this.setState({ if_zero_color: color });
};
handleColorChangeIfOne = color => {
this.setState({ if_one_color: color });
};
findLiveNeighbors = position => {
//find neighbors of the live cell to use the rules on.
//position should be an object featuring the row and position in row.
//A neighbor will be to the left to the right up down and diagonal which will be up to the left, up to the right and down to the left down to the right.
let totalAlive = 0;
let lookUp = true;
let lookDown = true;
let lookRight = true;
let lookLeft = true;
//logic for if I should look up or down.
if (position.row === 0) {
lookUp = false;
} else if (position.row === this.state.row_count - 1) {
lookDown = false;
}
//logic for if I should look right or left.
if (position.position_in_row === 0) {
lookLeft = false;
} else if (position.position_in_row === this.state.col_count - 1) {
lookRight = false;
}
if (lookUp) {
if (this.state.matrix[position.row - 1][position.position_in_row] === 1) {
totalAlive++;
}
}
if (lookDown) {
if (this.state.matrix[position.row + 1][position.position_in_row] === 1) {
totalAlive++;
}
}
if (lookRight) {
if (this.state.matrix[position.row][position.position_in_row + 1] === 1) {
//next in line plus one//2
totalAlive++;
}
}
if (lookLeft) {
if (this.state.matrix[position.row][position.position_in_row - 1] === 1) {
//4
totalAlive++;
}
}
if (lookUp && lookRight) {
if (
this.state.matrix[position.row - 1][position.position_in_row + 1] === 1
) {
//3
totalAlive++;
}
}
if (lookUp && lookLeft) {
//diagonal left up one minus one //5
if (
this.state.matrix[position.row - 1][position.position_in_row - 1] === 1
) {
totalAlive++;
}
}
if (lookDown && lookRight) {
//down one to the right (plus one) //7
if (
this.state.matrix[position.row + 1][position.position_in_row + 1] === 1
) {
totalAlive++;
}
}
if (lookDown && lookLeft) {
//down one to the left (minus one) //8
if (
this.state.matrix[position.row + 1][position.position_in_row - 1] === 1
) {
totalAlive++;
}
}
return totalAlive;
};
presetChange = type => {
const finished = this.setMatrixUp(); //will reset the grid before setting it up.
let row_index = 0;
let col_index = 0;
if (finished) {
const matrix = { ...this.state.matrixMirror }; //creates a copy
if (Object.keys(matrix).length) {
switch (type) {
case "Block":
this.setMatrixUp(); //will reset the grid before setting it up.
row_index =
this.state.row_count % 2 === 0
? this.state.row_count / 2
: (this.state.row_count - 1) / 2;
col_index =
this.state.col_count % 2 === 0
? this.state.col_count / 2
: (this.state.col_count - 1) / 2;
matrix[row_index][col_index] = 1;
matrix[row_index - 1][col_index] = 1;
matrix[row_index - 1][col_index - 1] = 1;
matrix[row_index][col_index - 1] = 1;
this.setState({ matrix }, () => {
this.continueWithGame();
});
break;
case "Beehive":
row_index =
this.state.row_count % 2 === 0
? this.state.row_count / 2
: (this.state.row_count - 1) / 2;
col_index =
this.state.col_count % 2 === 0
? this.state.col_count / 2
: (this.state.col_count - 1) / 2;
matrix[row_index][col_index] = 1;
matrix[row_index][col_index - 1] = 1;
matrix[row_index - 1][col_index - 2] = 1;
matrix[row_index - 2][col_index] = 1;
matrix[row_index - 2][col_index - 1] = 1;
matrix[row_index - 1][col_index + 1] = 1;
this.setState({ matrix }, () => {
this.continueWithGame();
});
break;
case "Loaf":
row_index =
this.state.row_count % 2 === 0
? this.state.row_count / 2
: (this.state.row_count - 1) / 2;
col_index =
this.state.col_count % 2 === 0
? this.state.col_count / 2
: (this.state.col_count - 1) / 2;
matrix[row_index][col_index] = 1;
matrix[row_index - 1][col_index] = 1;
matrix[row_index + 1][col_index - 1] = 1;
matrix[row_index][col_index - 2] = 1;
matrix[row_index - 1][col_index - 3] = 1;
matrix[row_index - 2][col_index - 2] = 1;
matrix[row_index - 2][col_index - 1] = 1;
this.setState({ matrix }, () => {
this.continueWithGame();
});
break;
case "Boat":
row_index =
this.state.row_count % 2 === 0
? this.state.row_count / 2
: (this.state.row_count - 1) / 2;
col_index =
this.state.col_count % 2 === 0
? this.state.col_count / 2
: (this.state.col_count - 1) / 2;
matrix[row_index - 1][col_index] = 1;
matrix[row_index - 1][col_index - 2] = 1;
matrix[row_index - 2][col_index - 2] = 1;
matrix[row_index - 2][col_index - 1] = 1;
matrix[row_index][col_index - 1] = 1;
this.setState({ matrix }, () => {
this.continueWithGame();
});
break;
case "Tub":
row_index =
this.state.row_count % 2 === 0
? this.state.row_count / 2
: (this.state.row_count - 1) / 2;
col_index =
this.state.col_count % 2 === 0
? this.state.col_count / 2
: (this.state.col_count - 1) / 2;
matrix[row_index - 1][col_index] = 1;
matrix[row_index - 1][col_index - 2] = 1;
matrix[row_index - 2][col_index - 1] = 1;
matrix[row_index][col_index - 1] = 1;
this.setState({ matrix }, () => {
this.continueWithGame();
});
break;
case "Jon":
matrix[2][1] = 1;
matrix[2][2] = 1;
matrix[2][3] = 1;
matrix[3][3] = 1;
matrix[4][3] = 1;
matrix[5][3] = 1;
matrix[6][3] = 1;
matrix[6][2] = 1;
matrix[6][1] = 1;
matrix[6][0] = 1;
matrix[5][0] = 1;
//above is J
matrix[2][6] = 1;
matrix[2][7] = 1;
matrix[2][8] = 1;
matrix[3][6] = 1;
matrix[4][6] = 1;
matrix[5][6] = 1;
matrix[6][6] = 1;
matrix[6][7] = 1;
matrix[6][8] = 1;
matrix[5][8] = 1;
matrix[4][8] = 1;
matrix[3][8] = 1;
//above is o
matrix[2][11] = 1;
matrix[2][12] = 1;
matrix[2][13] = 1;
matrix[3][11] = 1;
matrix[4][11] = 1;
matrix[5][11] = 1;
matrix[6][11] = 1;
matrix[3][13] = 1;
matrix[4][13] = 1;
matrix[5][13] = 1;
matrix[6][13] = 1;
this.setState({ matrix }, () => {
this.continueWithGame();
});
break;
case "Blinker 1":
row_index =
this.state.row_count % 2 === 0
? this.state.row_count / 2
: (this.state.row_count - 1) / 2;
col_index =
this.state.col_count % 2 === 0
? this.state.col_count / 2
: (this.state.col_count - 1) / 2;
matrix[row_index][col_index] = 1;
matrix[row_index - 1][col_index] = 1;
matrix[row_index + 1][col_index] = 1;
this.setState({ matrix }, () => {
this.continueWithGame();
});
break;
case "Blinker 2":
row_index =
this.state.row_count % 2 === 0
? this.state.row_count / 2
: (this.state.row_count - 1) / 2;
col_index =
this.state.col_count % 2 === 0
? this.state.col_count / 2
: (this.state.col_count - 1) / 2;
matrix[row_index][col_index] = 1;
matrix[row_index][col_index - 1] = 1;
matrix[row_index][col_index + 1] = 1;
this.setState({ matrix }, () => {
this.continueWithGame();
});
break;
default:
console.log("That type doesn't exist"); //only for react warning purposes this won't actually hit.
}
}
}
};
decrementSpeed = () => {
if (this.state.gameSpeed > 125) {
this.setState(prevState => ({ gameSpeed: prevState.gameSpeed - 1 }));
}
};
incrementSpeed = () => {
if (this.state.gameSpeed < 2000) {
this.setState(prevState => ({ gameSpeed: prevState.gameSpeed + 1 }));
}
};
render() {
const matrix = this.state.matrixUsing.slice();
return (
<div className="container" style={{ width: this.state.width }}>
<h2 className="titleApp">Conway's Game of Life</h2>
<p>
Rules: • If a cell is alive and it has exactly 2 or 3 living
neighbors, it stays alive. • If a cell is dead and it has exactly 3
living neighbors, it rises again.
</p>
<p>Click random then start or one of the presets then start. YOu can pause or stop. You can also click on some of the cells to make them alive then click next for one generation or start for automatic generation.</p>
<h4 className="titleApp">Generation : {this.state.generation}</h4>
<div className="topButtons">
<div>
<button onClick={this.startTheGame}>Start</button>{" "}
</div>
<div>
<button onClick={this.stopTheGame}>Stop</button>
</div>
<div>
<button onClick={this.reset}>Clear</button>
</div>
<div>
<button onClick={this.generateRandom}>Random</button>
</div>
<div>
<button onClick={this.nextGeneration}>Next</button>
</div>
<br />
<br />
<br />
</div>
{matrix.map((hash, id) => (
<div
key={id}
onClick={() =>
this.manualTurnOnOrOff(hash.row, hash.position_in_row)
}
style={{
background:
this.state.matrix[hash.row][hash.position_in_row] === 0
? this.state.if_zero_color
: this.state.if_one_color,
color:
this.state.matrix[hash.row][hash.position_in_row] === 0
? this.state.if_zero_color
: this.state.if_one_color
}}
className={
this.state.matrix[hash.row][hash.position_in_row] === 0
? "offDiv gridDiv"
: "onDiv gridDiv"
}
>
{hash.position_in_row}
</div>
))}
<div>
<h5>Presets</h5>
<div className="presetsDiv">
<p onClick={() => this.presetChange("Block")}>Block</p>
<p onClick={() => this.presetChange("Beehive")}>Beehive</p>
<p onClick={() => this.presetChange("Loaf")}>Loaf</p>
<p onClick={() => this.presetChange("Boat")}>Boat</p>
<p onClick={() => this.presetChange("Tub")}>Tub</p>
<p onClick={() => this.presetChange("Jon")}>Jon</p>
</div>
<h5>Current Speed {this.state.gameSpeed}</h5>
<p>Stop the game and then click start to notice speed change.</p>
<div className="presetsDiv">
<input
type="range"
min="125"
max="2000"
value={this.state.gameSpeed}
className="slider"
id="myRange"
name="gameSpeed"
onChange={this.handleChange}
/>
</div>
<div className="slidecontainer">
<h5>Row Count {this.state.row_count}</h5>
<input
type="range"
min="15"
max="30"
value={this.state.row_count}
className="slider"
id="myRange"
name="row_count"
onChange={this.handleChange}
/>
<h5>Col Count {this.state.col_count}</h5>
<input
type="range"
min="15"
max="30"
value={this.state.col_count}
className="slider"
id="myRange"
name="col_count"
onChange={this.handleChange}
/>
<p>Click update grid to set the row and column size</p>
<button onClick={this.setMatrixUp}>Update Grid</button>
<br />
<h5> Dead Color: {this.state.if_zero_color}</h5>
<div className="colorChoices">
<div
className="divcolor white"
onClick={() => this.handleColorChangeIfZero("white")}
>
w
</div>
<div
className="divcolor black"
onClick={() => this.handleColorChangeIfZero("black")}
>
b
</div>
<div
className="divcolor blue"
onClick={() => this.handleColorChangeIfZero("blue")}
>
bl
</div>
<div
className="divcolor green"
onClick={() => this.handleColorChangeIfZero("green")}
>
g
</div>
<div
className="divcolor orange"
onClick={() => this.handleColorChangeIfZero("orange")}
>
o
</div>
<div
className="divcolor purple"
onClick={() => this.handleColorChangeIfZero("purple")}
>
pu
</div>
<div
className="divcolor pink"
onClick={() => this.handleColorChangeIfZero("pink")}
>
pk
</div>
<div
className="divcolor yellow"
onClick={() => this.handleColorChangeIfZero("yellow")}
>
y
</div>
</div>
<h5> Alive Color: {this.state.if_one_color}</h5>
<div className="colorChoices">
<div
className="divcolor white"
onClick={() => this.handleColorChangeIfOne("white")}
>
w
</div>
<div
className="divcolor black"
onClick={() => this.handleColorChangeIfOne("black")}
>
b
</div>
<div
className="divcolor blue"
onClick={() => this.handleColorChangeIfOne("blue")}
>
bl
</div>
<div
className="divcolor green"
onClick={() => this.handleColorChangeIfOne("green")}
>
g
</div>
<div
className="divcolor orange"
onClick={() => this.handleColorChangeIfOne("orange")}
>
o
</div>
<div
className="divcolor purple"
onClick={() => this.handleColorChangeIfOne("purple")}
>
p
</div>
<div
className="divcolor pink"
onClick={() => this.handleColorChangeIfOne("pink")}
>
pk
</div>
<div
className="divcolor yellow"
onClick={() => this.handleColorChangeIfOne("yellow")}
>
y
</div>
</div>
</div>
<br />
<br />
<br />
<br />
<br />
<h5>About Conway's Game</h5>
<p>
The game was devised by John Horton Conway in 1970. It is a cellular
automaton where each cell is in one of finite number of states. For
example the states for the cells in this program is dead or alive.
The basics of celluar automaton is to use complex systems composed
of simple units. This program is turing completre which basically is
saying that the different generations can be simulated. What this
means if we start off at the same exact grid it will eventaully get
to a certain state that we can predict with 100% certainity. Take
the presets for example, the first 5 do not change. However Jon
changes and gets to about 110 before being unchangeable. This state
can be simulated and predicted. Turing completness comes from turing
machines. A turing machine can take a set of rules and determine a
result from a set of variables inputed. Thus the grid if it starts
off as ABC567 and we have rules stating what each input should
change to next we can predict what the input will be after x amount
of generations.
</p>
</div>
</div>
);
}
}
export default App;