Skip to content

Setting to highlight cells with an incorrect digit #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/modal/modal-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ export default function ModalSettings({modalHandler, modalState}) {
allSettings={allSettings}
setSetting={setSetting}
/>
<CheckBoxSetting
name={SETTINGS.highlightErrors}
text="Highlight errors"
allSettings={allSettings}
setSetting={setSetting}
/>
<CheckBoxSetting
name={SETTINGS.autocleanPencilmarks}
text="Auto-clean pencil marks"
Expand Down
10 changes: 8 additions & 2 deletions src/lib/sudoku-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export const SETTINGS = {
outlineSelection: "outline-selection",
highlightMatches: "highlight-matches",
highlightConflicts: "highlight-conflicts",
highlightErrors: "highlight-errors",
autocleanPencilmarks: "autoclean-pencilmarks",
flipNumericKeys: "flip-numeric-keys",
playVictoryAnimation: "play-victory-animation",
Expand Down Expand Up @@ -149,6 +150,7 @@ export const modelHelpers = {
[SETTINGS.outlineSelection]: false,
[SETTINGS.highlightMatches]: true,
[SETTINGS.highlightConflicts]: true,
[SETTINGS.highlightErrors]: false,
[SETTINGS.autocleanPencilmarks]: true,
[SETTINGS.flipNumericKeys]: false,
[SETTINGS.playVictoryAnimation]: true,
Expand Down Expand Up @@ -1415,7 +1417,8 @@ export const modelHelpers = {
},

applyErrorHighlights: (grid, errorAtIndex = {}) => {
if (!modelHelpers.getSetting(grid, SETTINGS.highlightConflicts)) {
if (!modelHelpers.getSetting(grid, SETTINGS.highlightConflicts)
&& !modelHelpers.getSetting(grid, SETTINGS.highlightErrors)) {
return grid;
}
const cells = grid.get('cells').map((c) => {
Expand Down Expand Up @@ -1607,7 +1610,10 @@ export const modelHelpers = {

checkCompletedDigits: (grid) => {
const digits = grid.get('cells').map(c => c.get('digit')).join('');
const result = modelHelpers.checkDigits(digits);
const finalDigits = grid.get('finalDigits');
const result = modelHelpers.getSetting(grid, SETTINGS.highlightErrors)
? modelHelpers.checkDigits(digits, finalDigits)
: modelHelpers.checkDigits(digits);
grid = grid.set('completedDigits', result.completedDigits);
if (result.isSolved && !grid.get('endTime')) {
return modelHelpers.setGridSolved(grid);
Expand Down
33 changes: 33 additions & 0 deletions src/lib/sudoku-model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,39 @@ test('no highlight conflicts', () => {
});
});

test('set highlight errors', () => {
let grid = newSudokuModel({initialDigits: initialDigitsComplete});
const settings = grid.get('settings');
grid = grid.set('settings', { ...settings, [SETTINGS.highlightErrors]: true});

expect(grid.get('currentSnapshot')).toBe('');
grid = modelHelpers.applySelectionOp(grid, 'setSelection', 8);
grid = modelHelpers.updateSelectedCells(grid, 'setDigit', '9');

expect(grid.get('currentSnapshot')).toBe('19D9');
expect(grid.get('matchDigit')).toBe('9');

let c8 = grid.get('cells').get(8);
expect(c8.get('digit')).toBe('9');
expect(c8.get('snapshot')).toBe('D9');
expect(c8.get('errorMessage')).toBe('Incorrect digit');
expect(c8.get('isGiven')).toBe(false);
expect(c8.get('isSelected')).toBe(true);

// clear the cell and expect it should have no error
grid = modelHelpers.applySelectionOp(grid, 'setSelection', 8);
grid = modelHelpers.updateSelectedCells(grid, 'clearCell');

expect(grid.get('currentSnapshot')).toBe('');

c8 = grid.get('cells').get(8);
expect(c8.get('digit')).toBe('0');
expect(c8.get('snapshot')).toBe('');
expect(c8.get('errorMessage')).toBe(undefined);
expect(c8.get('isGiven')).toBe(false);
expect(c8.get('isSelected')).toBe(true);
});

test('set cell color', () => {
let grid = newSudokuModel({initialDigits: initialDigitsPartial, skipCheck: true});

Expand Down