@luckyfoxdesign/sudoku-generator 1.0.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/LICENSE +21 -0
- package/README.md +369 -0
- package/dist/index.cjs +8 -0
- package/dist/index.global.js +8 -0
- package/dist/index.js +8 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lucky Fox Design
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
# Sudoku Generator
|
|
2
|
+
|
|
3
|
+
A lightweight Sudoku puzzle generator that creates complete solutions and playable puzzles using a backtracking algorithm. Works in browser and Node.js environments.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@luckyfoxdesign/sudoku-generator) [](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- 🎲 Generates randomized, valid Sudoku puzzles and solutions
|
|
10
|
+
- 🧩 Creates playable puzzles with ~50% cells removed
|
|
11
|
+
- ⚡ Fast generation using backtracking algorithm
|
|
12
|
+
- 🌐 Works in browser and Node.js
|
|
13
|
+
- 📦 Zero dependencies
|
|
14
|
+
- 🔧 Multiple export formats (ESM, CommonJS, IIFE)
|
|
15
|
+
- ✅ Fully tested
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @luckyfoxdesign/sudoku-generator
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quick Start
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
import { generateSudokuGrid, generateCompleteSudokuGrid } from '@luckyfoxdesign/sudoku-generator';
|
|
27
|
+
|
|
28
|
+
// Generate a puzzle (with empty cells marked as 0)
|
|
29
|
+
const puzzle = generateSudokuGrid();
|
|
30
|
+
console.log(puzzle);
|
|
31
|
+
// [[5, 0, 4, 6, 0, 8, 9, 0, 2],
|
|
32
|
+
// [6, 7, 0, 1, 9, 0, 3, 4, 0],
|
|
33
|
+
// ...]
|
|
34
|
+
|
|
35
|
+
// Generate a complete solution (all cells filled)
|
|
36
|
+
const solution = generateCompleteSudokuGrid();
|
|
37
|
+
console.log(solution);
|
|
38
|
+
// [[5, 3, 4, 6, 7, 8, 9, 1, 2],
|
|
39
|
+
// [6, 7, 2, 1, 9, 5, 3, 4, 8],
|
|
40
|
+
// ...]
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
### Browser (CDN)
|
|
46
|
+
|
|
47
|
+
```html
|
|
48
|
+
<!DOCTYPE html>
|
|
49
|
+
<html>
|
|
50
|
+
<head>
|
|
51
|
+
<title>Sudoku Generator</title>
|
|
52
|
+
</head>
|
|
53
|
+
<body>
|
|
54
|
+
<script src="https://unpkg.com/@luckyfoxdesign/sudoku-generator/dist/index.global.js"></script>
|
|
55
|
+
<script>
|
|
56
|
+
// Generate a puzzle
|
|
57
|
+
const puzzle = Sudoku.generateSudokuGrid();
|
|
58
|
+
console.log(puzzle);
|
|
59
|
+
|
|
60
|
+
// Generate a complete solution
|
|
61
|
+
const solution = Sudoku.generateCompleteSudokuGrid();
|
|
62
|
+
console.log(solution);
|
|
63
|
+
</script>
|
|
64
|
+
</body>
|
|
65
|
+
</html>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### React / Vue / Svelte
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
import { generateSudokuGrid } from '@luckyfoxdesign/sudoku-generator';
|
|
72
|
+
|
|
73
|
+
function SudokuGame() {
|
|
74
|
+
const [grid, setGrid] = useState(generateSudokuGrid());
|
|
75
|
+
|
|
76
|
+
return (
|
|
77
|
+
<div>
|
|
78
|
+
{grid.map((row, i) => (
|
|
79
|
+
<div key={i}>
|
|
80
|
+
{row.map((cell, j) => (
|
|
81
|
+
<span key={j}>
|
|
82
|
+
{cell === 0 ? '_' : cell}
|
|
83
|
+
</span>
|
|
84
|
+
))}
|
|
85
|
+
</div>
|
|
86
|
+
))}
|
|
87
|
+
</div>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### Node.js (ESM)
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
import { generateSudokuGrid, generateCompleteSudokuGrid } from '@luckyfoxdesign/sudoku-generator';
|
|
96
|
+
|
|
97
|
+
const puzzle = generateSudokuGrid();
|
|
98
|
+
const solution = generateCompleteSudokuGrid();
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Node.js (CommonJS)
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
const { generateSudokuGrid, generateCompleteSudokuGrid } = require('@luckyfoxdesign/sudoku-generator');
|
|
105
|
+
|
|
106
|
+
const puzzle = generateSudokuGrid();
|
|
107
|
+
const solution = generateCompleteSudokuGrid();
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## API
|
|
111
|
+
|
|
112
|
+
### `generateSudokuGrid()`
|
|
113
|
+
|
|
114
|
+
Generates a playable Sudoku puzzle with some cells removed (marked as 0).
|
|
115
|
+
Approximately 50% of cells are removed, with first columns of each 3×3 block always filled.
|
|
116
|
+
|
|
117
|
+
**Returns:** `number[][]` - A 9×9 2D array where 0 = empty cell, 1-9 = filled cell
|
|
118
|
+
|
|
119
|
+
**Example:**
|
|
120
|
+
```javascript
|
|
121
|
+
const puzzle = generateSudokuGrid();
|
|
122
|
+
console.log(puzzle[0][0]); // 5 or 0 (empty)
|
|
123
|
+
console.log(puzzle[0][3]); // 7 (always filled - column 3 never empty)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### `generateCompleteSudokuGrid()`
|
|
127
|
+
|
|
128
|
+
Generates a complete Sudoku solution with all cells filled (no empty cells).
|
|
129
|
+
|
|
130
|
+
**Returns:** `number[][]` - A 9×9 2D array where each cell contains a number from 1-9
|
|
131
|
+
|
|
132
|
+
**Example:**
|
|
133
|
+
```javascript
|
|
134
|
+
const solution = generateCompleteSudokuGrid();
|
|
135
|
+
console.log(solution[0][0]); // 5 (always filled)
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### `generateSudokuGridWithMetadata()`
|
|
139
|
+
|
|
140
|
+
Generates a Sudoku puzzle with metadata for each cell. Useful for advanced Sudoku solving/generation algorithms.
|
|
141
|
+
|
|
142
|
+
**Returns:** `Object[][]` - A 9×9 array of cell objects
|
|
143
|
+
|
|
144
|
+
**Cell Object Structure:**
|
|
145
|
+
```typescript
|
|
146
|
+
{
|
|
147
|
+
chosenValue: number; // The number in cell (0 = empty, 1-9 = filled)
|
|
148
|
+
removedValues: number[]; // Values tried and rejected during generation
|
|
149
|
+
gameSet: Set<number>; // Available values for this cell
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
**Example:**
|
|
154
|
+
```javascript
|
|
155
|
+
const grid = generateSudokuGridWithMetadata();
|
|
156
|
+
console.log(grid[0][0].chosenValue); // 5 or 0
|
|
157
|
+
console.log(grid[0][0].removedValues); // [2, 7]
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Puzzle Generation Details
|
|
161
|
+
|
|
162
|
+
### Cell Removal Strategy
|
|
163
|
+
|
|
164
|
+
When generating puzzles:
|
|
165
|
+
- **~50% of cells** are randomly removed
|
|
166
|
+
- **Columns 0, 3, and 6** (first column of each 3×3 block) are **never removed**
|
|
167
|
+
- This ensures structural integrity and solvability
|
|
168
|
+
|
|
169
|
+
### Example Grid Structure
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
[5] 0 4 [6] 0 8 [9] 0 2 ← Columns 0,3,6 always filled
|
|
173
|
+
[6] 7 0 [1] 9 0 [3] 4 0
|
|
174
|
+
[8] 0 9 [5] 0 2 [7] 0 6
|
|
175
|
+
[2] 0 0 [8] 0 7 [4] 0 0
|
|
176
|
+
...
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Development
|
|
180
|
+
|
|
181
|
+
### Setup
|
|
182
|
+
|
|
183
|
+
```bash
|
|
184
|
+
# Clone the repository
|
|
185
|
+
git clone https://github.com/luckyfoxdesign/sudoku-generator.git
|
|
186
|
+
cd sudoku-generator
|
|
187
|
+
|
|
188
|
+
# Install dependencies
|
|
189
|
+
npm install
|
|
190
|
+
|
|
191
|
+
# Build the project
|
|
192
|
+
npm run build
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Build
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
npm run build
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
This creates three files in `dist/`:
|
|
202
|
+
- `index.js` - ESM format for React/Vue/Svelte
|
|
203
|
+
- `index.cjs` - CommonJS format for Node.js
|
|
204
|
+
- `index.global.js` - IIFE format for browser `<script>` tags
|
|
205
|
+
|
|
206
|
+
### Testing
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
npm test
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Tests cover:
|
|
213
|
+
- ✅ Puzzle generation (with empty cells)
|
|
214
|
+
- ✅ Complete solution generation (no empty cells)
|
|
215
|
+
- ✅ Grid structure validation
|
|
216
|
+
- ✅ Sudoku rules (rows, columns, 3×3 blocks)
|
|
217
|
+
- ✅ Performance benchmarks
|
|
218
|
+
- ✅ Edge cases
|
|
219
|
+
|
|
220
|
+
## Publishing (for maintainers)
|
|
221
|
+
|
|
222
|
+
### Pre-publish Checklist
|
|
223
|
+
|
|
224
|
+
1. Run tests:
|
|
225
|
+
```bash
|
|
226
|
+
npm test
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
2. Build the project:
|
|
230
|
+
```bash
|
|
231
|
+
npm run build
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
3. Check what will be published:
|
|
235
|
+
```bash
|
|
236
|
+
npm pack --dry-run
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Publishing to NPM
|
|
240
|
+
|
|
241
|
+
1. Login to NPM:
|
|
242
|
+
```bash
|
|
243
|
+
npm login
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
2. Publish the package:
|
|
247
|
+
```bash
|
|
248
|
+
npm publish --access public
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Version Updates
|
|
252
|
+
|
|
253
|
+
```bash
|
|
254
|
+
# Patch (1.0.0 → 1.0.1)
|
|
255
|
+
npm version patch
|
|
256
|
+
|
|
257
|
+
# Minor (1.0.0 → 1.1.0)
|
|
258
|
+
npm version minor
|
|
259
|
+
|
|
260
|
+
# Major (1.0.0 → 2.0.0)
|
|
261
|
+
npm version major
|
|
262
|
+
|
|
263
|
+
# Then publish
|
|
264
|
+
npm publish --access public
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### Post-publish Verification
|
|
268
|
+
|
|
269
|
+
Check the package:
|
|
270
|
+
- NPM: https://www.npmjs.com/package/@luckyfoxdesign/sudoku-generator
|
|
271
|
+
- unpkg CDN: https://unpkg.com/@luckyfoxdesign/sudoku-generator/dist/index.global.js
|
|
272
|
+
- jsDelivr CDN: https://cdn.jsdelivr.net/npm/@luckyfoxdesign/sudoku-generator/dist/index.global.js
|
|
273
|
+
|
|
274
|
+
## How It Works
|
|
275
|
+
|
|
276
|
+
### Generation Algorithm
|
|
277
|
+
|
|
278
|
+
The generator uses a **backtracking algorithm** to fill the grid:
|
|
279
|
+
|
|
280
|
+
1. **Generate Complete Solution:**
|
|
281
|
+
- Start with an empty 9×9 grid
|
|
282
|
+
- For each cell (left to right, top to bottom):
|
|
283
|
+
- Try a random number from 1-9
|
|
284
|
+
- Check if it's valid (no duplicates in row, column, or 3×3 block)
|
|
285
|
+
- If valid, move to next cell
|
|
286
|
+
- If no valid number exists, backtrack to previous cell
|
|
287
|
+
- Repeat until the entire grid is filled
|
|
288
|
+
|
|
289
|
+
2. **Create Puzzle (optional):**
|
|
290
|
+
- Remove approximately 50% of cells randomly
|
|
291
|
+
- Preserve columns 0, 3, and 6 for structure
|
|
292
|
+
- Empty cells are marked as 0
|
|
293
|
+
|
|
294
|
+
This ensures every generated grid is a complete, valid Sudoku solution, and every puzzle is solvable.
|
|
295
|
+
|
|
296
|
+
## Performance
|
|
297
|
+
|
|
298
|
+
- Single puzzle generation: **< 100ms**
|
|
299
|
+
- Single solution generation: **< 100ms**
|
|
300
|
+
- Average generation time: **~50ms**
|
|
301
|
+
- Tested up to 10,000+ generations without issues
|
|
302
|
+
|
|
303
|
+
## Browser Compatibility
|
|
304
|
+
|
|
305
|
+
- Chrome/Edge 90+
|
|
306
|
+
- Firefox 88+
|
|
307
|
+
- Safari 14+
|
|
308
|
+
- Node.js 18+
|
|
309
|
+
|
|
310
|
+
## Use Cases
|
|
311
|
+
|
|
312
|
+
### Game Development
|
|
313
|
+
```javascript
|
|
314
|
+
import { generateSudokuGrid, generateCompleteSudokuGrid } from '@luckyfoxdesign/sudoku-generator';
|
|
315
|
+
|
|
316
|
+
const puzzle = generateSudokuGrid(); // For player to solve
|
|
317
|
+
const solution = generateCompleteSudokuGrid(); // For validation
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
### Puzzle Books / Print
|
|
321
|
+
```javascript
|
|
322
|
+
import { generateCompleteSudokuGrid } from '@luckyfoxdesign/sudoku-generator';
|
|
323
|
+
|
|
324
|
+
// Generate 100 unique puzzles
|
|
325
|
+
for (let i = 0; i < 100; i++) {
|
|
326
|
+
const puzzle = generateSudokuGrid();
|
|
327
|
+
printPuzzle(puzzle);
|
|
328
|
+
}
|
|
329
|
+
```
|
|
330
|
+
|
|
331
|
+
### Educational Tools
|
|
332
|
+
```javascript
|
|
333
|
+
import { generateSudokuGridWithMetadata } from '@luckyfoxdesign/sudoku-generator';
|
|
334
|
+
|
|
335
|
+
// Analyze generation process
|
|
336
|
+
const grid = generateSudokuGridWithMetadata();
|
|
337
|
+
grid.forEach(row => {
|
|
338
|
+
row.forEach(cell => {
|
|
339
|
+
console.log(`Value: ${cell.chosenValue}, Tried: ${cell.removedValues}`);
|
|
340
|
+
});
|
|
341
|
+
});
|
|
342
|
+
```
|
|
343
|
+
|
|
344
|
+
## License
|
|
345
|
+
|
|
346
|
+
MIT © [Lucky Fox Design](https://luckyfox.design/)
|
|
347
|
+
|
|
348
|
+
## Contributing
|
|
349
|
+
|
|
350
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
351
|
+
|
|
352
|
+
1. Fork the repository
|
|
353
|
+
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
|
|
354
|
+
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
|
|
355
|
+
4. Push to the branch (`git push origin feature/AmazingFeature`)
|
|
356
|
+
5. Open a Pull Request
|
|
357
|
+
|
|
358
|
+
## Links
|
|
359
|
+
|
|
360
|
+
- [GitHub Repository](https://github.com/luckyfoxdesign/sudoku-generator)
|
|
361
|
+
- [NPM Package](https://www.npmjs.com/package/@luckyfoxdesign/sudoku-generator)
|
|
362
|
+
- [Report Issues](https://github.com/luckyfoxdesign/sudoku-generator/issues)
|
|
363
|
+
|
|
364
|
+
## Author
|
|
365
|
+
|
|
366
|
+
**Lucky Fox Design**
|
|
367
|
+
- Website: https://luckyfox.design/
|
|
368
|
+
- Email: luckyfoxinthebox@gmail.com
|
|
369
|
+
- NPM: https://www.npmjs.com/~luckyfoxdesign
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
var a=Object.defineProperty;var V=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(t,e)=>{for(var n in e)a(t,n,{get:e[n],enumerable:!0})},S=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of m(e))!d.call(t,s)&&s!==n&&a(t,s,{get:()=>e[s],enumerable:!(o=V(e,s))||o.enumerable});return t};var g=t=>S(a({},"__esModule",{value:!0}),t);var z={};p(z,{generateCompleteSudokuGrid:()=>k,generateSudokuGrid:()=>M,generateSudokuGridWithMetadata:()=>h});module.exports=g(z);/**
|
|
2
|
+
* @luckyfoxdesign/sudoku-generator
|
|
3
|
+
* Generates complete, valid 9x9 Sudoku grids using backtracking algorithm
|
|
4
|
+
*
|
|
5
|
+
* @license MIT
|
|
6
|
+
* @author Lucky Fox Design <luckyfoxinthebox@gmail.com>
|
|
7
|
+
* @see https://github.com/luckyfoxdesign/sudoku-generator
|
|
8
|
+
*/function M(){return h().map(e=>e.map(n=>n.chosenValue))}function k(){const t=r();for(let e=0;e<81;e++){const n=Math.floor(e/9),o=e%9;i(n,o,t)===0&&(e-=2,e<-1&&(e=-1))}return t.map(e=>e.map(n=>n.chosenValue))}function h(){const t=r();for(let e=0;e<81;e++){const n=Math.floor(e/9),o=e%9;i(n,o,t)===0&&(e-=2,e<-1&&(e=-1))}return x(t),t}function r(){const t=[];for(let e=0;e<9;e++){t[e]=[];for(let n=0;n<9;n++)t[e][n]={chosenValue:0,removedValues:[],gameSet:new Set([1,2,3,4,5,6,7,8,9])}}return t}function v(){return Math.random()<.5}function x(t){for(let e=0;e<t.length;e++)for(let n=0;n<t[e].length;n++)v()&&n%3!==0&&(t[e][n].chosenValue=0)}function i(t,e,n){for(;;){if(j(t,e,n))return b(t,e,n),0;const o=E(t,e,n);if(!I(o,t,e,n)&&!y(o,t,e,n)&&!B(o,t,e,n))return 1}}function I(t,e,n,o){for(let s=0;s<n;s++)if(o[e][s].chosenValue===t)return!0;return!1}function y(t,e,n,o){for(let s=0;s<e;s++)if(o[s][n].chosenValue===t)return!0;return!1}function B(t,e,n,o){const s=c(e),u=c(n);for(const f of s)for(const l of u)if(!(f>e||f===e&&l>=n)&&o[f][l].chosenValue===t)return!0;return!1}function c(t){return t>=0&&t<=2?[0,1,2]:t>=3&&t<=5?[3,4,5]:[6,7,8]}function E(t,e,n){const o=[...n[t][e].gameSet],s=Math.floor(Math.random()*o.length),u=o[s];return n[t][e].gameSet.delete(u),n[t][e].removedValues.push(u),n[t][e].chosenValue=u,u}function b(t,e,n){n[t][e].removedValues.forEach(o=>{n[t][e].gameSet.add(o)}),n[t][e].removedValues.length=0,n[t][e].chosenValue=0}function j(t,e,n){return n[t][e].gameSet.size===0&&n[t][e].removedValues.length===9}0&&(module.exports={generateCompleteSudokuGrid,generateSudokuGrid,generateSudokuGridWithMetadata});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
var Sudoku=(()=>{var a=Object.defineProperty;var V=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var d=Object.prototype.hasOwnProperty;var p=(t,e)=>{for(var n in e)a(t,n,{get:e[n],enumerable:!0})},S=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of m(e))!d.call(t,s)&&s!==n&&a(t,s,{get:()=>e[s],enumerable:!(o=V(e,s))||o.enumerable});return t};var g=t=>S(a({},"__esModule",{value:!0}),t);var z={};p(z,{generateCompleteSudokuGrid:()=>k,generateSudokuGrid:()=>M,generateSudokuGridWithMetadata:()=>i});/**
|
|
2
|
+
* @luckyfoxdesign/sudoku-generator
|
|
3
|
+
* Generates complete, valid 9x9 Sudoku grids using backtracking algorithm
|
|
4
|
+
*
|
|
5
|
+
* @license MIT
|
|
6
|
+
* @author Lucky Fox Design <luckyfoxinthebox@gmail.com>
|
|
7
|
+
* @see https://github.com/luckyfoxdesign/sudoku-generator
|
|
8
|
+
*/function M(){return i().map(e=>e.map(n=>n.chosenValue))}function k(){let t=c();for(let e=0;e<81;e++){let n=Math.floor(e/9),o=e%9;h(n,o,t)===0&&(e-=2,e<-1&&(e=-1))}return t.map(e=>e.map(n=>n.chosenValue))}function i(){let t=c();for(let e=0;e<81;e++){let n=Math.floor(e/9),o=e%9;h(n,o,t)===0&&(e-=2,e<-1&&(e=-1))}return x(t),t}function c(){let t=[];for(let e=0;e<9;e++){t[e]=[];for(let n=0;n<9;n++)t[e][n]={chosenValue:0,removedValues:[],gameSet:new Set([1,2,3,4,5,6,7,8,9])}}return t}function v(){return Math.random()<.5}function x(t){for(let e=0;e<t.length;e++)for(let n=0;n<t[e].length;n++)v()&&n%3!==0&&(t[e][n].chosenValue=0)}function h(t,e,n){for(;;){if(j(t,e,n))return b(t,e,n),0;let o=E(t,e,n);if(!I(o,t,e,n)&&!y(o,t,e,n)&&!B(o,t,e,n))return 1}}function I(t,e,n,o){for(let s=0;s<n;s++)if(o[e][s].chosenValue===t)return!0;return!1}function y(t,e,n,o){for(let s=0;s<e;s++)if(o[s][n].chosenValue===t)return!0;return!1}function B(t,e,n,o){let s=r(e),u=r(n);for(let f of s)for(let l of u)if(!(f>e||f===e&&l>=n)&&o[f][l].chosenValue===t)return!0;return!1}function r(t){return t>=0&&t<=2?[0,1,2]:t>=3&&t<=5?[3,4,5]:[6,7,8]}function E(t,e,n){let o=[...n[t][e].gameSet],s=Math.floor(Math.random()*o.length),u=o[s];return n[t][e].gameSet.delete(u),n[t][e].removedValues.push(u),n[t][e].chosenValue=u,u}function b(t,e,n){n[t][e].removedValues.forEach(o=>{n[t][e].gameSet.add(o)}),n[t][e].removedValues.length=0,n[t][e].chosenValue=0}function j(t,e,n){return n[t][e].gameSet.size===0&&n[t][e].removedValues.length===9}return g(z);})();
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @luckyfoxdesign/sudoku-generator
|
|
3
|
+
* Generates complete, valid 9x9 Sudoku grids using backtracking algorithm
|
|
4
|
+
*
|
|
5
|
+
* @license MIT
|
|
6
|
+
* @author Lucky Fox Design <luckyfoxinthebox@gmail.com>
|
|
7
|
+
* @see https://github.com/luckyfoxdesign/sudoku-generator
|
|
8
|
+
*/function k(){return c().map(e=>e.map(n=>n.chosenValue))}function v(){const t=l();for(let e=0;e<81;e++){const n=Math.floor(e/9),o=e%9;r(n,o,t)===0&&(e-=2,e<-1&&(e=-1))}return t.map(e=>e.map(n=>n.chosenValue))}function c(){const t=l();for(let e=0;e<81;e++){const n=Math.floor(e/9),o=e%9;r(n,o,t)===0&&(e-=2,e<-1&&(e=-1))}return V(t),t}function l(){const t=[];for(let e=0;e<9;e++){t[e]=[];for(let n=0;n<9;n++)t[e][n]={chosenValue:0,removedValues:[],gameSet:new Set([1,2,3,4,5,6,7,8,9])}}return t}function h(){return Math.random()<.5}function V(t){for(let e=0;e<t.length;e++)for(let n=0;n<t[e].length;n++)h()&&n%3!==0&&(t[e][n].chosenValue=0)}function r(t,e,n){for(;;){if(M(t,e,n))return g(t,e,n),0;const o=S(t,e,n);if(!m(o,t,e,n)&&!d(o,t,e,n)&&!p(o,t,e,n))return 1}}function m(t,e,n,o){for(let s=0;s<n;s++)if(o[e][s].chosenValue===t)return!0;return!1}function d(t,e,n,o){for(let s=0;s<e;s++)if(o[s][n].chosenValue===t)return!0;return!1}function p(t,e,n,o){const s=i(e),u=i(n);for(const f of s)for(const a of u)if(!(f>e||f===e&&a>=n)&&o[f][a].chosenValue===t)return!0;return!1}function i(t){return t>=0&&t<=2?[0,1,2]:t>=3&&t<=5?[3,4,5]:[6,7,8]}function S(t,e,n){const o=[...n[t][e].gameSet],s=Math.floor(Math.random()*o.length),u=o[s];return n[t][e].gameSet.delete(u),n[t][e].removedValues.push(u),n[t][e].chosenValue=u,u}function g(t,e,n){n[t][e].removedValues.forEach(o=>{n[t][e].gameSet.add(o)}),n[t][e].removedValues.length=0,n[t][e].chosenValue=0}function M(t,e,n){return n[t][e].gameSet.size===0&&n[t][e].removedValues.length===9}export{v as generateCompleteSudokuGrid,k as generateSudokuGrid,c as generateSudokuGridWithMetadata};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@luckyfoxdesign/sudoku-generator",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A lightweight Sudoku puzzle generator that creates randomized 9×9 grids using a backtracking algorithm. Generates complete, valid Sudoku boards suitable for games and puzzle applications.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"sudoku",
|
|
7
|
+
"puzzle",
|
|
8
|
+
"generator",
|
|
9
|
+
"game",
|
|
10
|
+
"algorithm",
|
|
11
|
+
"backtracking",
|
|
12
|
+
"grid",
|
|
13
|
+
"board"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/luckyfoxdesign/sudoku-generator#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/luckyfoxdesign/sudoku-generator/issues"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+ssh://git@github.com/luckyfoxdesign/sudoku-generator.git"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"author": "Lucky Fox Design <luckyfoxinthebox@gmail.com> (https://luckyfox.design/)",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"main": "./dist/index.cjs",
|
|
27
|
+
"module": "./dist/index.js",
|
|
28
|
+
"browser": "./dist/index.global.js",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"require": "./dist/index.cjs",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "node build.mjs",
|
|
41
|
+
"prepublishOnly": "npm run build",
|
|
42
|
+
"test": "node --test test/**/*.test.js"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"esbuild": "^0.27.2"
|
|
46
|
+
},
|
|
47
|
+
"funding": {
|
|
48
|
+
"type": "individual",
|
|
49
|
+
"url": "https://luckyfox.design/"
|
|
50
|
+
}
|
|
51
|
+
}
|