@fgv/ts-sudoku-lib 5.0.1-9 → 5.0.2-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.browser.js +37 -0
- package/dist/index.js +30 -0
- package/dist/packlets/collections/collections.js +136 -0
- package/dist/packlets/collections/data/puzzles.json +56 -0
- package/dist/packlets/collections/index.js +25 -0
- package/dist/packlets/common/cage.js +83 -0
- package/dist/packlets/common/cell.js +88 -0
- package/dist/packlets/common/common.js +70 -0
- package/dist/packlets/common/converters.js +65 -0
- package/dist/packlets/common/ids.js +152 -0
- package/dist/packlets/common/index.js +36 -0
- package/dist/packlets/common/logging.js +32 -0
- package/dist/packlets/common/public.js +25 -0
- package/dist/packlets/common/puzzle.js +411 -0
- package/dist/packlets/common/puzzleDefinitions.js +247 -0
- package/dist/packlets/common/puzzleSession.js +369 -0
- package/dist/packlets/common/puzzleState.js +99 -0
- package/dist/packlets/files/converters.js +67 -0
- package/dist/packlets/files/fileTreeHelpers.js +37 -0
- package/dist/packlets/files/filesystem.js +37 -0
- package/dist/packlets/files/index.browser.js +32 -0
- package/dist/packlets/files/index.js +30 -0
- package/dist/packlets/files/model.js +25 -0
- package/dist/packlets/hints/baseHintProvider.js +199 -0
- package/dist/packlets/hints/explanations.js +270 -0
- package/dist/packlets/hints/hiddenSingles.js +233 -0
- package/dist/packlets/hints/hintRegistry.js +222 -0
- package/dist/packlets/hints/hints.js +311 -0
- package/dist/packlets/hints/index.js +39 -0
- package/dist/packlets/hints/interfaces.js +25 -0
- package/dist/packlets/hints/nakedSingles.js +198 -0
- package/dist/packlets/hints/puzzleSessionHints.js +495 -0
- package/dist/packlets/hints/types.js +43 -0
- package/dist/packlets/puzzles/anyPuzzle.js +47 -0
- package/dist/packlets/puzzles/index.js +31 -0
- package/dist/packlets/puzzles/internal/combinationCache.js +75 -0
- package/dist/packlets/puzzles/internal/combinationGenerator.js +142 -0
- package/dist/packlets/puzzles/internal/possibilityAnalyzer.js +121 -0
- package/dist/packlets/puzzles/killerCombinations.js +222 -0
- package/dist/packlets/puzzles/killerCombinationsTypes.js +25 -0
- package/dist/packlets/puzzles/killerSudokuPuzzle.js +136 -0
- package/dist/packlets/puzzles/sudokuPuzzle.js +43 -0
- package/dist/packlets/puzzles/sudokuXPuzzle.js +65 -0
- package/dist/test/unit/helpers/puzzleBuilders.js +149 -0
- package/dist/ts-sudoku-lib.d.ts +12 -7
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/index.browser.d.ts +7 -0
- package/lib/index.browser.js +78 -0
- package/lib/packlets/files/fileTreeHelpers.d.ts +13 -0
- package/lib/packlets/files/fileTreeHelpers.js +40 -0
- package/lib/packlets/files/index.browser.d.ts +5 -0
- package/lib/packlets/files/index.browser.js +70 -0
- package/lib/packlets/files/index.d.ts +3 -2
- package/lib/packlets/files/index.js +7 -4
- package/package.json +23 -5
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2023 Erik Fortune
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
import { succeed, fail, captureResult } from '@fgv/ts-utils';
|
|
25
|
+
/**
|
|
26
|
+
* Default validator for standard Sudoku puzzles
|
|
27
|
+
*/
|
|
28
|
+
class StandardSudokuValidator {
|
|
29
|
+
validateCells(cells, dimensions) {
|
|
30
|
+
const expectedLength = dimensions.cageHeightInCells *
|
|
31
|
+
dimensions.boardHeightInCages *
|
|
32
|
+
dimensions.cageWidthInCells *
|
|
33
|
+
dimensions.boardWidthInCages;
|
|
34
|
+
if (cells.length !== expectedLength) {
|
|
35
|
+
return fail(`Expected ${expectedLength} cells, got ${cells.length}`);
|
|
36
|
+
}
|
|
37
|
+
return succeed(true);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Validator for Killer Sudoku puzzles - understands extended format with cage definitions
|
|
42
|
+
*/
|
|
43
|
+
class KillerSudokuValidator {
|
|
44
|
+
validateCells(cells, dimensions) {
|
|
45
|
+
const expectedBasicLength = dimensions.cageHeightInCells *
|
|
46
|
+
dimensions.boardHeightInCages *
|
|
47
|
+
dimensions.cageWidthInCells *
|
|
48
|
+
dimensions.boardWidthInCages;
|
|
49
|
+
// Killer sudoku cells format includes cage definitions, so it will be longer than basic format
|
|
50
|
+
// The basic grid should be at the beginning of the string
|
|
51
|
+
if (cells.length < expectedBasicLength) {
|
|
52
|
+
return fail(`Killer sudoku cells must contain at least ${expectedBasicLength} grid cells, got ${cells.length}`);
|
|
53
|
+
}
|
|
54
|
+
// Find the separator between grid and cage definitions
|
|
55
|
+
const separatorIndex = cells.indexOf('|');
|
|
56
|
+
if (separatorIndex === -1) {
|
|
57
|
+
return fail('Killer sudoku cells must contain cage definitions after "|" separator');
|
|
58
|
+
}
|
|
59
|
+
if (separatorIndex !== expectedBasicLength) {
|
|
60
|
+
return fail(`Killer sudoku grid portion must be exactly ${expectedBasicLength} characters, got ${separatorIndex}`);
|
|
61
|
+
}
|
|
62
|
+
// Extract the basic grid portion (before the | separator)
|
|
63
|
+
const gridPortion = cells.substring(0, separatorIndex);
|
|
64
|
+
// For killer sudoku, the grid portion contains:
|
|
65
|
+
// - Digits 1-9 for prefilled values
|
|
66
|
+
// - '.' for empty cells
|
|
67
|
+
// - Letters A-Z, a-z for cage identifiers
|
|
68
|
+
const maxDigit = dimensions.cageWidthInCells * dimensions.cageHeightInCells;
|
|
69
|
+
const validDigits = new Set(['.', ...Array.from({ length: maxDigit }, (__val, i) => String(i + 1))]);
|
|
70
|
+
const validCageLetters = new Set([...'ABCDEFGHIJKLMNOPQRSTUVWXYZ', ...'abcdefghijklmnopqrstuvwxyz']);
|
|
71
|
+
for (let i = 0; i < gridPortion.length; i++) {
|
|
72
|
+
const char = gridPortion[i];
|
|
73
|
+
if (!validDigits.has(char) && !validCageLetters.has(char)) {
|
|
74
|
+
return fail(`Invalid character '${char}' at position ${i} in killer sudoku grid portion`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return succeed(true);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Validator for Sudoku X puzzles - same as standard but with diagonal constraints
|
|
82
|
+
*/
|
|
83
|
+
class SudokuXValidator {
|
|
84
|
+
validateCells(cells, dimensions) {
|
|
85
|
+
// Sudoku X uses the same cell format as standard sudoku
|
|
86
|
+
return new StandardSudokuValidator().validateCells(cells, dimensions);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Registry of validators for different puzzle types
|
|
91
|
+
*/
|
|
92
|
+
const PUZZLE_TYPE_VALIDATORS = {
|
|
93
|
+
sudoku: new StandardSudokuValidator(),
|
|
94
|
+
'killer-sudoku': new KillerSudokuValidator(),
|
|
95
|
+
'sudoku-x': new SudokuXValidator()
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Standard puzzle configurations
|
|
99
|
+
* @public
|
|
100
|
+
*/
|
|
101
|
+
export const STANDARD_CONFIGS = {
|
|
102
|
+
puzzle4x4: { cageWidthInCells: 2, cageHeightInCells: 2, boardWidthInCages: 2, boardHeightInCages: 2 },
|
|
103
|
+
puzzle6x6: { cageWidthInCells: 3, cageHeightInCells: 2, boardWidthInCages: 2, boardHeightInCages: 3 },
|
|
104
|
+
puzzle9x9: { cageWidthInCells: 3, cageHeightInCells: 3, boardWidthInCages: 3, boardHeightInCages: 3 },
|
|
105
|
+
puzzle12x12: { cageWidthInCells: 4, cageHeightInCells: 3, boardWidthInCages: 3, boardHeightInCages: 4 }
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Factory for creating and validating puzzle definitions
|
|
109
|
+
* @public
|
|
110
|
+
*/
|
|
111
|
+
export class PuzzleDefinitionFactory {
|
|
112
|
+
/**
|
|
113
|
+
* Create a puzzle definition from dimensions and options
|
|
114
|
+
*/
|
|
115
|
+
static create(dimensions, options = {}) {
|
|
116
|
+
var _a, _b, _c, _d, _e;
|
|
117
|
+
const dimensionValidation = this.validate(dimensions);
|
|
118
|
+
if (dimensionValidation.isFailure()) {
|
|
119
|
+
return fail(dimensionValidation.message);
|
|
120
|
+
}
|
|
121
|
+
const totalRows = dimensions.cageHeightInCells * dimensions.boardHeightInCages;
|
|
122
|
+
const totalColumns = dimensions.cageWidthInCells * dimensions.boardWidthInCages;
|
|
123
|
+
const maxValue = dimensions.cageWidthInCells * dimensions.cageHeightInCells;
|
|
124
|
+
const totalCages = dimensions.boardWidthInCages * dimensions.boardHeightInCages;
|
|
125
|
+
const basicCageTotal = this._calculateBasicCageTotal(maxValue);
|
|
126
|
+
// Validate cell data using type-specific validator
|
|
127
|
+
if (options.cells) {
|
|
128
|
+
const puzzleType = (_a = options.type) !== null && _a !== void 0 ? _a : 'sudoku';
|
|
129
|
+
const validator = PUZZLE_TYPE_VALIDATORS[puzzleType];
|
|
130
|
+
if (!validator) {
|
|
131
|
+
return fail(`Unknown puzzle type: ${puzzleType}`);
|
|
132
|
+
}
|
|
133
|
+
const cellValidation = validator.validateCells(options.cells, dimensions);
|
|
134
|
+
if (cellValidation.isFailure()) {
|
|
135
|
+
return fail(cellValidation.message);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const puzzleDefinition = {
|
|
139
|
+
// Dimensions
|
|
140
|
+
cageWidthInCells: dimensions.cageWidthInCells,
|
|
141
|
+
cageHeightInCells: dimensions.cageHeightInCells,
|
|
142
|
+
boardWidthInCages: dimensions.boardWidthInCages,
|
|
143
|
+
boardHeightInCages: dimensions.boardHeightInCages,
|
|
144
|
+
// Derived properties
|
|
145
|
+
totalRows,
|
|
146
|
+
totalColumns,
|
|
147
|
+
maxValue,
|
|
148
|
+
totalCages,
|
|
149
|
+
basicCageTotal,
|
|
150
|
+
// Default values
|
|
151
|
+
id: options.id,
|
|
152
|
+
description: (_b = options.description) !== null && _b !== void 0 ? _b : `${totalRows}x${totalColumns} puzzle`,
|
|
153
|
+
type: (_c = options.type) !== null && _c !== void 0 ? _c : 'sudoku',
|
|
154
|
+
level: (_d = options.level) !== null && _d !== void 0 ? _d : 1,
|
|
155
|
+
cells: (_e = options.cells) !== null && _e !== void 0 ? _e : '.'.repeat(totalRows * totalColumns),
|
|
156
|
+
cages: options.cages
|
|
157
|
+
};
|
|
158
|
+
return succeed(puzzleDefinition);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Create killer sudoku puzzle definition with cage constraints
|
|
162
|
+
*/
|
|
163
|
+
static createKiller(dimensions, description) {
|
|
164
|
+
return captureResult(() => {
|
|
165
|
+
// Import needed here to avoid circular dependencies
|
|
166
|
+
const { Ids } = require('./ids');
|
|
167
|
+
// Convert killer cage format to standard cage format - creating minimal cage objects
|
|
168
|
+
const cages = description.killerCages.map((killerCage) => {
|
|
169
|
+
const cellIds = killerCage.cellPositions.map((pos) => Ids.cellId(pos).orThrow());
|
|
170
|
+
return {
|
|
171
|
+
id: `K${killerCage.id}`,
|
|
172
|
+
cellIds,
|
|
173
|
+
cageType: 'killer',
|
|
174
|
+
total: killerCage.sum,
|
|
175
|
+
numCells: cellIds.length,
|
|
176
|
+
/* c8 ignore next 1 - coverage fails when run as part of full suite */
|
|
177
|
+
containsCell: (cellId) => cellIds.includes(cellId)
|
|
178
|
+
};
|
|
179
|
+
});
|
|
180
|
+
// Return the result directly, not wrapped in another Result
|
|
181
|
+
const result = this.create(dimensions, Object.assign(Object.assign({}, description), { cages, type: 'killer-sudoku' }));
|
|
182
|
+
if (result.isFailure()) {
|
|
183
|
+
throw new Error(result.message);
|
|
184
|
+
}
|
|
185
|
+
return result.value;
|
|
186
|
+
}).onFailure((error) => fail(`Failed to create killer sudoku: ${error}`));
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Validate puzzle dimensions
|
|
190
|
+
*/
|
|
191
|
+
static validate(dimensions) {
|
|
192
|
+
// Structural validation
|
|
193
|
+
if (dimensions.cageWidthInCells < 1 || dimensions.cageHeightInCells < 1) {
|
|
194
|
+
return fail('Cage dimensions must be positive integers');
|
|
195
|
+
}
|
|
196
|
+
// Sudoku cages must be multi-cell (both dimensions > 1)
|
|
197
|
+
if (dimensions.cageWidthInCells < 2 || dimensions.cageHeightInCells < 2) {
|
|
198
|
+
return fail('Cage dimensions must be at least 2x2 for valid Sudoku puzzles');
|
|
199
|
+
}
|
|
200
|
+
if (dimensions.boardWidthInCages < 1 || dimensions.boardHeightInCages < 1) {
|
|
201
|
+
return fail('Board dimensions must be positive integers');
|
|
202
|
+
}
|
|
203
|
+
// Size constraints (reasonable limits)
|
|
204
|
+
const totalRows = dimensions.cageHeightInCells * dimensions.boardHeightInCages;
|
|
205
|
+
const totalColumns = dimensions.cageWidthInCells * dimensions.boardWidthInCages;
|
|
206
|
+
if (totalRows > 25 || totalColumns > 25) {
|
|
207
|
+
return fail('Total grid size must not exceed 25x25');
|
|
208
|
+
}
|
|
209
|
+
// Mathematical validity - ensure cage size makes sense for Sudoku constraints
|
|
210
|
+
const maxValue = dimensions.cageWidthInCells * dimensions.cageHeightInCells;
|
|
211
|
+
if (maxValue < 2 || maxValue > 25) {
|
|
212
|
+
return fail('Cage size must result in values between 2 and 25');
|
|
213
|
+
}
|
|
214
|
+
return succeed(true);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Get a standard configuration by name
|
|
218
|
+
*/
|
|
219
|
+
static getStandardConfig(name) {
|
|
220
|
+
return STANDARD_CONFIGS[name];
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Get all available standard configurations
|
|
224
|
+
*/
|
|
225
|
+
static getStandardConfigs() {
|
|
226
|
+
return STANDARD_CONFIGS;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Get validator for a specific puzzle type
|
|
230
|
+
*/
|
|
231
|
+
static getValidator(puzzleType) {
|
|
232
|
+
return PUZZLE_TYPE_VALIDATORS[puzzleType];
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Register a custom validator for a puzzle type
|
|
236
|
+
*/
|
|
237
|
+
static registerValidator(puzzleType, validator) {
|
|
238
|
+
PUZZLE_TYPE_VALIDATORS[puzzleType] = validator;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Calculate the basic cage total (sum of 1 to maxValue)
|
|
242
|
+
*/
|
|
243
|
+
static _calculateBasicCageTotal(maxValue) {
|
|
244
|
+
return (maxValue * (maxValue + 1)) / 2;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
//# sourceMappingURL=puzzleDefinitions.js.map
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2023 Erik Fortune
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
import { captureResult, fail, succeed } from '@fgv/ts-utils';
|
|
25
|
+
import { Cage } from './cage';
|
|
26
|
+
import { Cell } from './cell';
|
|
27
|
+
import { Ids } from './ids';
|
|
28
|
+
/**
|
|
29
|
+
* Represents a single puzzle session, including puzzle, current state and redo/undo.
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export class PuzzleSession {
|
|
33
|
+
/**
|
|
34
|
+
* @internal
|
|
35
|
+
*/
|
|
36
|
+
constructor(puzzle) {
|
|
37
|
+
this._puzzle = puzzle;
|
|
38
|
+
this.state = puzzle.initialState;
|
|
39
|
+
this._nextStep = 0;
|
|
40
|
+
this._numSteps = 0;
|
|
41
|
+
this._steps = [];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* ID of the puzzle being solved.
|
|
45
|
+
*/
|
|
46
|
+
get id() {
|
|
47
|
+
return this._puzzle.id;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Description of the puzzle being solved.
|
|
51
|
+
*/
|
|
52
|
+
get description() {
|
|
53
|
+
return this._puzzle.description;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Type of the puzzle being solved.
|
|
57
|
+
*/
|
|
58
|
+
get type() {
|
|
59
|
+
return this._puzzle.type;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Number of rows in the puzzle being solved.
|
|
63
|
+
*/
|
|
64
|
+
get numRows() {
|
|
65
|
+
return this._puzzle.numRows;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Number of columns in the puzzle being solved.
|
|
69
|
+
*/
|
|
70
|
+
get numColumns() {
|
|
71
|
+
return this._puzzle.numColumns;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The row {@link ICage | cages} in the puzzle being solved.
|
|
75
|
+
*/
|
|
76
|
+
get rows() {
|
|
77
|
+
return this._puzzle.rows;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The column {@link ICage | cages} in the puzzle being solved.
|
|
81
|
+
*/
|
|
82
|
+
get cols() {
|
|
83
|
+
return this._puzzle.cols;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The section {@link ICage | cages} in the puzzle being solved.
|
|
87
|
+
*/
|
|
88
|
+
get sections() {
|
|
89
|
+
return this._puzzle.sections;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* All {@link ICage | cages} in the puzzle being solved.
|
|
93
|
+
*/
|
|
94
|
+
get cages() {
|
|
95
|
+
return this._puzzle.cages;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* The cells {@link ICell | cells} in the puzzle being solved.
|
|
99
|
+
*/
|
|
100
|
+
get cells() {
|
|
101
|
+
return this._puzzle.cells;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The puzzle structure for this session.
|
|
105
|
+
*/
|
|
106
|
+
get puzzle() {
|
|
107
|
+
return this._puzzle;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Index of the next step in this puzzle session.
|
|
111
|
+
*/
|
|
112
|
+
get nextStep() {
|
|
113
|
+
return this._nextStep;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Number of steps currently elapsed in this puzzle session. Note
|
|
117
|
+
* that after undo, `nextStep` will be less than `numSteps`.
|
|
118
|
+
*/
|
|
119
|
+
get numSteps() {
|
|
120
|
+
return this._numSteps;
|
|
121
|
+
}
|
|
122
|
+
/***
|
|
123
|
+
* Indicates whether undo is currently possible.
|
|
124
|
+
*/
|
|
125
|
+
get canUndo() {
|
|
126
|
+
return this._nextStep > 0;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Indicates whether redo is currently possible.
|
|
130
|
+
*/
|
|
131
|
+
get canRedo() {
|
|
132
|
+
return this._nextStep < this._numSteps;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Creates a new {@link PuzzleSession | puzzle session} from a supplied
|
|
136
|
+
* {@link Puzzle | puzzle}.
|
|
137
|
+
* @param puzzle - The {@link Puzzle | puzzle} from which the session is to be
|
|
138
|
+
* initialized.
|
|
139
|
+
* @returns `Success` with the requested {@link PuzzleSession | puzzle session},
|
|
140
|
+
* or `Failure` with details if an error occurs.
|
|
141
|
+
*/
|
|
142
|
+
static create(puzzle) {
|
|
143
|
+
return captureResult(() => new PuzzleSession(puzzle));
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Determines if the puzzle is correctly solved.
|
|
147
|
+
* @returns `true` if the puzzle is solved, `false` if the puzzle has
|
|
148
|
+
* empty or invalid cells.
|
|
149
|
+
*/
|
|
150
|
+
checkIsSolved() {
|
|
151
|
+
return this._puzzle.checkIsSolved(this.state);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Determines if the puzzle is valid in its current state.
|
|
155
|
+
* @returns `true` if all non-empty cells in the puzzle are valid,
|
|
156
|
+
* or `false` if any cells are invalid.
|
|
157
|
+
*/
|
|
158
|
+
checkIsValid() {
|
|
159
|
+
return this._puzzle.checkIsValid(this.state);
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Gets all of the currently empty {@link ICell | cells} in the puzzle.
|
|
163
|
+
* @returns An array of {@link ICell | ICell} with all empty cells.
|
|
164
|
+
*/
|
|
165
|
+
getEmptyCells() {
|
|
166
|
+
return this._puzzle.getEmptyCells(this.state);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Gets all of the currently invalid {@link ICell | cells} in the puzzle.
|
|
170
|
+
* @returns An array of {@link ICell | ICell} with all invalid cells.
|
|
171
|
+
*/
|
|
172
|
+
getInvalidCells() {
|
|
173
|
+
return this._puzzle.getInvalidCells(this.state);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Determines if a cell is valid.
|
|
177
|
+
* @param spec - A `string` ({@link CellId | CellId}), {@link IRowColumn | RowColumn} or {@link ICell | ICell}
|
|
178
|
+
* describing the cell to be tested.
|
|
179
|
+
* @returns `true` if the cell value is valid, `false` if the cell value or the cell itself is invalid.
|
|
180
|
+
*/
|
|
181
|
+
cellIsValid(spec) {
|
|
182
|
+
var _a;
|
|
183
|
+
return ((_a = this._cell(spec)) === null || _a === void 0 ? void 0 : _a.isValid(this.state)) === true;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Determines if a cell has a value.
|
|
187
|
+
* @param spec - A `string` ({@link CellId | CellId}), {@link IRowColumn | RowColumn} or {@link ICell | ICell}
|
|
188
|
+
* describing the cell to be tested.
|
|
189
|
+
* @returns `true` if the cell has a value, `false` if the cell is empty or the cell itself is invalid.
|
|
190
|
+
*/
|
|
191
|
+
cellHasValue(spec) {
|
|
192
|
+
var _a;
|
|
193
|
+
return ((_a = this._cell(spec)) === null || _a === void 0 ? void 0 : _a.hasValue(this.state)) === true;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Determines if supplied value is valid for a specific cell.
|
|
197
|
+
* @param spec - A `string` ({@link CellId | CellId}), {@link IRowColumn | RowColumn} or {@link ICell | ICell}
|
|
198
|
+
* describing the cell to be tested.
|
|
199
|
+
* @param value - The value to be tested.
|
|
200
|
+
* @returns `true` if `value` is valid for the requested cell, `false` if the value or the cell itself is invalid.
|
|
201
|
+
*/
|
|
202
|
+
isValidForCell(spec, value) {
|
|
203
|
+
var _a;
|
|
204
|
+
return ((_a = this._cell(spec)) === null || _a === void 0 ? void 0 : _a.isValidValue(value, this.state)) === true;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Gets the neighbor for a cell in a given direction using specified wrapping rules.
|
|
208
|
+
* @param spec - A `string` ({@link CellId | CellId}), {@link IRowColumn | RowColumn} or {@link ICell | ICell}
|
|
209
|
+
* describing the cell to be tested.
|
|
210
|
+
* @param direction - The direction of the desired neighbor.
|
|
211
|
+
* @param wrap - Wrapping rules to be applied.
|
|
212
|
+
* @returns `Success` with the requested {@link ICell | cell}, or `Failure` with details if an error occurs.
|
|
213
|
+
*/
|
|
214
|
+
getCellNeighbor(spec, direction, wrap) {
|
|
215
|
+
return this._puzzle.getCellNeighbor(spec, direction, wrap);
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Gets the {@link ICellContents | contents} for a specified cell.
|
|
219
|
+
* @param spec - A `string` ({@link CellId | CellId}), {@link IRowColumn | RowColumn} or {@link ICell | ICell}
|
|
220
|
+
* describing the cell to be queried.
|
|
221
|
+
* @returns `Success` with the {@link ICell | cell description} and {@link ICellContents | cell contents}, or
|
|
222
|
+
* `Failure` with details if an error occurs.
|
|
223
|
+
*/
|
|
224
|
+
getCellContents(spec) {
|
|
225
|
+
return this._puzzle.getCellContents(spec, this.state);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Updates the value of a cell.
|
|
229
|
+
* @param spec - A `string`, {@link IRowColumn | row and column}, or {@link ICell | cell} identifying
|
|
230
|
+
* the cell to be updated.
|
|
231
|
+
* @param value - A new value for the cell.
|
|
232
|
+
* @returns `Success` with `this` if the update is applied, `Failure` with details if an error occurs.
|
|
233
|
+
*/
|
|
234
|
+
updateCellValue(spec, value) {
|
|
235
|
+
const idResult = Ids.cellId(spec);
|
|
236
|
+
return idResult.onSuccess((id) => {
|
|
237
|
+
const notes = [];
|
|
238
|
+
const update = { id, value, notes };
|
|
239
|
+
return this._puzzle.updateValues(update, this.state).onSuccess((update) => {
|
|
240
|
+
this._addMove(update.cells);
|
|
241
|
+
this.state = update.to;
|
|
242
|
+
return succeed(this);
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Updates the notes on a cell.
|
|
248
|
+
* @param spec - A `string`, {@link IRowColumn | row and column}, or {@link ICell | cell} identifying
|
|
249
|
+
* the cell to be updated.
|
|
250
|
+
* @param notes - New notes for the cell.
|
|
251
|
+
* @returns `Success` with `this` if the update is applied, `Failure` with details if an error occurs.
|
|
252
|
+
*/
|
|
253
|
+
updateCellNotes(spec, notes) {
|
|
254
|
+
const idResult = Ids.cellId(spec);
|
|
255
|
+
return idResult.onSuccess((id) => {
|
|
256
|
+
const value = undefined;
|
|
257
|
+
const update = { id, value, notes };
|
|
258
|
+
return this._puzzle.updateNotes(update, this.state).onSuccess((update) => {
|
|
259
|
+
this._addMove(update.cells);
|
|
260
|
+
this.state = update.to;
|
|
261
|
+
return succeed(this);
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Updates value & notes for multiple cells.
|
|
267
|
+
* @param updates - An array of {@link ICellState | cell state} objects, each describing
|
|
268
|
+
* one cell to be updated.
|
|
269
|
+
* @returns `Success` with `this` if the updates are applied, `Failure` with details if
|
|
270
|
+
* an error occurs.
|
|
271
|
+
*/
|
|
272
|
+
updateCells(updates) {
|
|
273
|
+
return this._puzzle.updateContents(updates, this.state).onSuccess((update) => {
|
|
274
|
+
this._addMove(update.cells);
|
|
275
|
+
this.state = update.to;
|
|
276
|
+
return succeed(this);
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Determines if some {@link ICage | cage} contains a specific value.
|
|
281
|
+
* @param spec - A `string` ({@link CageId | CageId}) or {@link ICage | ICage}
|
|
282
|
+
* indicating the cage to be tested.
|
|
283
|
+
* @param value - The value to be tested.
|
|
284
|
+
* @returns `true` if the cage exists and contains the specified value,
|
|
285
|
+
* `false` otherwise.
|
|
286
|
+
*/
|
|
287
|
+
cageContainsValue(spec, value) {
|
|
288
|
+
var _a;
|
|
289
|
+
return ((_a = this._cage(spec)) === null || _a === void 0 ? void 0 : _a.containsValue(value, this.state)) === true;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Determines the numbers currently present in some cage.
|
|
293
|
+
* @param spec - A `string` ({@link CageId | CageId}) or {@link ICage | ICage}
|
|
294
|
+
* indicating the cage to be tested.
|
|
295
|
+
* @returns A `Set<number>` containing all numbers present in the cage.
|
|
296
|
+
*/
|
|
297
|
+
cageContainedValues(spec) {
|
|
298
|
+
var _a, _b;
|
|
299
|
+
return (_b = (_a = this._cage(spec)) === null || _a === void 0 ? void 0 : _a.containedValues(this.state)) !== null && _b !== void 0 ? _b : new Set();
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Undo a single move in this puzzle session.
|
|
303
|
+
* @returns `Success` with `this` if the undo is applied, or `Failure`
|
|
304
|
+
* with details if an error occurs.
|
|
305
|
+
*/
|
|
306
|
+
undo() {
|
|
307
|
+
if (!this.canUndo) {
|
|
308
|
+
return fail(`nothing to undo`);
|
|
309
|
+
}
|
|
310
|
+
return this._puzzle
|
|
311
|
+
.updateContents(this._steps[this._nextStep - 1].updates.map((u) => u.from), this.state)
|
|
312
|
+
.onSuccess((update) => {
|
|
313
|
+
this._nextStep--;
|
|
314
|
+
this.state = update.to;
|
|
315
|
+
return succeed(this);
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Redo a single move in this puzzle session.
|
|
320
|
+
* @returns `Success` with `this` if the redo is applied, or `Failure`
|
|
321
|
+
* with details if an error occurs.
|
|
322
|
+
*/
|
|
323
|
+
redo() {
|
|
324
|
+
if (!this.canRedo) {
|
|
325
|
+
return fail('nothing to redo');
|
|
326
|
+
}
|
|
327
|
+
return this._puzzle
|
|
328
|
+
.updateContents(this._steps[this._nextStep].updates.map((u) => u.to), this.state)
|
|
329
|
+
.onSuccess((update) => {
|
|
330
|
+
this._nextStep++;
|
|
331
|
+
this.state = update.to;
|
|
332
|
+
return succeed(this);
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Gets a string representation of this puzzle, one string
|
|
337
|
+
* per row.
|
|
338
|
+
*/
|
|
339
|
+
toStrings() {
|
|
340
|
+
return this._puzzle.toStrings(this.state);
|
|
341
|
+
}
|
|
342
|
+
_addMove(updates) {
|
|
343
|
+
if (this._nextStep < this._steps.length) {
|
|
344
|
+
this._steps[this._nextStep++] = { updates };
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
this._steps.push({ updates });
|
|
348
|
+
this._nextStep = this._steps.length;
|
|
349
|
+
}
|
|
350
|
+
this._numSteps = this._nextStep;
|
|
351
|
+
}
|
|
352
|
+
_cage(spec) {
|
|
353
|
+
if (spec instanceof Cage) {
|
|
354
|
+
return spec;
|
|
355
|
+
}
|
|
356
|
+
return Ids.cageId(spec)
|
|
357
|
+
.onSuccess((id) => {
|
|
358
|
+
return this._puzzle.getCage(id);
|
|
359
|
+
})
|
|
360
|
+
.orDefault();
|
|
361
|
+
}
|
|
362
|
+
_cell(spec) {
|
|
363
|
+
if (spec instanceof Cell) {
|
|
364
|
+
return spec;
|
|
365
|
+
}
|
|
366
|
+
return this._puzzle.getCell(spec).orDefault();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
//# sourceMappingURL=puzzleSession.js.map
|