Skip to content

Commit

Permalink
trigger rainbow mode on sudoku win
Browse files Browse the repository at this point in the history
  • Loading branch information
ayan4m1 committed Dec 26, 2023
1 parent e79aaf9 commit c2c2fed
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
47 changes: 33 additions & 14 deletions src/components/sudokuBoard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PropTypes from 'prop-types';
import { hsl } from 'd3-color';
import { chunk } from 'lodash-es';
import { getSudoku } from 'sudoku-gen';
import useLocalStorageState from 'use-local-storage-state';
Expand All @@ -22,6 +23,7 @@ import {

import SudokuCell from 'components/sudokuCell';
import { checkSolution, getInvalids } from 'utils/sudoku';
import useRainbow from 'hooks/useRainbow';

export default function SudokuBoard() {
const [solved, setSolved] = useState(false);
Expand Down Expand Up @@ -81,9 +83,17 @@ export default function SudokuBoard() {
setValues(savedState.values);
}, [savedState]);
const handleClear = useCallback(() => setSavedState(null), []);
const { color: animationColor, start, stop } = useRainbow(false, false);

useEffect(() => {
setSolved(checkSolution(cells, values, puzzle.solution));
const solved = checkSolution(cells, values, puzzle.solution);

if (solved) {
setSolved(solved);
start();
} else {
stop();
}
}, [cells, values, puzzle]);

return (
Expand Down Expand Up @@ -123,19 +133,28 @@ export default function SudokuBoard() {
</Row>
{cells.map((row, rowIdx) => (
<Row key={rowIdx} className="d-flex justify-content-center">
{row.map((value, colIdx) => (
<SudokuCell
row={rowIdx}
column={colIdx}
key={colIdx}
value={!value ? values[rowIdx][colIdx] : value}
unknown={!value}
active={activeCell[0] === rowIdx && activeCell[1] === colIdx}
valid={invalids[rowIdx][colIdx]}
onClick={handleClick}
onChange={handleChange}
/>
))}
{row.map((value, colIdx) => {
const cellColor = hsl(solved ? animationColor : '#ffffff');

if (solved) {
cellColor.h += (rowIdx * 9 + colIdx) * 5;
}

return (
<SudokuCell
row={rowIdx}
column={colIdx}
key={colIdx}
value={!value ? values[rowIdx][colIdx] : value}
unknown={!value}
active={activeCell[0] === rowIdx && activeCell[1] === colIdx}
valid={invalids[rowIdx][colIdx]}
onClick={handleClick}
onChange={handleChange}
bg={cellColor.formatHex()}
/>
);
})}
</Row>
))}
</Container>
Expand Down
5 changes: 4 additions & 1 deletion src/components/sudokuCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default function SudokuCell({
row,
column,
value,
bg = '#ffffff',
unknown = false,
active = false,
valid = true,
Expand Down Expand Up @@ -65,8 +66,9 @@ export default function SudokuCell({
'text-dark',
column > 0 && column % 3 === 2 && 'border-end',
row > 0 && row % 3 === 2 && 'border-bottom',
valid ? 'bg-white' : 'bg-danger'
!valid && 'bg-danger'
)}
style={{ backgroundColor: bg }}
>
{unknown && active ? (
<Form.Control
Expand All @@ -88,6 +90,7 @@ SudokuCell.propTypes = {
row: PropTypes.number.isRequired,
column: PropTypes.number.isRequired,
value: PropTypes.number,
bg: PropTypes.string,
unknown: PropTypes.bool,
active: PropTypes.bool,
valid: PropTypes.bool,
Expand Down
7 changes: 6 additions & 1 deletion src/hooks/useRainbow.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useEffect, useMemo, useRef, useState, useCallback } from 'react';

export default function useRainbow(
run = true,
loop = true,
startColor = '#ff0000',
endColor = '#0000ff',
timeDilation = 2000
Expand All @@ -18,7 +19,11 @@ export default function useRainbow(
const stop = useCallback(() => setRunning(false), []);

const animate = (time) => {
setColor(interpolator(Math.abs(Math.sin(time / timeDilation))));
setColor(
interpolator(
loop ? Math.abs(Math.sin(time / timeDilation)) : time / timeDilation
)
);

if (running) {
requestId.current = requestAnimationFrame(animate);
Expand Down

0 comments on commit c2c2fed

Please sign in to comment.