From e2e5344fed1edb46c42974c5642067c472922451 Mon Sep 17 00:00:00 2001 From: Victoria A <52001888+adjeiv@users.noreply.github.com> Date: Wed, 25 Oct 2023 22:55:21 +0100 Subject: [PATCH 1/3] Add support for new string definition including candidate information --- src/lib/sudoku-model.js | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/lib/sudoku-model.js b/src/lib/sudoku-model.js index 277eee9..e4087bb 100644 --- a/src/lib/sudoku-model.js +++ b/src/lib/sudoku-model.js @@ -57,6 +57,7 @@ const difficultyLevels = [ ]; const MAX_SAVED_PUZZLES = 5; +const ALL_CANDIDATES_SUM = 1022; const emptySet = Set(); const charCodeOne = '1'.charCodeAt(0); @@ -100,7 +101,7 @@ export function newSudokuModel({initialDigits, difficultyLevel, onPuzzleStateCha if (initialDigits.length < 81) { initialDigits = expandPuzzleDigits(initialDigits); } - initialDigits = initialDigits.replace(/\D/g, '') + initialDigits = initialDigits.replace(/\D/g, '') // clues const initialError = skipCheck ? undefined : modelHelpers.initialErrorCheck(initialDigits); const mode = initialError ? 'enter' : 'solve'; const settings = modelHelpers.loadSettings(); @@ -951,7 +952,37 @@ export const modelHelpers = { return grid.set('modalState', { modalType: MODAL_TYPE_SOLVER, initialDigits: grid.get('initialDigits'), - allDigits: grid.get('cells').map(c => c.get('digit')).join(''), + allDigits: grid.get('cells') + .map(c => { + const encode_as_str = (encoded_cell) => { + let encoded_str = encoded_cell.toString(32); + if (encoded_str.length < 2) { + encoded_str = '0' + encoded_str; + } + return encoded_str; + }; + + const digit = parseInt(c.get('digit')); + if (c.get('isGiven')) { + // increment to set 'clue' flag + const encoded_cell = Math.pow(2, digit) + 1; + return encode_as_str(encoded_cell); + } + + // cell based on user input + if (digit === 0) { + const candidates = c.get('innerPencils').toArray(); + const encoded_cell = candidates.length !== 0 + ? candidates.map(c => Math.pow(2, parseInt(c))) + .reduce((a, b) => a + b, 0) + : ALL_CANDIDATES_SUM; + return encode_as_str(encoded_cell); + } else { + // return digit as is + const encoded_cell = Math.pow(2, digit); + return encode_as_str(encoded_cell); + } + }).join(''), passProgressSetting, escapeAction: 'close', }); From 262d33c6b642d5115b38978be3e58fc5c09cb1ec Mon Sep 17 00:00:00 2001 From: Victoria A <52001888+adjeiv@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:53:26 +0100 Subject: [PATCH 2/3] Add test for new string definition --- src/lib/sudoku-model.test.js | 97 ++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/src/lib/sudoku-model.test.js b/src/lib/sudoku-model.test.js index ca77e1a..3c3a1a5 100644 --- a/src/lib/sudoku-model.test.js +++ b/src/lib/sudoku-model.test.js @@ -1039,3 +1039,100 @@ test('check digits', () => { }, }); }); + +test('check modal solver', () => { + let grid = newSudokuModel({initialDigits: initialDigitsComplete, skipCheck: true}); + let result = modelHelpers.showSolverModal(grid).get('modalState'); + expect(result['allDigits']).toBe( + 'vuvuvuvuvu030509vu' + + '030509vuvu81vu0hvu' + + '81vu0hvuvu412111vu' + + '412111vuvuvuvuvuvu' + + 'vuvuvuvuvuvuvuvuvu' + + 'vuvuvuvuvuvu030509' + + 'vu030509vuvu81vu0h' + + 'vu81vu0hvuvu412111' + + 'vu412111vuvuvuvuvu' + ); + + grid = modelHelpers.showCalculatedCandidates(grid); + expect(grid.get('currentSnapshot')).toBe( + '11N569,12N59,13N79,14N69,15N4569,19N789,' + + '24N69,25N569,27N9,29N79,' + + '32N9,34N29,35N239,39N19,' + + '44N1289,45N123489,46N2349,47N49,48N89,49N89,' + + '51N2349,52N349,53N189,54N126789,55N123456789,56N234569,57N459,58N789,59N6789,' + + '61N49,62N49,63N89,64N6789,65N456789,66N4569,' + + '71N59,75N679,76N69,78N9,' + + '81N39,83N9,85N129,86N29,' + + '91N349,95N1289,96N29,97N39,98N19,99N129' + ); + result = modelHelpers.showSolverModal(grid).get('modalState'); + expect(result['allDigits']).toBe( + 'j0h0k0i0jg030509s0' + + '030509i0j081g00hk0' + + '81g00hg4gc412111g2' + + '412111o6ougsggo0o0' + + 'gsgoo2u6vujshgs0u0' + + 'ggggo0u0vgjg030509' + + 'h0030509m0i081g00h' + + 'g881g00hg6g4412111' + + 'go412111o6g4g8g2g6' + ); + + grid = modelHelpers.applySelectionOp(grid, 'setSelection', 72); + grid = modelHelpers.updateSelectedCells(grid, 'setDigit', 4); + grid = modelHelpers.applySelectionOp(grid, 'setSelection', 34); + grid = modelHelpers.updateSelectedCells(grid, 'setDigit', 8); + expect(grid.get('currentSnapshot')).toBe( + '11N569,12N59,13N79,14N69,15N4569,19N789,' + + '24N69,25N569,27N9,29N79,' + + '32N9,34N29,35N239,39N19,' + + '44N129,45N12349,46N2349,47N49,48D8,49N9,' + + '51N239,52N349,53N189,54N126789,55N123456789,56N234569,57N459,58N79,59N679,' + + '61N9,62N49,63N89,64N6789,65N456789,66N4569,' + + '71N59,75N679,76N69,78N9,' + + '81N39,83N9,85N129,86N29,' + + '91D4,95N1289,96N29,97N39,98N19,99N129' + ); + result = modelHelpers.showSolverModal(grid).get('modalState'); + expect(result['allDigits']).toBe( + 'j0h0k0i0jg030509s0' + + '030509i0j081g00hk0' + + '81g00hg4gc412111g2' + + '412111g6gugsgg80g0' + + 'gcgoo2u6vujshgk0m0' + + 'g0ggo0u0vgjg030509' + + 'h0030509m0i081g00h' + + 'g881g00hg6g4412111' + + '0g412111o6g4g8g2g6' + ); + + grid = modelHelpers.applySelectionOp(grid, 'setSelection', 13); + grid = modelHelpers.updateSelectedCells(grid, 'clearCell'); + grid = modelHelpers.applySelectionOp(grid, 'setSelection', 50); + grid = modelHelpers.updateSelectedCells(grid, 'clearCell'); + expect(grid.get('currentSnapshot')).toBe( + '11N569,12N59,13N79,14N69,15N4569,19N789,' + + '24N69,27N9,29N79,' + + '32N9,34N29,35N239,39N19,' + + '44N129,45N12349,46N2349,47N49,48D8,49N9,' + + '51N239,52N349,53N189,54N126789,55N123456789,56N234569,57N459,58N79,59N679,' + + '61N9,62N49,63N89,64N6789,65N456789,' + + '71N59,75N679,76N69,78N9,' + + '81N39,83N9,85N129,86N29,' + + '91D4,95N1289,96N29,97N39,98N19,99N129' + ); + result = modelHelpers.showSolverModal(grid).get('modalState'); + expect(result['allDigits']).toBe( + 'j0h0k0i0jg030509s0' + + '030509i0vu81g00hk0' + + '81g00hg4gc412111g2' + + '412111g6gugsgg80g0' + + 'gcgoo2u6vujshgk0m0' + + 'g0ggo0u0vgvu030509' + + 'h0030509m0i081g00h' + + 'g881g00hg6g4412111' + + '0g412111o6g4g8g2g6' + ); +}); From 5e2874f2bf288fc68bcc1992bbb4227b81ca4a23 Mon Sep 17 00:00:00 2001 From: Victoria A <52001888+adjeiv@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:59:47 +0100 Subject: [PATCH 3/3] Remove comment --- src/lib/sudoku-model.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/sudoku-model.js b/src/lib/sudoku-model.js index e4087bb..f4175d5 100644 --- a/src/lib/sudoku-model.js +++ b/src/lib/sudoku-model.js @@ -101,7 +101,7 @@ export function newSudokuModel({initialDigits, difficultyLevel, onPuzzleStateCha if (initialDigits.length < 81) { initialDigits = expandPuzzleDigits(initialDigits); } - initialDigits = initialDigits.replace(/\D/g, '') // clues + initialDigits = initialDigits.replace(/\D/g, '') const initialError = skipCheck ? undefined : modelHelpers.initialErrorCheck(initialDigits); const mode = initialError ? 'enter' : 'solve'; const settings = modelHelpers.loadSettings();