@@ -44,13 +44,13 @@ const SwitchGame = () => {
4444 // console.log(switchState);
4545 // }, [switchState]);
4646
47- const handleToggle = ( switchName , newValue ) => {
48- setSwitchState ( ( prevState ) =>
49- prevState . map ( ( item ) =>
50- item . switchName === switchName ? { ...item , isOn : newValue } : item
51- )
52- ) ;
53- } ;
47+ // const handleToggle = (switchName, newValue) => {
48+ // setSwitchState((prevState) =>
49+ // prevState.map((item) =>
50+ // item.switchName === switchName ? { ...item, isOn: newValue } : item
51+ // )
52+ // );
53+ // };
5454
5555 const handleMix = ( ) => {
5656 setGameState ( "mixing" ) ;
@@ -73,85 +73,108 @@ const SwitchGame = () => {
7373 const handleSolve = ( ) => {
7474 console . log ( "solve checkpoint" ) ;
7575 let sequence = solve ( switchState ) ;
76- let delayGap = 0 ;
77- sequence . forEach ( ( switchName ) => {
78- delayGap ++ ;
76+ // let delayGap = 0;
77+ // sequence.forEach((switchName) => {
78+ // delayGap++;
79+ // setTimeout(() => {
80+ // gameLogic(switchName);
81+ // }, delayGap * 800);
82+ // });
83+ for ( let i = 0 ; i < sequence . length ; i ++ ) {
7984 setTimeout ( ( ) => {
80- gameLogic ( switchName ) ;
81- } , delayGap * 600 ) ;
82- } ) ;
83- console . log ( sequence ) ;
85+ const selectedSwitch = sequence [ i ] ;
86+ console . log ( selectedSwitch ) ;
87+ gameLogic ( selectedSwitch ) ;
88+ } , i * 600 ) ;
89+ }
90+ //console.log(sequence);
8491 } ;
8592
8693 const gameLogic = ( switchName ) => {
87- const switchIndex = parseInt ( switchName . replace ( "switch" , "" ) ) ;
94+ // const switchIndex = parseInt(switchName.replace("switch", ""));
8895 // console.log(`You clicked switch ${switchIndex}`);
89- switch ( switchIndex ) {
90- case 0 :
96+ switch ( switchName ) {
97+ case "switch0" :
9198 setSwitchState ( ( prevState ) =>
9299 prevState . map ( ( item ) =>
93- item . switchName === "switch1" ? { ...item , isOn : ! item . isOn } : item
100+ item . switchName === "switch1" || item . switchName === "switch0"
101+ ? { ...item , isOn : ! item . isOn }
102+ : item
94103 )
95104 ) ;
96105 break ;
97- case 1 :
106+ case "switch1" :
98107 setSwitchState ( ( prevState ) =>
99108 prevState . map ( ( item ) =>
100- item . switchName === "switch0" || item . switchName === "switch2"
109+ item . switchName === "switch0" ||
110+ item . switchName === "switch1" ||
111+ item . switchName === "switch2"
101112 ? { ...item , isOn : ! item . isOn }
102113 : item
103114 )
104115 ) ;
105116 break ;
106- case 2 :
117+ case "switch2" :
107118 setSwitchState ( ( prevState ) =>
108119 prevState . map ( ( item ) =>
109- item . switchName === "switch1" || item . switchName === "switch3"
120+ item . switchName === "switch1" ||
121+ item . switchName === "switch2" ||
122+ item . switchName === "switch3"
110123 ? { ...item , isOn : ! item . isOn }
111124 : item
112125 )
113126 ) ;
114127 break ;
115- case 3 :
128+ case "switch3" :
116129 setSwitchState ( ( prevState ) =>
117130 prevState . map ( ( item ) =>
118- item . switchName === "switch2" || item . switchName === "switch4"
131+ item . switchName === "switch2" ||
132+ item . switchName === "switch3" ||
133+ item . switchName === "switch4"
119134 ? { ...item , isOn : ! item . isOn }
120135 : item
121136 )
122137 ) ;
123138 break ;
124- case 4 :
139+ case "switch4" :
125140 setSwitchState ( ( prevState ) =>
126141 prevState . map ( ( item ) =>
127- item . switchName === "switch3" || item . switchName === "switch5"
142+ item . switchName === "switch3" ||
143+ item . switchName === "switch4" ||
144+ item . switchName === "switch5"
128145 ? { ...item , isOn : ! item . isOn }
129146 : item
130147 )
131148 ) ;
132149 break ;
133- case 5 :
150+ case "switch5" :
134151 setSwitchState ( ( prevState ) =>
135152 prevState . map ( ( item ) =>
136- item . switchName === "switch4" || item . switchName === "switch6"
153+ item . switchName === "switch4" ||
154+ item . switchName === "switch5" ||
155+ item . switchName === "switch6"
137156 ? { ...item , isOn : ! item . isOn }
138157 : item
139158 )
140159 ) ;
141160 break ;
142- case 6 :
161+ case "switch6" :
143162 setSwitchState ( ( prevState ) =>
144163 prevState . map ( ( item ) =>
145- item . switchName === "switch5" || item . switchName === "switch7"
164+ item . switchName === "switch5" ||
165+ item . switchName === "switch6" ||
166+ item . switchName === "switch7"
146167 ? { ...item , isOn : ! item . isOn }
147168 : item
148169 )
149170 ) ;
150171 break ;
151- case 7 :
172+ case "switch7" :
152173 setSwitchState ( ( prevState ) =>
153174 prevState . map ( ( item ) =>
154- item . switchName === "switch6" ? { ...item , isOn : ! item . isOn } : item
175+ item . switchName === "switch6" || item . switchName === "switch7"
176+ ? { ...item , isOn : ! item . isOn }
177+ : item
155178 )
156179 ) ;
157180 break ;
@@ -193,8 +216,8 @@ const SwitchGame = () => {
193216 disabled = { gameState === "mixing" || gameState === "solving" }
194217 type = "checkbox"
195218 checked = { switchItem . isOn }
196- onChange = { ( e ) => {
197- handleToggle ( switchItem . switchName , e . target . checked ) ;
219+ onChange = { ( ) => {
220+ // handleToggle(switchItem.switchName, e.target.checked);
198221 gameLogic ( switchItem . switchName ) ;
199222 } }
200223 />
@@ -224,7 +247,7 @@ const SwitchGame = () => {
224247 } }
225248 >
226249 { ( ( ) => {
227- console . log ( gameState ) ;
250+ // console.log(gameState);
228251 switch ( gameState ) {
229252 case "solved" :
230253 return "Mix it" ;
0 commit comments