Browse Source

Reset button stops the game

master
Dylan Baker 3 years ago
parent
commit
444d6fcb18
4 changed files with 43 additions and 9 deletions
  1. 19
    6
      src/Board.tsx
  2. 3
    1
      src/Cell.tsx
  3. 19
    1
      src/Controls.tsx
  4. 2
    1
      src/index.css

+ 19
- 6
src/Board.tsx View File

@@ -3,9 +3,10 @@ import Cell from './Cell';
3 3
 import Controls from './Controls';
4 4
 import { generateNewGrid, updateGridDimensions } from './lib';
5 5
 
6
-const DEFAULT_WIDTH = 20;
7
-const DEFAULT_HEIGHT = 10;
8
-const DEFAULT_SPEED = 500;
6
+const DEFAULT_WIDTH = 30;
7
+const DEFAULT_HEIGHT = 20;
8
+const DEFAULT_SPEED = 100;
9
+const DEFAULT_CELL_SIZE = 30;
9 10
 
10 11
 const INITIAL_GRID = Array(DEFAULT_HEIGHT).fill(
11 12
   Array(DEFAULT_WIDTH).fill(false),
@@ -14,6 +15,7 @@ const INITIAL_GRID = Array(DEFAULT_HEIGHT).fill(
14 15
 interface State {
15 16
   grid: boolean[][];
16 17
   running: boolean;
18
+  cellSize: number;
17 19
   speed: number;
18 20
   width: number;
19 21
   height: number;
@@ -27,6 +29,7 @@ class Board extends React.Component<{}, State> {
27 29
     this.state = {
28 30
       grid: [...INITIAL_GRID],
29 31
       running: false,
32
+      cellSize: DEFAULT_CELL_SIZE,
30 33
       speed: DEFAULT_SPEED,
31 34
       width: DEFAULT_WIDTH,
32 35
       height: DEFAULT_HEIGHT,
@@ -47,6 +50,10 @@ class Board extends React.Component<{}, State> {
47 50
 
48 51
   reset = () => {
49 52
     this.setState({ grid: [...INITIAL_GRID] });
53
+    this.stop();
54
+    this.setState({ running: false });
55
+    this.setState({ cellSize: DEFAULT_CELL_SIZE });
56
+    this.setState({ width: DEFAULT_WIDTH, height: DEFAULT_HEIGHT });
50 57
   };
51 58
 
52 59
   toggleCell = (x: number, y: number) => {
@@ -71,6 +78,10 @@ class Board extends React.Component<{}, State> {
71 78
     }
72 79
   };
73 80
 
81
+  updateCellSize = (f: (size: number) => number) => {
82
+    this.setState({ cellSize: f(this.state.cellSize) });
83
+  };
84
+
74 85
   updateDimensions = (dimensions: { width?: number; height?: number }) => {
75 86
     if (dimensions.width) {
76 87
       this.setState({ width: dimensions.width });
@@ -90,16 +101,17 @@ class Board extends React.Component<{}, State> {
90 101
   };
91 102
 
92 103
   render() {
93
-    const { grid, running, speed, width, height } = this.state;
104
+    const { grid, running, speed, width, height, cellSize } = this.state;
94 105
 
95 106
     return (
96 107
       <div className="container">
97 108
         <Controls
98 109
           running={running}
99 110
           speed={speed}
100
-          toggle={this.toggleRunning}
101 111
           width={width}
102 112
           height={height}
113
+          toggle={this.toggleRunning}
114
+          updateCellSize={this.updateCellSize}
103 115
           updateDimensions={this.updateDimensions}
104 116
           updateSpeed={this.updateSpeed}
105 117
           reset={this.reset}
@@ -107,12 +119,13 @@ class Board extends React.Component<{}, State> {
107 119
         <div className="board-container">
108 120
           <div className="board">
109 121
             {grid.map((row, y) => (
110
-              <div className="row" key={y}>
122
+              <div className="row" key={y} style={{ height: cellSize }}>
111 123
                 {row.map((cell, x) => (
112 124
                   <Cell
113 125
                     disabled={running}
114 126
                     x={x}
115 127
                     y={y}
128
+                    cellSize={cellSize}
116 129
                     alive={cell}
117 130
                     toggle={this.toggleCell}
118 131
                     key={x}

+ 3
- 1
src/Cell.tsx View File

@@ -6,16 +6,18 @@ interface Props {
6 6
   alive: boolean;
7 7
   disabled: boolean;
8 8
   toggle: (x: number, y: number) => void;
9
+  cellSize: number;
9 10
 }
10 11
 
11 12
 const Cell = (props: Props) => {
12
-  const { alive, toggle, x, y, disabled } = props;
13
+  const { alive, toggle, x, y, disabled, cellSize } = props;
13 14
   return (
14 15
     <span
15 16
       onClick={() => (disabled ? null : toggle(x, y))}
16 17
       className={`cell ${alive ? 'alive' : 'dead'} ${
17 18
         disabled ? 'nonclickable' : 'clickable'
18 19
       }`}
20
+      style={{ width: `${cellSize}px`, height: `${cellSize}px` }}
19 21
     ></span>
20 22
   );
21 23
 };

+ 19
- 1
src/Controls.tsx View File

@@ -7,8 +7,9 @@ interface Props {
7 7
   height: number;
8 8
   reset: () => void;
9 9
   toggle: () => void;
10
-  updateSpeed: (speed: number) => void;
10
+  updateCellSize: (f: (size: number) => number) => void;
11 11
   updateDimensions: (dimensions: { width?: number; height?: number }) => void;
12
+  updateSpeed: (speed: number) => void;
12 13
 }
13 14
 
14 15
 const Controls = (props: Props) => {
@@ -20,6 +21,7 @@ const Controls = (props: Props) => {
20 21
     width,
21 22
     height,
22 23
     reset,
24
+    updateCellSize,
23 25
     updateDimensions,
24 26
   } = props;
25 27
 
@@ -33,6 +35,14 @@ const Controls = (props: Props) => {
33 35
     }
34 36
   };
35 37
 
38
+  const getSmaller = () => {
39
+    updateCellSize((size) => size - 1);
40
+  };
41
+
42
+  const getBigger = () => {
43
+    updateCellSize((size) => size + 1);
44
+  };
45
+
36 46
   useEffect(() => {
37 47
     document.addEventListener('keyup', (e) => {
38 48
       if (e.key === ' ') {
@@ -75,6 +85,14 @@ const Controls = (props: Props) => {
75 85
           Faster
76 86
         </button>
77 87
       </div>
88
+      <div className="controls__section size">
89
+        <button className="button" onClick={getSmaller}>
90
+          Smaller
91
+        </button>
92
+        <button className="button" onClick={getBigger}>
93
+          Bigger
94
+        </button>
95
+      </div>
78 96
       <div className="controls__section play-reset">
79 97
         <div className="reset">
80 98
           <button className="button" onClick={reset}>

+ 2
- 1
src/index.css View File

@@ -91,11 +91,12 @@ code {
91 91
   margin: 0.5em 0;
92 92
 }
93 93
 
94
+.size,
94 95
 .speed {
95 96
   align-items: center;
96 97
   display: flex;
97 98
   flex: 1;
98
-  justify-content: flex-end;
99
+  justify-content: center;
99 100
 }
100 101
 
101 102
 .button[disabled] {

Loading…
Cancel
Save