|
|
@@ -3,29 +3,29 @@ import Cell from './Cell';
|
|
3
|
3
|
import Controls from './Controls';
|
|
4
|
4
|
import { generateNewGrid } from './lib';
|
|
5
|
5
|
|
|
6
|
|
-interface Props {
|
|
7
|
|
- width?: number;
|
|
8
|
|
- height?: number;
|
|
9
|
|
- speed?: number;
|
|
10
|
|
-}
|
|
|
6
|
+const DEFAULT_WIDTH = 20;
|
|
|
7
|
+const DEFAULT_HEIGHT = 10;
|
|
|
8
|
+const DEFAULT_SPEED = 500;
|
|
11
|
9
|
|
|
12
|
10
|
interface State {
|
|
13
|
11
|
grid: boolean[][];
|
|
14
|
12
|
running: boolean;
|
|
15
|
13
|
speed: number;
|
|
|
14
|
+ width: number;
|
|
|
15
|
+ height: number;
|
|
16
|
16
|
}
|
|
17
|
17
|
|
|
18
|
|
-class Board extends React.Component<Props, State> {
|
|
|
18
|
+class Board extends React.Component<{}, State> {
|
|
19
|
19
|
timer: NodeJS.Timeout | null;
|
|
20
|
20
|
|
|
21
|
|
- constructor(props: Props) {
|
|
|
21
|
+ constructor(props: {}) {
|
|
22
|
22
|
super(props);
|
|
23
|
|
- const height = props.height ?? 10;
|
|
24
|
|
- const width = props.height ?? 20;
|
|
25
|
23
|
this.state = {
|
|
26
|
|
- grid: Array(height).fill(Array(width).fill(false)),
|
|
|
24
|
+ grid: Array(DEFAULT_HEIGHT).fill(Array(DEFAULT_WIDTH).fill(false)),
|
|
27
|
25
|
running: false,
|
|
28
|
|
- speed: 500,
|
|
|
26
|
+ speed: DEFAULT_SPEED,
|
|
|
27
|
+ width: DEFAULT_WIDTH,
|
|
|
28
|
+ height: DEFAULT_HEIGHT,
|
|
29
|
29
|
};
|
|
30
|
30
|
this.timer = null;
|
|
31
|
31
|
}
|
|
|
@@ -63,8 +63,36 @@ class Board extends React.Component<Props, State> {
|
|
63
|
63
|
}
|
|
64
|
64
|
};
|
|
65
|
65
|
|
|
|
66
|
+ updateDimensions = (dimensions: { width?: number; height?: number }) => {
|
|
|
67
|
+ if (dimensions.width) {
|
|
|
68
|
+ this.setState({ width: dimensions.width });
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ if (dimensions.height) {
|
|
|
72
|
+ this.setState({ height: dimensions.height });
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+ const width = dimensions.width ?? this.state.width;
|
|
|
76
|
+ const height = dimensions.height ?? this.state.height;
|
|
|
77
|
+
|
|
|
78
|
+ const grid = [...this.state.grid];
|
|
|
79
|
+ while (grid.length < height) {
|
|
|
80
|
+ grid.push(Array(width).fill(false));
|
|
|
81
|
+ }
|
|
|
82
|
+
|
|
|
83
|
+ const newGrid = grid.map((row) => {
|
|
|
84
|
+ while (row.length < width) {
|
|
|
85
|
+ row.push(false);
|
|
|
86
|
+ }
|
|
|
87
|
+
|
|
|
88
|
+ return row;
|
|
|
89
|
+ });
|
|
|
90
|
+
|
|
|
91
|
+ this.setState({ grid: newGrid });
|
|
|
92
|
+ };
|
|
|
93
|
+
|
|
66
|
94
|
render() {
|
|
67
|
|
- const { grid, running, speed } = this.state;
|
|
|
95
|
+ const { grid, running, speed, width, height } = this.state;
|
|
68
|
96
|
|
|
69
|
97
|
return (
|
|
70
|
98
|
<div className="board">
|
|
|
@@ -72,6 +100,9 @@ class Board extends React.Component<Props, State> {
|
|
72
|
100
|
running={running}
|
|
73
|
101
|
speed={speed}
|
|
74
|
102
|
toggle={this.toggleRunning}
|
|
|
103
|
+ width={width}
|
|
|
104
|
+ height={height}
|
|
|
105
|
+ updateDimensions={this.updateDimensions}
|
|
75
|
106
|
updateSpeed={this.updateSpeed}
|
|
76
|
107
|
/>
|
|
77
|
108
|
{grid.map((row, y) => (
|