Skip to content

Commit

Permalink
add difficulty selection to new button for sudoku
Browse files Browse the repository at this point in the history
  • Loading branch information
ayan4m1 committed Dec 26, 2023
1 parent 46cd1c0 commit 43fcacd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 16 deletions.
38 changes: 29 additions & 9 deletions src/components/sudokuBoard.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import {
Row,
Col,
ButtonGroup,
Button
Button,
Dropdown,
DropdownButton
} from 'react-bootstrap';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { Fragment, useCallback, useEffect, useMemo, useState } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
faRecycle,
Expand All @@ -22,7 +24,7 @@ import {
} from '@fortawesome/free-solid-svg-icons';

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

export default function SudokuBoard() {
Expand Down Expand Up @@ -65,7 +67,10 @@ export default function SudokuBoard() {
}),
[]
);
const handleNew = useCallback(() => setPuzzle(getSudoku('easy')), []);
const handleNew = useCallback(
(difficulty) => setPuzzle(getSudoku(difficulty)),
[]
);
const handleSave = useCallback(
() =>
setSavedState({
Expand Down Expand Up @@ -107,9 +112,23 @@ export default function SudokuBoard() {
<Row className="mb-2">
<Col className="d-flex justify-content-center g-0">
<ButtonGroup className="w-100">
<Button onClick={handleNew}>
<FontAwesomeIcon icon={faRecycle} /> New
</Button>
<DropdownButton
as={ButtonGroup}
title={
<Fragment>
<FontAwesomeIcon icon={faRecycle} /> New
</Fragment>
}
>
{difficulties.map((difficulty) => (
<Dropdown.Item
key={difficulty}
onClick={() => handleNew(difficulty.toLowerCase())}
>
{difficulty}
</Dropdown.Item>
))}
</DropdownButton>
<Button onClick={handleSave}>
<FontAwesomeIcon icon={faFloppyDisk} /> Save
</Button>
Expand All @@ -134,9 +153,10 @@ export default function SudokuBoard() {
{cells.map((row, rowIdx) => (
<Row key={rowIdx} className="d-flex justify-content-center">
{row.map((value, colIdx) => {
const cellColor = hsl(solved ? animationColor : '#ffffff');
let cellColor = null;

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

Expand All @@ -151,7 +171,7 @@ export default function SudokuBoard() {
valid={invalids[rowIdx][colIdx]}
onClick={handleClick}
onChange={handleChange}
bg={cellColor.formatHex()}
bg={cellColor?.formatHex?.()}
/>
);
})}
Expand Down
10 changes: 3 additions & 7 deletions src/components/sudokuCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function SudokuCell({
row,
column,
value,
bg = '#ffffff',
bg,
unknown = false,
active = false,
valid = true,
Expand Down Expand Up @@ -54,11 +54,7 @@ export default function SudokuCell({

return (
<Col
onClick={() => {
if (onClick) {
onClick(row, column);
}
}}
onClick={() => onClick(row, column)}
className={classNames(
'sudoku-cell',
'border-dark',
Expand All @@ -68,7 +64,7 @@ export default function SudokuCell({
row > 0 && row % 3 === 2 && 'border-bottom',
!valid && 'bg-danger'
)}
style={{ backgroundColor: bg }}
style={bg && { backgroundColor: bg }}
>
{unknown && active ? (
<Form.Control
Expand Down
1 change: 1 addition & 0 deletions src/components/sudokuCell.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ $cell-lg: $cell-xs * 1.75;
justify-content: center;
align-items: center;
padding: 0;
background-color: white;
height: $cell-xs;
min-width: $cell-xs;
max-width: $cell-xs;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/sudoku.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const difficulties = ['Easy', 'Medium', 'Hard', 'Expert'];

export function checkSolution(puzzle, values, solution) {
if (!puzzle.length) {
return false;
Expand Down

0 comments on commit 43fcacd

Please sign in to comment.