|
@@ -1,7 +1,7 @@
|
1
|
1
|
import React from 'react';
|
2
|
2
|
import Cell from './Cell';
|
3
|
3
|
import Controls from './Controls';
|
4
|
|
-import { countLivingNeighbors } from './lib';
|
|
4
|
+import { generateNewGrid } from './lib';
|
5
|
5
|
|
6
|
6
|
interface Props {
|
7
|
7
|
width?: number;
|
|
@@ -32,32 +32,8 @@ class Board extends React.Component<Props, State> {
|
32
|
32
|
|
33
|
33
|
start = () => {
|
34
|
34
|
this.timer = setInterval(() => {
|
35
|
|
- console.log('Iterating');
|
36
|
|
- const { grid } = this.state;
|
37
|
|
- const newGrid = [...grid].map((row, y) => {
|
38
|
|
- const newRow = [...row].map((cell, x) => {
|
39
|
|
- const isAlive = cell;
|
40
|
|
- const livingNeighbors = countLivingNeighbors(grid, x, y);
|
41
|
|
-
|
42
|
|
- if (isAlive) {
|
43
|
|
- if (livingNeighbors === 2 || livingNeighbors === 3) {
|
44
|
|
- return true;
|
45
|
|
- } else {
|
46
|
|
- return false;
|
47
|
|
- }
|
48
|
|
- } else {
|
49
|
|
- if (livingNeighbors === 3) {
|
50
|
|
- return true;
|
51
|
|
- }
|
52
|
|
- }
|
53
|
|
-
|
54
|
|
- return isAlive;
|
55
|
|
- });
|
56
|
|
-
|
57
|
|
- return newRow;
|
58
|
|
- });
|
59
|
|
-
|
60
|
|
- this.setState({ grid: newGrid });
|
|
35
|
+ const grid = generateNewGrid(this.state.grid);
|
|
36
|
+ this.setState({ grid });
|
61
|
37
|
}, this.state.speed);
|
62
|
38
|
};
|
63
|
39
|
|