-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.js
343 lines (272 loc) · 9.93 KB
/
generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
GENERATOR=(function(){
// Adapted from: https://www.emanueleferonato.com/2015/06/23/pure-javascript-sudoku-generatorsolver/
// given a sudoku cell, returns the row
function returnRow(cell) {
return Math.floor(cell / 9);
}
// given a sudoku cell, returns the column
function returnCol(cell) {
return cell % 9;
}
// given a sudoku cell, returns the 3x3 block
function returnBlock(cell) {
return Math.floor(returnRow(cell) / 3) * 3 + Math.floor(returnCol(cell) / 3);
}
// given a number, a row and a sudoku, returns true if the number can be placed in the row
function isPossibleRow(number,row,sudoku) {
for (let i=0; i<=8; i++)
if (sudoku[row*9+i] == number)
return false;
return true;
}
// given a number, a column and a sudoku, returns true if the number can be placed in the column
function isPossibleCol(number,col,sudoku) {
for (let i=0; i<=8; i++)
if (sudoku[col+9*i] == number)
return false;
return true;
}
// given a number, a 3x3 block and a sudoku, returns true if the number can be placed in the block
function isPossibleBlock(number,block,sudoku) {
for (let i=0; i<=8; i++)
if (sudoku[Math.floor(block/3)*27+i%3+9*Math.floor(i/3)+3*(block%3)] == number)
return false;
return true;
}
// given a cell, a number and a sudoku, returns true if the number can be placed in the cell
function isPossibleNumber(cell,number,sudoku) {
let
row = returnRow(cell),
col = returnCol(cell),
block = returnBlock(cell);
return isPossibleRow(number,row,sudoku) && isPossibleCol(number,col,sudoku) && isPossibleBlock(number,block,sudoku);
}
// given a row and a sudoku, returns true if it's a legal row
function isCorrectRow(row,sudoku) {
let
rightSequence = new Array(1,2,3,4,5,6,7,8,9),
rowTemp= new Array();
for (let i=0; i<=8; i++)
rowTemp[i] = sudoku[row*9+i];
rowTemp.sort();
return rowTemp.join() == rightSequence.join();
}
// given a column and a sudoku, returns true if it's a legal column
function isCorrectCol(col,sudoku) {
let
rightSequence = new Array(1,2,3,4,5,6,7,8,9),
colTemp= new Array();
for (let i=0; i<=8; i++)
colTemp[i] = sudoku[col+i*9];
colTemp.sort();
return colTemp.join() == rightSequence.join();
}
// given a 3x3 block and a sudoku, returns true if it's a legal block
function isCorrectBlock(block,sudoku) {
let
rightSequence = new Array(1,2,3,4,5,6,7,8,9),
blockTemp= new Array();
for (let i=0; i<=8; i++)
blockTemp[i] = sudoku[Math.floor(block/3)*27+i%3+9*Math.floor(i/3)+3*(block%3)];
blockTemp.sort();
return blockTemp.join() == rightSequence.join();
}
// given a sudoku, returns true if the sudoku is solved
function isSolvedSudoku(sudoku) {
for (let i=0; i<=8; i++)
if (!isCorrectBlock(i,sudoku) || !isCorrectRow(i,sudoku) || !isCorrectCol(i,sudoku))
return false;
return true;
}
// given a cell and a sudoku, returns an array with all possible values we can write in the cell
function determinePossibleValues(cell,sudoku) {
let
possible = new Array();
for (let i=1; i<=9; i++)
if (isPossibleNumber(cell,i,sudoku))
possible.unshift(i);
return possible;
}
// given an array of possible values assignable to a cell, returns a random value picked from the array
function determineRandomPossibleValue(possible,cell) {
let
randomPicked = Math.floor(Math.random() * possible[cell].length);
return possible[cell][randomPicked];
}
// given a sudoku, returns a two dimension array with all possible values
function scanSudokuForUnique(sudoku) {
let
possible = new Array();
for (let i=0; i<=80; i++)
if (sudoku[i] == 0) {
possible[i] = new Array();
possible[i] = determinePossibleValues(i,sudoku);
if (possible[i].length==0)
return false;
}
return possible;
}
// given an array and a number, removes the number from the array
function removeAttempt(attemptArray,number) {
let
newArray = new Array();
for (let i=0; i<attemptArray.length; i++)
if (attemptArray[i] != number)
newArray.unshift(attemptArray[i]);
return newArray;
}
// given a two dimension array of possible values, returns the index of a cell where there are the less possible numbers to choose from
function nextRandom(possible) {
let
max = 9,
minChoices = 0;
for (let i=0; i<=80; i++)
if (possible[i]!=undefined)
if ((possible[i].length<=max) && (possible[i].length>0)) {
max = possible[i].length;
minChoices = i;
}
return minChoices;
}
// given a sudoku, solves it
function solve(sudoku) {
let
saved = new Array(),
savedSudoku = new Array(),
i=0,
nextMove,
whatToTry,
attempt;
while (!isSolvedSudoku(sudoku)) {
i++;
nextMove = scanSudokuForUnique(sudoku);
if (nextMove == false) {
nextMove = saved.pop();
sudoku = savedSudoku.pop();
}
whatToTry = nextRandom(nextMove);
attempt = determineRandomPossibleValue(nextMove,whatToTry);
if (nextMove[whatToTry].length>1) {
nextMove[whatToTry] = removeAttempt(nextMove[whatToTry],attempt);
saved.push(nextMove.slice());
savedSudoku.push(sudoku.slice());
}
sudoku[whatToTry] = attempt;
}
}
// check if a sudoku is complete
function isComplete(sudoku) {
for (let i=0;i<81;i++)
if (!sudoku[0])
return false;
return true;
}
// generate and render a Sudoku
function printSudoku(context,fields,scale,ox,oy,blanks) {
const
CELLSIZE = 80;
let
valid = false,
validSudoku = [];
// --- Generate a valid sudoku
do {
for (let i=0;i<81;i++)
validSudoku[i]=0;
solve(validSudoku);
valid = isComplete(validSudoku);
} while (!valid);
// --- Creates a solvable sudoku
let
blankPos,
blanksLeft,
sudoku,
testSudoku;
do {
valid = false;
blanksLeft = blanks;
sudoku = validSudoku.slice();
do {
blankPos = Math.floor(Math.random()*81);
if (sudoku[blankPos]) {
sudoku[blankPos] = 0;
blanksLeft--;
}
} while (blanksLeft);
testSudoku = sudoku.slice();
solve(testSudoku);
valid = isComplete(testSudoku);
} while (!valid);
// --- Draw it
context.lineWidth = 2;
context.strokeStyle = "#000";
context.textBaseline = "middle";
context.textAlign = "center";
context.font = "bold 48px Seshat";
for (let y=0;y<9;y++)
for (let x=0;x<9;x++) {
let
px = ox+(x*CELLSIZE),
py = oy+(y*CELLSIZE),
pos = y*9+x;
context.beginPath();
context.rect(px, py, CELLSIZE, CELLSIZE);
context.stroke();
if (sudoku[pos])
context.fillText(sudoku[pos], px+(CELLSIZE/2), py+(CELLSIZE/2));
else
fields.push({
type:"text",
x:px*scale,
y:py*scale,
width:CELLSIZE*scale,
height:CELLSIZE*scale,
multiline:false,
align:"center"
});
}
context.lineWidth = 6;
for (let y=0;y<3;y++)
for (let x=0;x<3;x++) {
context.beginPath();
context.rect(ox+(x*CELLSIZE*3), oy+(y*CELLSIZE*3), CELLSIZE*3, CELLSIZE*3);
context.stroke();
}
}
return {
run:(self,json,sub,cb)=>{
let
fields = [],
sheetWidth = 420,
sheetHeight = 297,
canvasWidth = 2525,
canvasHeight = 1785,
canvas = document.createElement("canvas"),
context = canvas.getContext("2d");
canvas.width = canvasWidth;
canvas.height = canvasHeight;
for (let i=0;i<6;i++)
printSudoku(context,fields,sheetWidth/canvasWidth,95+(800*(i%3)),110+(840*Math.floor(i/3)),20+((i*4)+(sub.settings.difficulty*8)));
json.data.push({
type:"sheet",
data:{
x:0,
y:0,
width:sheetWidth,
height:sheetHeight,
frame:true,
model:{
EN:{
isResource:true,
type:"canvas",
canvas:canvas
}
},
fields:{
EN:fields
}
}
})
cb(json);
}
}
})();