1- import { useState } from "react" ;
1+ import { useState } from "react" ;
22import "../../styles/pages/games/MagicSquares.css" ;
33
44export const MagicSquares = ( ) => {
@@ -38,6 +38,28 @@ export const MagicSquares = () => {
3838 } , 3000 ) ;
3939 } ;
4040
41+ const isNumberPresent = ( number ) => {
42+ for ( let i = 0 ; i < board . length ; i ++ ) {
43+ if ( board [ i ] . value === number ) {
44+ return true ;
45+ }
46+ }
47+ return false ;
48+ } ;
49+
50+ const autocheck = ( ) => {
51+ let allCellsFilled = true ;
52+ for ( let i = 0 ; i < 9 ; i ++ ) {
53+ if ( board [ i ] . value === "" ) {
54+ allCellsFilled = false ;
55+ break ;
56+ }
57+ }
58+ if ( allCellsFilled ) {
59+ checkFn ( ) ;
60+ }
61+ }
62+
4163 const checkFn = ( ) => {
4264 console . log ( "in check function" ) ;
4365 let flag_empty = 0 ;
@@ -163,15 +185,27 @@ export const MagicSquares = () => {
163185 ) ;
164186 } ) }
165187 </ div >
166- < div
167- className = "button"
168- onClick = { ( ) => {
169- if ( ! winFlag ) {
170- checkFn ( ) ;
171- }
172- } }
173- >
174- Check
188+ < div className = "flex-button" >
189+ < div
190+ className = "button"
191+ onClick = { ( ) => {
192+ if ( ! winFlag ) {
193+ checkFn ( ) ;
194+ }
195+ } }
196+ >
197+ Check
198+ </ div >
199+ < div className = "button" onClick = { ( ) => {
200+
201+ let tempBoard = [ ...board ] ;
202+ for ( let i = 0 ; i < 9 ; i ++ ) {
203+ tempBoard [ i ] . value = "" ;
204+ }
205+ setBoard ( tempBoard ) ;
206+ } } >
207+ Clear Board
208+ </ div >
175209 </ div >
176210 </ div >
177211 < div className = "each-col" >
@@ -184,26 +218,45 @@ export const MagicSquares = () => {
184218 value = { cellInputValue }
185219 onChange = { ( e ) => setCellInputValue ( e . target . value ) }
186220 />
187- < div
188- className = "button"
189- onClick = { ( ) => {
190- if ( ! winFlag ) {
191- if ( focusCell === "" ) {
192- callError ( "No cell selected!" ) ;
193- } else if ( cellInputValue === "" ) {
194- callError ( "Nothing to insert!" ) ;
195- } else {
196- let tempBoard = board ;
197- if ( focusCell !== "" )
198- tempBoard [ focusCell ] . value = cellInputValue ;
199- // console.log("focus on", focusCell, tempBoard);
200- setBoard ( tempBoard ) ;
201- setFocusCell ( "" ) ;
221+ < div className = "flex-button" >
222+ < div
223+ className = "button"
224+ onClick = { ( ) => {
225+ if ( ! winFlag ) {
226+ if ( focusCell === "" ) {
227+ callError ( "No cell selected!" ) ;
228+ } else if ( cellInputValue === "" ) {
229+ callError ( "Nothing to insert!" ) ;
230+ } else if ( isNumberPresent ( cellInputValue ) ) {
231+ callError ( "Number already exists in the board!!" ) ;
232+ } else {
233+ let tempBoard = board ;
234+ if ( focusCell !== "" )
235+ tempBoard [ focusCell ] . value = cellInputValue ;
236+ // console.log("focus on", focusCell, tempBoard);
237+ setBoard ( tempBoard ) ;
238+ setFocusCell ( "" ) ;
239+ autocheck ( ) ;
240+ }
202241 }
242+ } }
243+ >
244+ Insert
245+ </ div >
246+ < div className = "button" onClick = { ( ) => {
247+ if ( focusCell === "" ) {
248+ callError ( "No cell selected!" ) ;
249+ } else {
250+ let tempBoard = board ;
251+ if ( focusCell !== "" )
252+ tempBoard [ focusCell ] . value = cellInputValue ;
253+ setBoard ( tempBoard ) ;
254+ setFocusCell ( "" ) ;
255+ autocheck ( ) ;
203256 }
204- } }
205- >
206- Insert
257+ } } >
258+ clear
259+ </ div >
207260 </ div >
208261 < hr />
209262 < div className = "rules" >
0 commit comments