Skip to content

Commit 43fcacd

Browse files
committed
add difficulty selection to new button for sudoku
1 parent 46cd1c0 commit 43fcacd

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

src/components/sudokuBoard.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import {
1010
Row,
1111
Col,
1212
ButtonGroup,
13-
Button
13+
Button,
14+
Dropdown,
15+
DropdownButton
1416
} from 'react-bootstrap';
15-
import { useCallback, useEffect, useMemo, useState } from 'react';
17+
import { Fragment, useCallback, useEffect, useMemo, useState } from 'react';
1618
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
1719
import {
1820
faRecycle,
@@ -22,7 +24,7 @@ import {
2224
} from '@fortawesome/free-solid-svg-icons';
2325

2426
import SudokuCell from 'components/sudokuCell';
25-
import { checkSolution, getInvalids } from 'utils/sudoku';
27+
import { checkSolution, getInvalids, difficulties } from 'utils/sudoku';
2628
import useRainbow from 'hooks/useRainbow';
2729

2830
export default function SudokuBoard() {
@@ -65,7 +67,10 @@ export default function SudokuBoard() {
6567
}),
6668
[]
6769
);
68-
const handleNew = useCallback(() => setPuzzle(getSudoku('easy')), []);
70+
const handleNew = useCallback(
71+
(difficulty) => setPuzzle(getSudoku(difficulty)),
72+
[]
73+
);
6974
const handleSave = useCallback(
7075
() =>
7176
setSavedState({
@@ -107,9 +112,23 @@ export default function SudokuBoard() {
107112
<Row className="mb-2">
108113
<Col className="d-flex justify-content-center g-0">
109114
<ButtonGroup className="w-100">
110-
<Button onClick={handleNew}>
111-
<FontAwesomeIcon icon={faRecycle} /> New
112-
</Button>
115+
<DropdownButton
116+
as={ButtonGroup}
117+
title={
118+
<Fragment>
119+
<FontAwesomeIcon icon={faRecycle} /> New
120+
</Fragment>
121+
}
122+
>
123+
{difficulties.map((difficulty) => (
124+
<Dropdown.Item
125+
key={difficulty}
126+
onClick={() => handleNew(difficulty.toLowerCase())}
127+
>
128+
{difficulty}
129+
</Dropdown.Item>
130+
))}
131+
</DropdownButton>
113132
<Button onClick={handleSave}>
114133
<FontAwesomeIcon icon={faFloppyDisk} /> Save
115134
</Button>
@@ -134,9 +153,10 @@ export default function SudokuBoard() {
134153
{cells.map((row, rowIdx) => (
135154
<Row key={rowIdx} className="d-flex justify-content-center">
136155
{row.map((value, colIdx) => {
137-
const cellColor = hsl(solved ? animationColor : '#ffffff');
156+
let cellColor = null;
138157

139158
if (solved) {
159+
cellColor = hsl(animationColor);
140160
cellColor.h += (rowIdx * 9 + colIdx) * 5;
141161
}
142162

@@ -151,7 +171,7 @@ export default function SudokuBoard() {
151171
valid={invalids[rowIdx][colIdx]}
152172
onClick={handleClick}
153173
onChange={handleChange}
154-
bg={cellColor.formatHex()}
174+
bg={cellColor?.formatHex?.()}
155175
/>
156176
);
157177
})}

src/components/sudokuCell.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default function SudokuCell({
77
row,
88
column,
99
value,
10-
bg = '#ffffff',
10+
bg,
1111
unknown = false,
1212
active = false,
1313
valid = true,
@@ -54,11 +54,7 @@ export default function SudokuCell({
5454

5555
return (
5656
<Col
57-
onClick={() => {
58-
if (onClick) {
59-
onClick(row, column);
60-
}
61-
}}
57+
onClick={() => onClick(row, column)}
6258
className={classNames(
6359
'sudoku-cell',
6460
'border-dark',
@@ -68,7 +64,7 @@ export default function SudokuCell({
6864
row > 0 && row % 3 === 2 && 'border-bottom',
6965
!valid && 'bg-danger'
7066
)}
71-
style={{ backgroundColor: bg }}
67+
style={bg && { backgroundColor: bg }}
7268
>
7369
{unknown && active ? (
7470
<Form.Control

src/components/sudokuCell.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ $cell-lg: $cell-xs * 1.75;
88
justify-content: center;
99
align-items: center;
1010
padding: 0;
11+
background-color: white;
1112
height: $cell-xs;
1213
min-width: $cell-xs;
1314
max-width: $cell-xs;

src/utils/sudoku.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export const difficulties = ['Easy', 'Medium', 'Hard', 'Expert'];
2+
13
export function checkSolution(puzzle, values, solution) {
24
if (!puzzle.length) {
35
return false;

0 commit comments

Comments
 (0)