|
@@ -1,5 +1,6 @@
|
1
|
1
|
import React, { useState } from 'react';
|
2
|
2
|
import Cell from './Cell';
|
|
3
|
+import Controls from './Controls';
|
3
|
4
|
|
4
|
5
|
interface Props {
|
5
|
6
|
width?: number;
|
|
@@ -11,9 +12,12 @@ const Board = (props: Props) => {
|
11
|
12
|
const height = props.height ?? 5;
|
12
|
13
|
|
13
|
14
|
const initialGrid: boolean[][] = Array(height).fill(Array(width).fill(false));
|
|
15
|
+
|
14
|
16
|
const [grid, setGrid] = useState<boolean[][]>(initialGrid);
|
15
|
17
|
|
16
|
|
- const toggle = (x: number, y: number) => {
|
|
18
|
+ const [running, setRunning] = useState<boolean>(false);
|
|
19
|
+
|
|
20
|
+ const toggleCell = (x: number, y: number) => {
|
17
|
21
|
const newGrid = [...grid];
|
18
|
22
|
const newRow = [...newGrid[y]];
|
19
|
23
|
newRow[x] = !newGrid[y][x];
|
|
@@ -21,12 +25,17 @@ const Board = (props: Props) => {
|
21
|
25
|
setGrid(newGrid);
|
22
|
26
|
};
|
23
|
27
|
|
|
28
|
+ const toggleRunning = () => {
|
|
29
|
+ setRunning(!running);
|
|
30
|
+ };
|
|
31
|
+
|
24
|
32
|
return (
|
25
|
33
|
<div className="board">
|
|
34
|
+ <Controls running={running} toggle={toggleRunning} />
|
26
|
35
|
{grid.map((row, y) => (
|
27
|
36
|
<div className="row" key={y}>
|
28
|
37
|
{row.map((cell, x) => (
|
29
|
|
- <Cell x={x} y={y} alive={cell} toggle={toggle} key={x} />
|
|
38
|
+ <Cell x={x} y={y} alive={cell} toggle={toggleCell} key={x} />
|
30
|
39
|
))}
|
31
|
40
|
</div>
|
32
|
41
|
))}
|