Skip to content

Commit 7c26acb

Browse files
added Clear Box and clear board button (#60)
* check that all number used are different * once all the square is filled it will validate automatically whether the square is correct or not. * made the error text smaller * conflict resolve * conflict * added the clear box and clear board button
1 parent affed6c commit 7c26acb

File tree

2 files changed

+89
-30
lines changed

2 files changed

+89
-30
lines changed

src/pages/games/MagicSquares.js

Lines changed: 81 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from "react";
1+
import { useState} from "react";
22
import "../../styles/pages/games/MagicSquares.css";
33

44
export 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">

src/styles/pages/games/MagicSquares.css

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ input::-webkit-inner-spin-button {
9292
.each-col {
9393
display: flex;
9494
flex-direction: column;
95-
justify-content: space-between;
95+
gap: 10px;
9696

9797
}
9898

@@ -121,11 +121,17 @@ hr {
121121

122122
.game-error {
123123
color: crimson;
124-
font-size: 50px;
124+
font-size: 30px;
125125
}
126126

127127

128128
.game-outcome {
129129
color: indigo;
130130
font-size: 60px;
131+
}
132+
133+
134+
.flex-button{
135+
display: flex;
136+
131137
}

0 commit comments

Comments
 (0)