Skip to content

Commit

Permalink
Merge pull request #17 from muffymn/main
Browse files Browse the repository at this point in the history
Modify timer start from 0:00 and alert with time
  • Loading branch information
jiajunxd authored Aug 16, 2023
2 parents 37a561c + fbeb7a6 commit a5d651a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
59 changes: 34 additions & 25 deletions sudoku-app/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,29 @@ function App() {
const [grid, setGrid] = useState(new Array(9).fill().map(() => new Array(9).fill('')));
const [givenGrid, setGivenGrid] = useState(new Array(9).fill().map(() => new Array(9).fill('')));
const [errorsGrid, setErrorsGrid] = useState(new Array(9).fill().map(() => new Array(9).fill(false)));
const [errorsVisibility, setErrorsVisibility] = useState(false)
const [errorsVisibility, setErrorsVisibility] = useState(false);
const [solution, setSolution] = useState([]);
const [remainingTime, setRemainingTime] = useState(0);
const [timerId, setTimerId] = useState(null)
const [startTime, setStartTime] = useState(null);
const [elapsedTime, setElapsedTime] = useState(0);

useEffect(() => {
if (remainingTime > 0) {
if (startTime !== null) {
const timer = setInterval(() => {
setRemainingTime(prevTime => prevTime - 1);
const currentTime = Date.now();
const elapsedSeconds = Math.floor((currentTime - startTime) / 1000);
setElapsedTime(elapsedSeconds);
}, 1000);
setTimerId(timer)
return () => clearInterval(timer);
}
}, [remainingTime]);
}, [startTime]);

const startTimer = () => {
setStartTime(Date.now());
};

const isFullyFilled = (grid) => {
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
if (grid[row][col] === "") return false;
}
}
return true;
}
const stopTimer = () => {
setStartTime(null);
};

const handleGridChange = (row, col, value) => {
if (!/^[1-9\b]*$/.test(value)) {
Expand All @@ -52,14 +51,30 @@ function App() {

if (isFullyFilled(newGrid)) {
if (sudokuChecker(newGrid.map(row => row.slice()))) {
alert("Sudoku solved!");
stopTimer()
const totalSeconds = elapsedTime;
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
const formattedTime = `${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
alert(`Sudoku solved in ${formattedTime}`);
stopTimer();
} else {
alert("Sudoku wrong!");
}
}
};


const isFullyFilled = (grid) => {
for (let row = 0; row < 9; row++) {
for (let col = 0; col < 9; col++) {
if (grid[row][col] === "") return false;
}
}
return true;
}



const handleDifficultySelection = (difficulty) => {
let holes = 0;

Expand Down Expand Up @@ -101,13 +116,7 @@ function App() {
setErrorsVisibility(!errorsVisibility)
}

const startTimer = (minutes) => {
setRemainingTime(minutes * 60);
};

const stopTimer = () => {
clearInterval(timerId);
};


return (
<div className="App">
Expand All @@ -126,7 +135,7 @@ function App() {
handleDifficultySelection(difficulty)}
solveForMe={solveForMe}
startTimer={startTimer}
remainingTime={remainingTime}
remainingTime={elapsedTime}
toggleErrors={toggleErrors}
errorsVisibility={errorsVisibility}
/>
Expand Down
2 changes: 1 addition & 1 deletion sudoku-app/src/Buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Buttons({ startTimer, handleDifficultySelection, solveForMe, remainingT
<button className="button" onClick={solveForMe}>Solve for Me</button>
<button className="button" onClick={toggleErrors}>{errorsVisibility ? "Hide Errors" : "Show Errors"}</button>
</div>
<div className="timer">Time remaining: {Math.floor(remainingTime / 60)}:{(remainingTime % 60).toString().padStart(2, '0')}</div>
<div className="timer">Time elapsed: {Math.floor(remainingTime / 60)}:{(remainingTime % 60).toString().padStart(2, '0')}</div>

</div>
);
Expand Down

0 comments on commit a5d651a

Please sign in to comment.