@fgv/ts-sudoku-lib 5.0.0-2 → 5.0.0-21

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.
Files changed (80) hide show
  1. package/.vscode/settings.json +3 -0
  2. package/config/api-extractor.json +343 -0
  3. package/config/rig.json +16 -0
  4. package/dist/tsdoc-metadata.json +1 -1
  5. package/lib/test/unit/collections.test.d.ts +2 -0
  6. package/lib/test/unit/common/cell.test.d.ts +2 -0
  7. package/lib/test/unit/common/ids.test.d.ts +2 -0
  8. package/lib/test/unit/common/puzzle.test.d.ts +2 -0
  9. package/lib/test/unit/common/puzzleSession.test.d.ts +2 -0
  10. package/lib/test/unit/common/puzzleState.test.d.ts +2 -0
  11. package/lib/test/unit/puzzles/killerSudokuState.test.d.ts +2 -0
  12. package/package.json +14 -14
  13. package/src/index.ts +30 -0
  14. package/src/packlets/collections/collections.ts +124 -0
  15. package/src/packlets/collections/data/puzzles.json +61 -0
  16. package/src/packlets/collections/index.ts +25 -0
  17. package/src/packlets/common/cage.ts +98 -0
  18. package/src/packlets/common/cell.ts +116 -0
  19. package/src/packlets/common/common.ts +120 -0
  20. package/src/packlets/common/converters.ts +84 -0
  21. package/src/packlets/common/ids.ts +98 -0
  22. package/src/packlets/common/index.ts +37 -0
  23. package/src/packlets/common/model.ts +39 -0
  24. package/src/packlets/common/public.ts +100 -0
  25. package/src/packlets/common/puzzle.ts +523 -0
  26. package/src/packlets/common/puzzleSession.ts +415 -0
  27. package/src/packlets/common/puzzleState.ts +108 -0
  28. package/src/packlets/file/converters.ts +49 -0
  29. package/src/packlets/file/index.ts +28 -0
  30. package/src/packlets/file/model.ts +33 -0
  31. package/src/packlets/puzzles/anyPuzzle.ts +50 -0
  32. package/src/packlets/puzzles/index.ts +30 -0
  33. package/src/packlets/puzzles/killerSudokuPuzzle.ts +151 -0
  34. package/src/packlets/puzzles/sudokuPuzzle.ts +46 -0
  35. package/src/packlets/puzzles/sudokuXPuzzle.ts +67 -0
  36. package/CHANGELOG.md +0 -106
  37. package/lib/index.d.ts.map +0 -1
  38. package/lib/index.js.map +0 -1
  39. package/lib/packlets/collections/collections.d.ts.map +0 -1
  40. package/lib/packlets/collections/collections.js.map +0 -1
  41. package/lib/packlets/collections/index.d.ts.map +0 -1
  42. package/lib/packlets/collections/index.js.map +0 -1
  43. package/lib/packlets/common/cage.d.ts.map +0 -1
  44. package/lib/packlets/common/cage.js.map +0 -1
  45. package/lib/packlets/common/cell.d.ts.map +0 -1
  46. package/lib/packlets/common/cell.js.map +0 -1
  47. package/lib/packlets/common/common.d.ts.map +0 -1
  48. package/lib/packlets/common/common.js.map +0 -1
  49. package/lib/packlets/common/converters.d.ts.map +0 -1
  50. package/lib/packlets/common/converters.js.map +0 -1
  51. package/lib/packlets/common/ids.d.ts.map +0 -1
  52. package/lib/packlets/common/ids.js.map +0 -1
  53. package/lib/packlets/common/index.d.ts.map +0 -1
  54. package/lib/packlets/common/index.js.map +0 -1
  55. package/lib/packlets/common/model.d.ts.map +0 -1
  56. package/lib/packlets/common/model.js.map +0 -1
  57. package/lib/packlets/common/public.d.ts.map +0 -1
  58. package/lib/packlets/common/public.js.map +0 -1
  59. package/lib/packlets/common/puzzle.d.ts.map +0 -1
  60. package/lib/packlets/common/puzzle.js.map +0 -1
  61. package/lib/packlets/common/puzzleSession.d.ts.map +0 -1
  62. package/lib/packlets/common/puzzleSession.js.map +0 -1
  63. package/lib/packlets/common/puzzleState.d.ts.map +0 -1
  64. package/lib/packlets/common/puzzleState.js.map +0 -1
  65. package/lib/packlets/file/converters.d.ts.map +0 -1
  66. package/lib/packlets/file/converters.js.map +0 -1
  67. package/lib/packlets/file/index.d.ts.map +0 -1
  68. package/lib/packlets/file/index.js.map +0 -1
  69. package/lib/packlets/file/model.d.ts.map +0 -1
  70. package/lib/packlets/file/model.js.map +0 -1
  71. package/lib/packlets/puzzles/anyPuzzle.d.ts.map +0 -1
  72. package/lib/packlets/puzzles/anyPuzzle.js.map +0 -1
  73. package/lib/packlets/puzzles/index.d.ts.map +0 -1
  74. package/lib/packlets/puzzles/index.js.map +0 -1
  75. package/lib/packlets/puzzles/killerSudokuPuzzle.d.ts.map +0 -1
  76. package/lib/packlets/puzzles/killerSudokuPuzzle.js.map +0 -1
  77. package/lib/packlets/puzzles/sudokuPuzzle.d.ts.map +0 -1
  78. package/lib/packlets/puzzles/sudokuPuzzle.js.map +0 -1
  79. package/lib/packlets/puzzles/sudokuXPuzzle.d.ts.map +0 -1
  80. package/lib/packlets/puzzles/sudokuXPuzzle.js.map +0 -1
@@ -0,0 +1,415 @@
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
+
25
+ import { Result, captureResult, fail, succeed } from '@fgv/ts-utils';
26
+ import { Cage } from './cage';
27
+ import { Cell } from './cell';
28
+ import { ICellContents, ICellState, IRowColumn, NavigationDirection, NavigationWrap } from './common';
29
+ import { Ids } from './ids';
30
+ import { ICage, ICell } from './public';
31
+ import { ICellUpdate, Puzzle } from './puzzle';
32
+ import { PuzzleState } from './puzzleState';
33
+
34
+ interface IPuzzleStep {
35
+ updates: ICellUpdate[];
36
+ }
37
+
38
+ /**
39
+ * Represents a single puzzle session, including puzzle, current state and redo/undo.
40
+ * @public
41
+ */
42
+ export class PuzzleSession {
43
+ /**
44
+ * The current {@link PuzzleState | state} of this puzzle session.
45
+ */
46
+ public state: PuzzleState;
47
+
48
+ protected readonly _puzzle: Puzzle;
49
+ protected _nextStep: number;
50
+ protected _numSteps: number;
51
+ protected _steps: IPuzzleStep[];
52
+
53
+ /**
54
+ * @internal
55
+ */
56
+ protected constructor(puzzle: Puzzle) {
57
+ this._puzzle = puzzle;
58
+ this.state = puzzle.initialState;
59
+ this._nextStep = 0;
60
+ this._numSteps = 0;
61
+ this._steps = [];
62
+ }
63
+
64
+ /**
65
+ * ID of the puzzle being solved.
66
+ */
67
+ public get id(): string | undefined {
68
+ return this._puzzle.id;
69
+ }
70
+
71
+ /**
72
+ * Description of the puzzle being solved.
73
+ */
74
+ public get description(): string {
75
+ return this._puzzle.description;
76
+ }
77
+
78
+ /**
79
+ * Number of rows in the puzzle being solved.
80
+ */
81
+ public get numRows(): number {
82
+ return this._puzzle.numRows;
83
+ }
84
+
85
+ /**
86
+ * Number of columns in the puzzle being solved.
87
+ */
88
+ public get numColumns(): number {
89
+ return this._puzzle.numColumns;
90
+ }
91
+
92
+ /**
93
+ * The row {@link ICage | cages} in the puzzle being solved.
94
+ */
95
+ public get rows(): ICage[] {
96
+ return this._puzzle.rows;
97
+ }
98
+
99
+ /**
100
+ * The column {@link ICage | cages} in the puzzle being solved.
101
+ */
102
+ public get cols(): ICage[] {
103
+ return this._puzzle.cols;
104
+ }
105
+
106
+ /**
107
+ * The section {@link ICage | cages} in the puzzle being solved.
108
+ */
109
+ public get sections(): ICage[] {
110
+ return this._puzzle.sections;
111
+ }
112
+
113
+ /**
114
+ * All {@link ICage | cages} in the puzzle being solved.
115
+ */
116
+ public get cages(): ICage[] {
117
+ return this._puzzle.cages;
118
+ }
119
+
120
+ /**
121
+ * The cells {@link ICell | cells} in the puzzle being solved.
122
+ */
123
+ public get cells(): ICell[] {
124
+ return this._puzzle.cells;
125
+ }
126
+
127
+ /**
128
+ * Index of the next step in this puzzle session.
129
+ */
130
+ public get nextStep(): number {
131
+ return this._nextStep;
132
+ }
133
+
134
+ /**
135
+ * Number of steps currently elapsed in this puzzle session. Note
136
+ * that after undo, `nextStep` will be less than `numSteps`.
137
+ */
138
+ public get numSteps(): number {
139
+ return this._numSteps;
140
+ }
141
+
142
+ /***
143
+ * Indicates whether undo is currently possible.
144
+ */
145
+ public get canUndo(): boolean {
146
+ return this._nextStep > 0;
147
+ }
148
+
149
+ /**
150
+ * Indicates whether redo is currently possible.
151
+ */
152
+ public get canRedo(): boolean {
153
+ return this._nextStep < this._numSteps;
154
+ }
155
+
156
+ /**
157
+ * Creates a new {@link PuzzleSession | puzzle session} from a supplied
158
+ * {@link Puzzle | puzzle}.
159
+ * @param puzzle - The {@link Puzzle | puzzle} from which the session is to be
160
+ * initialized.
161
+ * @returns `Success` with the requested {@link PuzzleSession | puzzle session},
162
+ * or `Failure` with details if an error occurs.
163
+ */
164
+ public static create(puzzle: Puzzle): Result<PuzzleSession> {
165
+ return captureResult(() => new PuzzleSession(puzzle));
166
+ }
167
+
168
+ /**
169
+ * Determines if the puzzle is correctly solved.
170
+ * @returns `true` if the puzzle is solved, `false` if the puzzle has
171
+ * empty or invalid cells.
172
+ */
173
+ public checkIsSolved(): boolean {
174
+ return this._puzzle.checkIsSolved(this.state);
175
+ }
176
+
177
+ /**
178
+ * Determines if the puzzle is valid in its current state.
179
+ * @returns `true` if all non-empty cells in the puzzle are valid,
180
+ * or `false` if any cells are invalid.
181
+ */
182
+ public checkIsValid(): boolean {
183
+ return this._puzzle.checkIsValid(this.state);
184
+ }
185
+
186
+ /**
187
+ * Gets all of the currently empty {@link ICell | cells} in the puzzle.
188
+ * @returns An array of {@link ICell | ICell} with all empty cells.
189
+ */
190
+ public getEmptyCells(): ICell[] {
191
+ return this._puzzle.getEmptyCells(this.state);
192
+ }
193
+
194
+ /**
195
+ * Gets all of the currently invalid {@link ICell | cells} in the puzzle.
196
+ * @returns An array of {@link ICell | ICell} with all invalid cells.
197
+ */
198
+ public getInvalidCells(): ICell[] {
199
+ return this._puzzle.getInvalidCells(this.state);
200
+ }
201
+
202
+ /**
203
+ * Determines if a cell is valid.
204
+ * @param spec - A `string` ({@link CellId | CellId}), {@link IRowColumn | RowColumn} or {@link ICell | ICell}
205
+ * describing the cell to be tested.
206
+ * @returns `true` if the cell value is valid, `false` if the cell value or the cell itself is invalid.
207
+ */
208
+ public cellIsValid(spec: string | IRowColumn | ICell): boolean {
209
+ return this._cell(spec)?.isValid(this.state) === true;
210
+ }
211
+
212
+ /**
213
+ * Determines if a cell has a value.
214
+ * @param spec - A `string` ({@link CellId | CellId}), {@link IRowColumn | RowColumn} or {@link ICell | ICell}
215
+ * describing the cell to be tested.
216
+ * @returns `true` if the cell has a value, `false` if the cell is empty or the cell itself is invalid.
217
+ */
218
+ public cellHasValue(spec: string | IRowColumn | ICell): boolean {
219
+ return this._cell(spec)?.hasValue(this.state) === true;
220
+ }
221
+
222
+ /**
223
+ * Determines if supplied value is valid for a specific cell.
224
+ * @param spec - A `string` ({@link CellId | CellId}), {@link IRowColumn | RowColumn} or {@link ICell | ICell}
225
+ * describing the cell to be tested.
226
+ * @param value - The value to be tested.
227
+ * @returns `true` if `value` is valid for the requested cell, `false` if the value or the cell itself is invalid.
228
+ */
229
+ public isValidForCell(spec: string | IRowColumn | ICell, value: number): boolean {
230
+ return this._cell(spec)?.isValidValue(value, this.state) === true;
231
+ }
232
+
233
+ /**
234
+ * Gets the neighbor for a cell in a given direction using specified wrapping rules.
235
+ * @param spec - A `string` ({@link CellId | CellId}), {@link IRowColumn | RowColumn} or {@link ICell | ICell}
236
+ * describing the cell to be tested.
237
+ * @param direction - The direction of the desired neighbor.
238
+ * @param wrap - Wrapping rules to be applied.
239
+ * @returns `Success` with the requested {@link ICell | cell}, or `Failure` with details if an error occurs.
240
+ */
241
+ public getCellNeighbor(
242
+ spec: string | IRowColumn | ICell,
243
+ direction: NavigationDirection,
244
+ wrap: NavigationWrap
245
+ ): Result<ICell> {
246
+ return this._puzzle.getCellNeighbor(spec, direction, wrap);
247
+ }
248
+
249
+ /**
250
+ * Gets the {@link ICellContents | contents} for a specified cell.
251
+ * @param spec - A `string` ({@link CellId | CellId}), {@link IRowColumn | RowColumn} or {@link ICell | ICell}
252
+ * describing the cell to be queried.
253
+ * @returns `Success` with the {@link ICell | cell description} and {@link ICellContents | cell contents}, or
254
+ * `Failure` with details if an error occurs.
255
+ */
256
+ public getCellContents(spec: string | IRowColumn): Result<{ cell: ICell; contents: ICellContents }> {
257
+ return this._puzzle.getCellContents(spec, this.state);
258
+ }
259
+
260
+ /**
261
+ * Updates the value of a cell.
262
+ * @param spec - A `string`, {@link IRowColumn | row and column}, or {@link ICell | cell} identifying
263
+ * the cell to be updated.
264
+ * @param value - A new value for the cell.
265
+ * @returns `Success` with `this` if the update is applied, `Failure` with details if an error occurs.
266
+ */
267
+ public updateCellValue(spec: string | IRowColumn | ICell, value: number | undefined): Result<this> {
268
+ const idResult = Ids.cellId(spec);
269
+ return idResult.onSuccess((id) => {
270
+ const notes: number[] = [];
271
+ const update: ICellState = { id, value, notes };
272
+ return this._puzzle.updateValues(update, this.state).onSuccess((update) => {
273
+ this._addMove(update.cells);
274
+ this.state = update.to;
275
+ return succeed(this);
276
+ });
277
+ });
278
+ }
279
+
280
+ /**
281
+ * Updates the notes on a cell.
282
+ * @param spec - A `string`, {@link IRowColumn | row and column}, or {@link ICell | cell} identifying
283
+ * the cell to be updated.
284
+ * @param notes - New notes for the cell.
285
+ * @returns `Success` with `this` if the update is applied, `Failure` with details if an error occurs.
286
+ */
287
+ public updateCellNotes(spec: string | IRowColumn | ICell, notes: number[]): Result<this> {
288
+ const idResult = Ids.cellId(spec);
289
+ return idResult.onSuccess((id) => {
290
+ const value = undefined;
291
+ const update: ICellState = { id, value, notes };
292
+ return this._puzzle.updateNotes(update, this.state).onSuccess((update) => {
293
+ this._addMove(update.cells);
294
+ this.state = update.to;
295
+ return succeed(this);
296
+ });
297
+ });
298
+ }
299
+
300
+ /**
301
+ * Updates value & notes for multiple cells.
302
+ * @param updates - An array of {@link ICellState | cell state} objects, each describing
303
+ * one cell to be updated.
304
+ * @returns `Success` with `this` if the updates are applied, `Failure` with details if
305
+ * an error occurs.
306
+ */
307
+ public updateCells(updates: ICellState[]): Result<this> {
308
+ return this._puzzle.updateContents(updates, this.state).onSuccess((update) => {
309
+ this._addMove(update.cells);
310
+ this.state = update.to;
311
+ return succeed(this);
312
+ });
313
+ }
314
+
315
+ /**
316
+ * Determines if some {@link ICage | cage} contains a specific value.
317
+ * @param spec - A `string` ({@link CageId | CageId}) or {@link ICage | ICage}
318
+ * indicating the cage to be tested.
319
+ * @param value - The value to be tested.
320
+ * @returns `true` if the cage exists and contains the specified value,
321
+ * `false` otherwise.
322
+ */
323
+ public cageContainsValue(spec: string | ICage, value: number): boolean {
324
+ return this._cage(spec)?.containsValue(value, this.state) === true;
325
+ }
326
+
327
+ /**
328
+ * Determines the numbers currently present in some cage.
329
+ * @param spec - A `string` ({@link CageId | CageId}) or {@link ICage | ICage}
330
+ * indicating the cage to be tested.
331
+ * @returns A `Set<number>` containing all numbers present in the cage.
332
+ */
333
+ public cageContainedValues(spec: string | ICage): Set<number> {
334
+ return this._cage(spec)?.containedValues(this.state) ?? new Set<number>();
335
+ }
336
+
337
+ /**
338
+ * Undo a single move in this puzzle session.
339
+ * @returns `Success` with `this` if the undo is applied, or `Failure`
340
+ * with details if an error occurs.
341
+ */
342
+ public undo(): Result<this> {
343
+ if (!this.canUndo) {
344
+ return fail(`nothing to undo`);
345
+ }
346
+
347
+ return this._puzzle
348
+ .updateContents(
349
+ this._steps[this._nextStep - 1].updates.map((u) => u.from),
350
+ this.state
351
+ )
352
+ .onSuccess((update) => {
353
+ this._nextStep--;
354
+ this.state = update.to;
355
+ return succeed(this);
356
+ });
357
+ }
358
+
359
+ /**
360
+ * Redo a single move in this puzzle session.
361
+ * @returns `Success` with `this` if the redo is applied, or `Failure`
362
+ * with details if an error occurs.
363
+ */
364
+ public redo(): Result<this> {
365
+ if (!this.canRedo) {
366
+ return fail('nothing to redo');
367
+ }
368
+ return this._puzzle
369
+ .updateContents(
370
+ this._steps[this._nextStep].updates.map((u) => u.to),
371
+ this.state
372
+ )
373
+ .onSuccess((update) => {
374
+ this._nextStep++;
375
+ this.state = update.to;
376
+ return succeed(this);
377
+ });
378
+ }
379
+
380
+ /**
381
+ * Gets a string representation of this puzzle, one string
382
+ * per row.
383
+ */
384
+ public toStrings(): string[] {
385
+ return this._puzzle.toStrings(this.state);
386
+ }
387
+
388
+ private _addMove(updates: ICellUpdate[]): void {
389
+ if (this._nextStep < this._steps.length) {
390
+ this._steps[this._nextStep++] = { updates };
391
+ } else {
392
+ this._steps.push({ updates });
393
+ this._nextStep = this._steps.length;
394
+ }
395
+ this._numSteps = this._nextStep;
396
+ }
397
+
398
+ private _cage(spec: string | ICage): Cage | undefined {
399
+ if (spec instanceof Cage) {
400
+ return spec;
401
+ }
402
+ return Ids.cageId(spec)
403
+ .onSuccess((id) => {
404
+ return this._puzzle.getCage(id);
405
+ })
406
+ .orDefault();
407
+ }
408
+
409
+ private _cell(spec: string | IRowColumn | ICell): Cell | undefined {
410
+ if (spec instanceof Cell) {
411
+ return spec;
412
+ }
413
+ return this._puzzle.getCell(spec as string | IRowColumn).orDefault();
414
+ }
415
+ }
@@ -0,0 +1,108 @@
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
+
25
+ import { Result, fail, succeed } from '@fgv/ts-utils';
26
+ import { CellId, ICellContents, ICellState } from './common';
27
+
28
+ /**
29
+ * @public
30
+ */
31
+ export class PuzzleState {
32
+ /**
33
+ * @internal
34
+ */
35
+ protected readonly _cells: Map<CellId, ICellContents>;
36
+
37
+ /**
38
+ * @internal
39
+ */
40
+ protected constructor(from: Map<CellId, ICellContents>, updates?: ICellState[]) {
41
+ const entries = [...Array.from(from.entries()), ...PuzzleState._toEntries(updates)];
42
+ this._cells = new Map(entries);
43
+ }
44
+
45
+ /**
46
+ * Constructs a new {@link PuzzleState | PuzzleState}.
47
+ * @param cells - An array of {@link ICellState | CellState} used to initialize the state.
48
+ * @returns The new {@link PuzzleState | PuzzleState}.
49
+ */
50
+ public static create(cells: ICellState[]): Result<PuzzleState> {
51
+ return succeed(new PuzzleState(new Map(), cells));
52
+ }
53
+
54
+ /**
55
+ * Convert {@link ICellContents | CellContents} to `[`{@link CellId | CellId}`,` {@link ICellContents | CellContents}`]`
56
+ * tuple for `Map` construction.
57
+ * @param states - An array of {@link ICellContents | CellContents} to be converted.
58
+ * @returns The corresponding array of `[`{@link CellId | CellId}`,` {@link ICellContents | CellContents}`]`
59
+ * @internal
60
+ */
61
+ protected static _toEntries(states?: ICellState[]): [CellId, ICellContents][] {
62
+ /* c8 ignore next 3 - defense in depth */
63
+ if (!states) {
64
+ return [];
65
+ }
66
+ return states.map((state) => {
67
+ const { id, value, notes } = state;
68
+ return [id, { value, notes }];
69
+ });
70
+ }
71
+
72
+ /**
73
+ * Gets the contents of a cell specified by {@link CellId | id}.
74
+ * @param id - The {@link CellId | id} of the cell to be retrieved.
75
+ * @returns A {@link ICellContents | CellContents} with the contents of
76
+ * the requested cell.
77
+ */
78
+ public getCellContents(id: CellId): Result<ICellContents> {
79
+ const cell = this._cells.get(id);
80
+ return cell ? succeed(cell) : fail(`cell ${id} not found`);
81
+ }
82
+
83
+ /**
84
+ * Determines if some cell has an assigned value.
85
+ * @param id - The {@link CellId | id} of the cell to be tested.
86
+ * @returns `true` if the cell has a value, `false` if the cell
87
+ * is empty or invalid.
88
+ */
89
+ public hasValue(id: CellId): boolean {
90
+ const cell = this._cells.get(id);
91
+ return cell?.value !== undefined;
92
+ }
93
+
94
+ /**
95
+ * Creates a new {@link PuzzleState | PuzzleState} which corresponds
96
+ * to this state with updates applied.
97
+ * @param updates - An array of {@link ICellState | CellState} to be
98
+ * applied.
99
+ * @returns A new {@link PuzzleState} with updates applied.
100
+ */
101
+ public update(updates: ICellState[]): Result<PuzzleState> {
102
+ const updated = new PuzzleState(this._cells, updates);
103
+ if (updated._cells.size > this._cells.size) {
104
+ return fail(`update added cells`);
105
+ }
106
+ return succeed(updated);
107
+ }
108
+ }
@@ -0,0 +1,49 @@
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
+
25
+ import { Converter, Converters, Result } from '@fgv/ts-utils';
26
+ import { IPuzzlesFile } from './model';
27
+
28
+ import { JsonFile } from '@fgv/ts-json-base';
29
+ import { Converters as CommonConverters } from '../common';
30
+
31
+ /**
32
+ * Converts an arbitrary object to a {@link File.Model.IPuzzlesFile | IPuzzlesFile}.
33
+ * @public
34
+ */
35
+ export const puzzlesFile: Converter<IPuzzlesFile> = Converters.strictObject<IPuzzlesFile>({
36
+ puzzles: Converters.arrayOf(CommonConverters.puzzleDescription)
37
+ });
38
+
39
+ /**
40
+ * Loads an arbitrary JSON file and parses it to return a validated
41
+ * {@link File.Model.IPuzzlesFile | IPuzzlesFile}.
42
+ * @param path - String path to the file
43
+ * @returns `Success` with the resulting file, or `Failure` with details if an
44
+ * error occurs.
45
+ * @public
46
+ */
47
+ export function loadJsonPuzzlesFileSync(path: string): Result<IPuzzlesFile> {
48
+ return JsonFile.convertJsonFileSync(path, puzzlesFile);
49
+ }
@@ -0,0 +1,28 @@
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
+
25
+ import * as Converters from './converters';
26
+ import * as Model from './model';
27
+
28
+ export { Converters, Model };
@@ -0,0 +1,33 @@
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
+
25
+ import { IPuzzleDescription } from '../common';
26
+
27
+ /**
28
+ * Parsed file containing a collection of puzzles.
29
+ * @public
30
+ */
31
+ export interface IPuzzlesFile {
32
+ puzzles: IPuzzleDescription[];
33
+ }
@@ -0,0 +1,50 @@
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
+
25
+ import { Result, fail } from '@fgv/ts-utils';
26
+
27
+ import { IPuzzleDescription, Puzzle } from '../common';
28
+ import { KillerSudokuPuzzle } from './killerSudokuPuzzle';
29
+ import { SudokuPuzzle } from './sudokuPuzzle';
30
+ import { SudokuXPuzzle } from './sudokuXPuzzle';
31
+
32
+ /**
33
+ * Static class to instantiate any puzzle from a {@link PuzzleDescription | puzzle description}.
34
+ * @internal
35
+ */
36
+ export class AnyPuzzle {
37
+ public static create(puzzle: IPuzzleDescription): Result<Puzzle> {
38
+ switch (puzzle.type) {
39
+ case 'sudoku':
40
+ return SudokuPuzzle.create(puzzle);
41
+ case 'sudoku-x':
42
+ return SudokuXPuzzle.create(puzzle);
43
+ case 'killer-sudoku':
44
+ return KillerSudokuPuzzle.create(puzzle);
45
+ /* c8 ignore next 2 */
46
+ default:
47
+ return fail(`Puzzle '${puzzle.description}' unsupported type ${puzzle.type}`);
48
+ }
49
+ }
50
+ }