@fgv/ts-sudoku-lib 5.0.0-21 → 5.0.0-23
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/package.json +5 -5
- package/src/index.ts +0 -30
- package/src/packlets/collections/collections.ts +0 -124
- package/src/packlets/collections/data/puzzles.json +0 -61
- package/src/packlets/collections/index.ts +0 -25
- package/src/packlets/common/cage.ts +0 -98
- package/src/packlets/common/cell.ts +0 -116
- package/src/packlets/common/common.ts +0 -120
- package/src/packlets/common/converters.ts +0 -84
- package/src/packlets/common/ids.ts +0 -98
- package/src/packlets/common/index.ts +0 -37
- package/src/packlets/common/model.ts +0 -39
- package/src/packlets/common/public.ts +0 -100
- package/src/packlets/common/puzzle.ts +0 -523
- package/src/packlets/common/puzzleSession.ts +0 -415
- package/src/packlets/common/puzzleState.ts +0 -108
- package/src/packlets/file/converters.ts +0 -49
- package/src/packlets/file/index.ts +0 -28
- package/src/packlets/file/model.ts +0 -33
- package/src/packlets/puzzles/anyPuzzle.ts +0 -50
- package/src/packlets/puzzles/index.ts +0 -30
- package/src/packlets/puzzles/killerSudokuPuzzle.ts +0 -151
- package/src/packlets/puzzles/sudokuPuzzle.ts +0 -46
- package/src/packlets/puzzles/sudokuXPuzzle.ts +0 -67
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgv/ts-sudoku-lib",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-23",
|
|
4
4
|
"description": "Sudoku rules library",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "dist/ts-sudoku-lib.d.ts",
|
|
@@ -43,12 +43,12 @@
|
|
|
43
43
|
"@rushstack/eslint-config": "~4.4.0",
|
|
44
44
|
"@rushstack/heft-jest-plugin": "~0.16.11",
|
|
45
45
|
"eslint-plugin-tsdoc": "~0.4.0",
|
|
46
|
-
"@fgv/ts-utils": "5.0.0-
|
|
47
|
-
"@fgv/ts-utils-jest": "5.0.0-
|
|
48
|
-
"@fgv/ts-json-base": "5.0.0-
|
|
46
|
+
"@fgv/ts-utils": "5.0.0-23",
|
|
47
|
+
"@fgv/ts-utils-jest": "5.0.0-23",
|
|
48
|
+
"@fgv/ts-json-base": "5.0.0-23"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"@fgv/ts-utils": "5.0.0-
|
|
51
|
+
"@fgv/ts-utils": "5.0.0-23"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
54
54
|
"build": "heft build --clean",
|
package/src/index.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
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 File from './packlets/file';
|
|
26
|
-
import * as Puzzles from './packlets/puzzles';
|
|
27
|
-
|
|
28
|
-
export * from './packlets/collections';
|
|
29
|
-
export * from './packlets/common';
|
|
30
|
-
export { File, Puzzles };
|
|
@@ -1,124 +0,0 @@
|
|
|
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 FileData from '../file';
|
|
26
|
-
import * as Puzzles from '../puzzles';
|
|
27
|
-
|
|
28
|
-
import { Result, captureResult, fail, succeed } from '@fgv/ts-utils';
|
|
29
|
-
|
|
30
|
-
import { IPuzzleDescription, PuzzleSession } from '../common';
|
|
31
|
-
import DefaultPuzzles from './data/puzzles.json';
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* A collection of puzzles of various types.
|
|
35
|
-
* @public
|
|
36
|
-
*/
|
|
37
|
-
export class PuzzleCollection {
|
|
38
|
-
/**
|
|
39
|
-
* All puzzles in the collection.
|
|
40
|
-
*/
|
|
41
|
-
public readonly puzzles: readonly IPuzzleDescription[];
|
|
42
|
-
|
|
43
|
-
private readonly _byId: Map<string, IPuzzleDescription>;
|
|
44
|
-
|
|
45
|
-
private constructor(puzzles: FileData.Model.IPuzzlesFile) {
|
|
46
|
-
this.puzzles = puzzles.puzzles;
|
|
47
|
-
this._byId = new Map(
|
|
48
|
-
this.puzzles
|
|
49
|
-
.map((p): [string | undefined, IPuzzleDescription] => [p.id, p])
|
|
50
|
-
.filter((p): p is [string, IPuzzleDescription] => p !== undefined)
|
|
51
|
-
);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Creates a new puzzle from a loaded {@link File.Model.IPuzzlesFile | PuzzlesFile}
|
|
56
|
-
* @param from - The {@link File.Model.IPuzzlesFile | puzzles file} to be loaded.
|
|
57
|
-
* @returns `Success` with the resulting {@link PuzzleCollection | PuzzleCollection}
|
|
58
|
-
* or `Failure` with details if an error occurs.
|
|
59
|
-
*/
|
|
60
|
-
public static create(from: FileData.Model.IPuzzlesFile): Result<PuzzleCollection> {
|
|
61
|
-
return captureResult(() => new PuzzleCollection(from));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Creates a new puzzle from a JSON file.
|
|
66
|
-
* @param path - path to the JSON file to be loaded.
|
|
67
|
-
* @returns `Success` with the resulting {@link PuzzleCollection | PuzzleCollection}
|
|
68
|
-
* or `Failure` with details if an error occurs.
|
|
69
|
-
*/
|
|
70
|
-
public static load(path: string): Result<PuzzleCollection> {
|
|
71
|
-
return FileData.Converters.loadJsonPuzzlesFileSync(path).onSuccess(PuzzleCollection.create);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Gets a puzzle by id from this collection.
|
|
76
|
-
* @param id - The string ID of the puzzle to be returned.
|
|
77
|
-
* @returns `Success` with the requested {@link PuzzleSession | puzzle}, or
|
|
78
|
-
* `Failure` with details if an error occurs.
|
|
79
|
-
*/
|
|
80
|
-
public getPuzzle(id: string): Result<PuzzleSession> {
|
|
81
|
-
return this.getDescription(id)
|
|
82
|
-
.onSuccess((desc) => {
|
|
83
|
-
return Puzzles.Any.create(desc);
|
|
84
|
-
})
|
|
85
|
-
.onSuccess((puzzle) => {
|
|
86
|
-
return PuzzleSession.create(puzzle);
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Gets a puzzle by id from this collection.
|
|
92
|
-
* @param id - The string ID of the puzzle to be returned.
|
|
93
|
-
* @returns `Success` with the requested {@link PuzzleSession | puzzle}, or
|
|
94
|
-
* `Failure` with details if an error occurs.
|
|
95
|
-
*/
|
|
96
|
-
public getDescription(id: string): Result<IPuzzleDescription> {
|
|
97
|
-
const desc = this._byId.get(id);
|
|
98
|
-
if (!desc) {
|
|
99
|
-
return fail(`Puzzle "${id}" not found`);
|
|
100
|
-
}
|
|
101
|
-
return succeed(desc);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Get well-known {@link PuzzleCollection | puzzle collections}.
|
|
107
|
-
* @public
|
|
108
|
-
*/
|
|
109
|
-
export class PuzzleCollections {
|
|
110
|
-
private static _default: PuzzleCollection | undefined = undefined;
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* The default {@link PuzzleCollection | puzzle collection}.
|
|
114
|
-
*/
|
|
115
|
-
public static get default(): PuzzleCollection {
|
|
116
|
-
if (!PuzzleCollections._default) {
|
|
117
|
-
PuzzleCollections._default = FileData.Converters.puzzlesFile
|
|
118
|
-
.convert(DefaultPuzzles)
|
|
119
|
-
.onSuccess(PuzzleCollection.create)
|
|
120
|
-
.orThrow();
|
|
121
|
-
}
|
|
122
|
-
return PuzzleCollections._default;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"puzzles": [
|
|
3
|
-
{
|
|
4
|
-
"id": "hidden-pair",
|
|
5
|
-
"description": "hidden pair sample",
|
|
6
|
-
"type": "sudoku",
|
|
7
|
-
"level": 1,
|
|
8
|
-
"rows": 9,
|
|
9
|
-
"cols": 9,
|
|
10
|
-
"cells": [
|
|
11
|
-
".........",
|
|
12
|
-
"9.46.7...",
|
|
13
|
-
".768.41..",
|
|
14
|
-
"3.97.1.8.",
|
|
15
|
-
"7.8...3.1",
|
|
16
|
-
".513.87.2",
|
|
17
|
-
"..75.261.",
|
|
18
|
-
"..54.32.8",
|
|
19
|
-
"........."
|
|
20
|
-
]
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
"id": "almost-done",
|
|
24
|
-
"description": "nearly complete puzzle missing only 1 in cell A1",
|
|
25
|
-
"type": "sudoku",
|
|
26
|
-
"level": 1,
|
|
27
|
-
"rows": 9,
|
|
28
|
-
"cols": 9,
|
|
29
|
-
"cells": ".46728539837549126259316748685297413312684957794153862428965371571832694963471285"
|
|
30
|
-
},
|
|
31
|
-
{
|
|
32
|
-
"id": "sudoku-x-example",
|
|
33
|
-
"description": "sample x-sudoku puzzle",
|
|
34
|
-
"type": "sudoku-x",
|
|
35
|
-
"level": 1,
|
|
36
|
-
"rows": 9,
|
|
37
|
-
"cols": 9,
|
|
38
|
-
"cells": "4.....13....6.1.....7..29...76.....2....3..9.9.1....577...1.6..3...5.7...4......1"
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
"id": "killer-insane",
|
|
42
|
-
"description": "Insane (3208329) example from djape.net",
|
|
43
|
-
"type": "killer-sudoku",
|
|
44
|
-
"level": 100,
|
|
45
|
-
"rows": 9,
|
|
46
|
-
"cols": 9,
|
|
47
|
-
"cells": [
|
|
48
|
-
"ABCCCDDDE",
|
|
49
|
-
"ABFFGGGDE",
|
|
50
|
-
"HIJKGGLLL",
|
|
51
|
-
"HIJKMGLNN",
|
|
52
|
-
"HOPPMQQNR",
|
|
53
|
-
"OOSTMUVWR",
|
|
54
|
-
"SSSTTUVWR",
|
|
55
|
-
"XYTTTZZab",
|
|
56
|
-
"XYYYcccab",
|
|
57
|
-
"|A11,B09,C09,D20,E16,F17,G30,H17,I13,J09,K11,L16,M16,N11,O16,P07,Q11,R10,S14,T39,U08,V17,W16,X06,Y26,Z06,a09,b09,c11"
|
|
58
|
-
]
|
|
59
|
-
}
|
|
60
|
-
]
|
|
61
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
export * from './collections';
|
|
@@ -1,98 +0,0 @@
|
|
|
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 } from '@fgv/ts-utils';
|
|
26
|
-
import { CageId, CageType, CellId } from './common';
|
|
27
|
-
import { ICage } from './public';
|
|
28
|
-
import { PuzzleState } from './puzzleState';
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @internal
|
|
32
|
-
*/
|
|
33
|
-
export class Cage implements ICage {
|
|
34
|
-
public readonly id: CageId;
|
|
35
|
-
public readonly cageType: CageType;
|
|
36
|
-
public readonly total: number;
|
|
37
|
-
|
|
38
|
-
protected readonly _cellIds: CellId[];
|
|
39
|
-
private _values: Set<number>;
|
|
40
|
-
|
|
41
|
-
private constructor(id: CageId, type: CageType, total: number, cells: CellId[]) {
|
|
42
|
-
this.id = id;
|
|
43
|
-
this.cageType = type;
|
|
44
|
-
this.total = total;
|
|
45
|
-
this._cellIds = Array.from(cells);
|
|
46
|
-
this._values = new Set();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
public get cellIds(): CellId[] {
|
|
50
|
-
return Array.from(this._cellIds);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public get numCells(): number {
|
|
54
|
-
return this._cellIds.length;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
public static create(id: CageId, type: CageType, total: number, cells: CellId[]): Result<Cage> {
|
|
58
|
-
return captureResult(() => new Cage(id, type, total, cells));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
public containsCell(id: CellId): boolean {
|
|
62
|
-
return this._cellIds.includes(id);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public containsValue(value: number, state: PuzzleState, ignore?: CellId[]): boolean {
|
|
66
|
-
for (const cellId of this._cellIds) {
|
|
67
|
-
if (!ignore?.includes(cellId)) {
|
|
68
|
-
/* c8 ignore next - defense in depth, ? should never happen */
|
|
69
|
-
if (state.getCellContents(cellId).orDefault()?.value === value) {
|
|
70
|
-
return true;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return false;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public containedValues(state: PuzzleState): Set<number> {
|
|
78
|
-
const values = new Set<number>();
|
|
79
|
-
for (const cellId of this._cellIds) {
|
|
80
|
-
/* c8 ignore next - defense in depth, ? should never happen */
|
|
81
|
-
const value = state.getCellContents(cellId).orDefault()?.value;
|
|
82
|
-
if (value !== undefined) {
|
|
83
|
-
values.add(value);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return values;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
public toString(state?: PuzzleState): string {
|
|
90
|
-
return this._cellIds
|
|
91
|
-
.map((id) => {
|
|
92
|
-
/* c8 ignore next - defense in depth, ? should never happen */
|
|
93
|
-
const value = state?.getCellContents(id).orDefault()?.value;
|
|
94
|
-
return value ? String(value) : '.';
|
|
95
|
-
})
|
|
96
|
-
.join('');
|
|
97
|
-
}
|
|
98
|
-
}
|
|
@@ -1,116 +0,0 @@
|
|
|
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 { Cage } from './cage';
|
|
27
|
-
import { CellId, ICellContents, ICellState } from './common';
|
|
28
|
-
import { ICell } from './public';
|
|
29
|
-
import { PuzzleState } from './puzzleState';
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @internal
|
|
33
|
-
*/
|
|
34
|
-
export interface ICellInit {
|
|
35
|
-
readonly id: CellId;
|
|
36
|
-
readonly row: number;
|
|
37
|
-
readonly col: number;
|
|
38
|
-
readonly immutableValue?: number;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @internal
|
|
43
|
-
*/
|
|
44
|
-
export class Cell implements ICellInit, ICell {
|
|
45
|
-
public readonly id: CellId;
|
|
46
|
-
public readonly row: number;
|
|
47
|
-
public readonly col: number;
|
|
48
|
-
public readonly cages: readonly Cage[];
|
|
49
|
-
|
|
50
|
-
public readonly immutable: boolean;
|
|
51
|
-
public readonly immutableValue?: number;
|
|
52
|
-
|
|
53
|
-
public constructor(init: ICellInit, cages: readonly Cage[]) {
|
|
54
|
-
this.id = init.id;
|
|
55
|
-
this.row = init.row;
|
|
56
|
-
this.col = init.col;
|
|
57
|
-
this.cages = [...cages];
|
|
58
|
-
this.immutableValue = init.immutableValue;
|
|
59
|
-
this.immutable = init.immutableValue !== undefined;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public isValid(state: PuzzleState): boolean {
|
|
63
|
-
if (this.immutable) {
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
const cell = state.getCellContents(this.id).orDefault();
|
|
67
|
-
return cell !== undefined && this.isValidValue(cell.value, state);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
public hasValue(state: PuzzleState): boolean {
|
|
71
|
-
if (this.immutable) {
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
const cell = state.getCellContents(this.id).orDefault();
|
|
75
|
-
/* c8 ignore next - defense in depth ? should never happen */
|
|
76
|
-
return cell?.value !== undefined;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public isValidValue(value: number | undefined, state: PuzzleState): boolean {
|
|
80
|
-
if (this.immutable) {
|
|
81
|
-
return false;
|
|
82
|
-
} else if (value === undefined) {
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
return !this.cages.find((cage) => cage.containsValue(value, state, [this.id]));
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public update(value: number | undefined, notes: number[]): Result<ICellState> {
|
|
89
|
-
if (this.immutable) {
|
|
90
|
-
return fail(`cannot set value "${value}" in immutable cell ${this.id}`);
|
|
91
|
-
}
|
|
92
|
-
return succeed({
|
|
93
|
-
id: this.id,
|
|
94
|
-
value,
|
|
95
|
-
notes
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
public updateValue(value: number | undefined, state: PuzzleState): Result<ICellState> {
|
|
100
|
-
return state.getCellContents(this.id).onSuccess((contents: ICellContents) => {
|
|
101
|
-
return this.update(value, contents.notes);
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
public updateNotes(notes: number[], state: PuzzleState): Result<ICellState> {
|
|
106
|
-
return state.getCellContents(this.id).onSuccess((contents: ICellContents) => {
|
|
107
|
-
return this.update(contents.value, notes);
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
public toString(state?: PuzzleState): string {
|
|
112
|
-
const contents = state?.getCellContents(this.id).orDefault();
|
|
113
|
-
const value = this.immutableValue ?? contents?.value;
|
|
114
|
-
return value && value >= 1 && value <= 9 ? String(value) : '.';
|
|
115
|
-
}
|
|
116
|
-
}
|
|
@@ -1,120 +0,0 @@
|
|
|
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 { Brand } from '@fgv/ts-utils';
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Nominal identifier for a single {@Link ICell | cell} in a {@link PuzzleSession | puzzle}.
|
|
29
|
-
* @public
|
|
30
|
-
*/
|
|
31
|
-
export type CellId = Brand<string, 'CellId'>;
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Nominal identifier for a single {@Link ICage | cage} in a {@link PuzzleSession | puzzle}.
|
|
35
|
-
* @public
|
|
36
|
-
*/
|
|
37
|
-
export type CageId = Brand<string, 'CageId'>;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Identifies the type of a {@Link ICage | cage}.
|
|
41
|
-
* @public
|
|
42
|
-
*/
|
|
43
|
-
export type CageType = 'row' | 'column' | 'section' | 'x' | 'killer';
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* The row/column coordinate of a single {@Link ICell | cell} in a {@link PuzzleSession | puzzle}.
|
|
47
|
-
* @public
|
|
48
|
-
*/
|
|
49
|
-
export interface IRowColumn {
|
|
50
|
-
row: number;
|
|
51
|
-
col: number;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* The contents of a single {@Link ICell | cell} in a {@link PuzzleSession | puzzle}.
|
|
56
|
-
* @public
|
|
57
|
-
*/
|
|
58
|
-
export interface ICellContents {
|
|
59
|
-
/**
|
|
60
|
-
* The value of the {@link ICell | cell}, or `undefined` if no value has been assigned.
|
|
61
|
-
*/
|
|
62
|
-
readonly value?: number;
|
|
63
|
-
/**
|
|
64
|
-
* Any notes associated with the {@link ICell | cell}.
|
|
65
|
-
*/
|
|
66
|
-
readonly notes: number[];
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Describes the state of or a state update for a single {@link ICell |cell} in a
|
|
71
|
-
* {@link PuzzleSession | puzzle}.
|
|
72
|
-
* @public
|
|
73
|
-
*/
|
|
74
|
-
|
|
75
|
-
export interface ICellState extends ICellContents {
|
|
76
|
-
readonly id: CellId;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Describes the rules that apply to the puzzle.
|
|
81
|
-
* @public
|
|
82
|
-
*/
|
|
83
|
-
export type PuzzleType = 'killer-sudoku' | 'sudoku' | 'sudoku-x';
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* All supported public types.
|
|
87
|
-
* @public
|
|
88
|
-
*/
|
|
89
|
-
export const allPuzzleTypes: PuzzleType[] = ['killer-sudoku', 'sudoku', 'sudoku-x'];
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* The minimum and maximum possible values for a {@link ICage | cage}, by cage size in
|
|
93
|
-
* {@link ICell | cells}.
|
|
94
|
-
* @public
|
|
95
|
-
*/
|
|
96
|
-
export const totalsByCageSize: readonly { min: number; max: number }[] = [
|
|
97
|
-
{ min: 0, max: 0 },
|
|
98
|
-
{ min: 1, max: 9 },
|
|
99
|
-
{ min: 3, max: 17 },
|
|
100
|
-
{ min: 6, max: 24 },
|
|
101
|
-
{ min: 10, max: 30 },
|
|
102
|
-
{ min: 15, max: 35 },
|
|
103
|
-
{ min: 21, max: 39 },
|
|
104
|
-
{ min: 28, max: 42 },
|
|
105
|
-
{ min: 36, max: 44 },
|
|
106
|
-
{ min: 45, max: 45 }
|
|
107
|
-
];
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Navigation direction within a puzzle.
|
|
111
|
-
* @public
|
|
112
|
-
*
|
|
113
|
-
*/
|
|
114
|
-
export type NavigationDirection = 'down' | 'left' | 'right' | 'up';
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Wrapping rules when navigating within a puzzle.
|
|
118
|
-
* @public
|
|
119
|
-
*/
|
|
120
|
-
export type NavigationWrap = 'none' | 'wrap-around' | 'wrap-next';
|
|
@@ -1,84 +0,0 @@
|
|
|
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, fail, succeed } from '@fgv/ts-utils';
|
|
26
|
-
import { CageId, CellId, PuzzleType, allPuzzleTypes } from './common';
|
|
27
|
-
import { IPuzzleDescription } from './model';
|
|
28
|
-
|
|
29
|
-
const cageIdRegExp: RegExp = /^(R[A-Z]$)|(C[0-9]$)|(S[A-Z][0-9]$)|(X[1-2]$)|(K[A-Za-z]$)/;
|
|
30
|
-
|
|
31
|
-
function validateCageId(from: string): Result<CageId> {
|
|
32
|
-
if (cageIdRegExp.test(from)) {
|
|
33
|
-
return succeed(from as CageId);
|
|
34
|
-
}
|
|
35
|
-
return fail(`malformed cage ID "${from}" (expected "R[A-Z]", "C[0-9]" or "S[A-Z][0-9]")`);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const cellIdRegExp: RegExp = /^[A-Z][0-9]$/;
|
|
39
|
-
function validateCellId(from: string): Result<CellId> {
|
|
40
|
-
if (cellIdRegExp.test(from)) {
|
|
41
|
-
return succeed(from as CellId);
|
|
42
|
-
}
|
|
43
|
-
return fail(`malformed cell ID "${from}" (expected "[A-Z][0-9]")`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Converts an arbitrary value to a {@link CageId | CageId}.
|
|
48
|
-
* @public
|
|
49
|
-
*/
|
|
50
|
-
export const cageId: Converter<CageId> = Converters.string.map(validateCageId);
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Converts an arbitrary value to a {@link CellId | CellId}.
|
|
54
|
-
* @public
|
|
55
|
-
*/
|
|
56
|
-
export const cellId: Converter<CellId> = Converters.string.map(validateCellId);
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Converts an arbitrary value to a {@link PuzzleType | PuzzleType}.
|
|
60
|
-
* @public
|
|
61
|
-
*/
|
|
62
|
-
export const puzzleType: Converter<
|
|
63
|
-
PuzzleType,
|
|
64
|
-
ReadonlyArray<PuzzleType>
|
|
65
|
-
> = Converters.enumeratedValue<PuzzleType>(allPuzzleTypes);
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Converts an arbitrary object to a {@link IPuzzleDescription | IPuzzleDescription}.
|
|
69
|
-
* @public
|
|
70
|
-
*/
|
|
71
|
-
export const puzzleDescription: Converter<IPuzzleDescription> = Converters.strictObject<IPuzzleDescription>(
|
|
72
|
-
{
|
|
73
|
-
id: Converters.string,
|
|
74
|
-
description: Converters.string,
|
|
75
|
-
type: puzzleType,
|
|
76
|
-
level: Converters.number,
|
|
77
|
-
rows: Converters.number,
|
|
78
|
-
cols: Converters.number,
|
|
79
|
-
cells: Converters.oneOf([Converters.string, Converters.stringArray.map((s) => succeed(s.join('')))])
|
|
80
|
-
},
|
|
81
|
-
{
|
|
82
|
-
optionalFields: ['id']
|
|
83
|
-
}
|
|
84
|
-
);
|