1
- import React , { useState } from "react" ;
1
+ import React , { useEffect , useState } from "react" ;
2
2
3
3
import { Box } from "./Box.tsx" ;
4
4
import { playNote } from "../util/playNote.ts" ;
@@ -24,36 +24,28 @@ export function App() {
24
24
const [ gameState , setGameState ] = useState < GameState > ( "pre-game" ) ;
25
25
const [ correctMoves , setCorrectMoves ] = useState < readonly number [ ] > ( [ ] ) ;
26
26
27
+ useEffect ( ( ) => {
28
+ if ( gameState !== "cpu-turn" ) {
29
+ return ;
30
+ }
31
+ setCorrectMoves ( ( prev ) => [ ...prev , randNum ( 4 ) ] ) ;
32
+ setPlayerMoves ( [ ] ) ;
33
+ setGameState ( "player-turn" ) ;
34
+ } , [ gameState ] ) ;
35
+
36
+ const newGame = ( ) => {
37
+ setGameState ( "cpu-turn" ) ;
38
+ setCorrectMoves ( [ ] ) ; // TODO: Add to cpu-turn state instead once created
39
+ setPlayerMoves ( [ ] ) ;
40
+ } ;
41
+
27
42
switch ( gameState ) {
28
43
case "cpu-turn" : {
29
44
return (
30
45
< >
31
46
< div style = { { display : "flex" , gap : 10 } } >
32
47
{ config . boxes . map ( ( box , index ) => (
33
- < Box
34
- key = { index }
35
- color = { box . color }
36
- onClick = { ( ) => {
37
- playNote ( box . frequency ) ;
38
- setPlayerMoves ( ( ) => {
39
- const newPlayerMoves = [ ...playerMoves , index ] ;
40
- const isSequenceCorrect = isPrefixCorrect (
41
- newPlayerMoves ,
42
- correctMoves ,
43
- ) ;
44
- if ( ! isSequenceCorrect ) {
45
- setGameState ( "game-over" ) ;
46
- return newPlayerMoves ;
47
- }
48
- if ( newPlayerMoves . length === correctMoves . length ) {
49
- setGameState ( "cpu-turn" ) ;
50
- return [ ] ;
51
- }
52
- setGameState ( "player-turn" ) ;
53
- return newPlayerMoves ;
54
- } ) ;
55
- } }
56
- />
48
+ < Box key = { index } color = { box . color } isDisabled />
57
49
) ) }
58
50
</ div >
59
51
< pre > Game State: { gameState } </ pre >
@@ -65,37 +57,8 @@ export function App() {
65
57
case "game-over" : {
66
58
return (
67
59
< >
68
- < div style = { { display : "flex" , gap : 10 } } >
69
- { config . boxes . map ( ( box , index ) => (
70
- < Box
71
- key = { index }
72
- color = { box . color }
73
- onClick = { ( ) => {
74
- playNote ( box . frequency ) ;
75
- setPlayerMoves ( ( ) => {
76
- const newPlayerMoves = [ ...playerMoves , index ] ;
77
- const isSequenceCorrect = isPrefixCorrect (
78
- newPlayerMoves ,
79
- correctMoves ,
80
- ) ;
81
- if ( ! isSequenceCorrect ) {
82
- setGameState ( "game-over" ) ;
83
- return newPlayerMoves ;
84
- }
85
- if ( newPlayerMoves . length === correctMoves . length ) {
86
- setGameState ( "cpu-turn" ) ;
87
- return [ ] ;
88
- }
89
- setGameState ( "player-turn" ) ;
90
- return newPlayerMoves ;
91
- } ) ;
92
- } }
93
- />
94
- ) ) }
95
- </ div >
96
- < pre > Game State: { gameState } </ pre >
97
- < pre > Player Moves: { JSON . stringify ( playerMoves , null , 2 ) } </ pre >
98
- < pre > Correct Moves: { JSON . stringify ( correctMoves , null , 2 ) } </ pre >
60
+ < h1 > Game Over ... Start again ...</ h1 >
61
+ < button onClick = { newGame } > New Game</ button >
99
62
</ >
100
63
) ;
101
64
}
@@ -129,14 +92,7 @@ export function App() {
129
92
case "pre-game" : {
130
93
return (
131
94
< div style = { { display : "flex" , gap : 10 } } >
132
- < button
133
- onClick = { ( ) => {
134
- setGameState ( "cpu-turn" ) ;
135
- setCorrectMoves ( ( prev ) => [ ...prev , randNum ( 4 ) ] ) ; // TODO: Add to cpu-turn state instead once created
136
- } }
137
- >
138
- Start Game
139
- </ button >
95
+ < button onClick = { newGame } > Start Game</ button >
140
96
Simon Game
141
97
</ div >
142
98
) ;
0 commit comments