@@ -7,8 +7,11 @@ function App() {
7
7
const [ gameBoard , setGameBoard ] = useState < number [ ] [ ] > ( Array ( MAX_WIDTH ) . fill ( Array ( MAX_HEIGHT ) . fill ( 0 ) ) ) ;
8
8
const [ simulationRunning , setSimulationRunning ] = useState < boolean > ( false ) ;
9
9
const [ gameSpeed , setGameSpeed ] = useState < number > ( 200 ) ;
10
+ const [ toroidalBorders , setToroidalBorders ] = useState < boolean > ( false ) ;
10
11
const gameSpeedRef = useRef < number > ( gameSpeed ) ;
11
12
const simulationRunningRef = useRef < boolean > ( simulationRunning ) ;
13
+ const toroidalBordersRef = useRef < boolean > ( toroidalBorders ) ;
14
+ toroidalBordersRef . current = toroidalBorders ;
12
15
gameSpeedRef . current = gameSpeed ;
13
16
simulationRunningRef . current = simulationRunning ;
14
17
@@ -27,7 +30,7 @@ function App() {
27
30
return produce ( g , ( gameBoardCopy ) => {
28
31
for ( let x = 0 ; x < gameBoardCopy . length ; x ++ ) {
29
32
for ( let y = 0 ; y < gameBoardCopy . length ; y ++ ) {
30
- gameBoardCopy [ x ] [ y ] = resolvePosition ( g , x , y ) ;
33
+ gameBoardCopy [ x ] [ y ] = resolvePosition ( g , x , y , toroidalBordersRef . current ) ;
31
34
}
32
35
}
33
36
} ) ;
@@ -41,48 +44,70 @@ function App() {
41
44
} , [ ] ) ;
42
45
43
46
return (
44
- < div >
45
- < div style = { { display : 'grid' , gridTemplateColumns : `repeat(${ MAX_WIDTH } ,20px` } } >
46
- { gameBoard . map ( ( row : number [ ] , i ) =>
47
- row . map ( ( _column : number , j ) => {
48
- const key = `el_${ i } _${ j } ` ;
49
- return (
50
- < div
51
- key = { key }
52
- onClick = { ( ) => {
53
- handleTogglePosition ( i , j ) ;
47
+ < div style = { { height : '100vh' , display : 'flex' , justifyContent : 'center' , alignItems : 'center' } } >
48
+ < div >
49
+ < div style = { { display : 'grid' , gridTemplateColumns : `repeat(${ MAX_WIDTH } ,20px` } } >
50
+ { gameBoard . map ( ( row : number [ ] , i ) =>
51
+ row . map ( ( _column : number , j ) => {
52
+ const key = `el_${ i } _${ j } ` ;
53
+ return (
54
+ < div
55
+ key = { key }
56
+ onClick = { ( ) => {
57
+ handleTogglePosition ( i , j ) ;
58
+ } }
59
+ style = { {
60
+ width : 20 ,
61
+ height : 20 ,
62
+ backgroundColor : gameBoard [ i ] [ j ] ? 'skyblue' : undefined ,
63
+ border : '1px solid black' ,
64
+ } }
65
+ />
66
+ ) ;
67
+ } ) ,
68
+ ) }
69
+ </ div >
70
+ < span style = { { display : 'flex' , flexDirection : 'row' } } >
71
+ < button
72
+ style = { { width : '200px' , height : '50px' , margin : '10px' } }
73
+ onClick = { ( ) => {
74
+ setSimulationRunning ( ! simulationRunning ) ;
75
+ if ( ! simulationRunning ) {
76
+ simulationRunningRef . current = true ;
77
+ startSimulation ( ) ;
78
+ }
79
+ } }
80
+ >
81
+ { simulationRunning ? 'pause' : 'start' }
82
+ </ button >
83
+ < div style = { { margin : '10px' , padding : '5px' } } >
84
+ < label >
85
+ < input
86
+ type = { 'numeric' }
87
+ value = { gameSpeed }
88
+ onChange = { ( e ) => {
89
+ const { value : rawValue } = e . target ;
90
+ const num = parseInt ( rawValue || '0' ) ;
91
+ setGameSpeed ( num ) ;
54
92
} }
55
- style = { {
56
- width : 20 ,
57
- height : 20 ,
58
- backgroundColor : gameBoard [ i ] [ j ] ? 'skyblue' : undefined ,
59
- border : '1px solid black' ,
93
+ />
94
+ < span > game speed</ span >
95
+ </ label >
96
+ </ div >
97
+ < div style = { { margin : '10px' , padding : '5px' } } >
98
+ < label className = { 'switch' } >
99
+ < input
100
+ type = "checkbox"
101
+ value = { `${ toroidalBorders } ` }
102
+ onChange = { ( ) => {
103
+ setToroidalBorders ( ! toroidalBorders ) ;
60
104
} }
61
105
/>
62
- ) ;
63
- } ) ,
64
- ) }
106
+ < span > Wrap Toroidal</ span >
107
+ </ label >
108
+ </ div >
109
+ </ span >
65
110
</ div >
66
- < button
67
- style = { { width : '200px' , height : '50px' } }
68
- onClick = { ( ) => {
69
- setSimulationRunning ( ! simulationRunning ) ;
70
- if ( ! simulationRunning ) {
71
- simulationRunningRef . current = true ;
72
- startSimulation ( ) ;
73
- }
74
- } }
75
- >
76
- { simulationRunning ? 'pause' : 'start' }
77
- </ button >
78
- < input
79
- type = { 'numeric' }
80
- value = { gameSpeed }
81
- onChange = { ( e ) => {
82
- const num = parseInt ( e . target . value ) ;
83
- setGameSpeed ( num ) ;
84
- } }
85
- />
86
111
</ div >
87
112
) ;
88
113
}
0 commit comments