-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
562 lines (465 loc) · 17.9 KB
/
script.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
let grid = [];
let originalGrid;
for (let i = 0 ; i < 81 ; i++) {
grid.push(0);
}
originalGrid = grid.map(x => x);
let allNumbers = [1,2,3,4,5,6,7,8,9];
let sectorMainIndexes = [0,3,6, 27,30,33, 54,57,60];
let sectorIterationIndexes = [0,1,2, 9,10,11, 18,19,20];
function consoleMatrix (matrix) {
for ( let j = 0 ; j < 9 ; j++ ){
let row = matrix.slice(j*9 , (j+1)*9 );
console.log(row);
}
}
function lookForNumbers() {
let foundNumbers = [];
for (let num = 1 ; num <= 9 ; num++) {
let gridCopy = grid.map(x => x);
for (let j = 0 ; j < 9 ; j ++) {
for (let i = 0 ; i < 9 ; i++) {
let index = i + j*9;
if (grid[index] == num) {
// for that row
for (let k = j*9 ; k < (j+1)*9 ; k ++ ) {
gridCopy[k] = 10;
}
// for that column
for (let k = i ; k < 81 ; k += 9 ) {
gridCopy[k] = 10;
}
// for that sector
let sectorIndex = Math.trunc(i/3)*3 + Math.trunc(j/3)*3 * 9
for (let k of sectorIterationIndexes) {
gridCopy[sectorIndex+k] = 10;
}
} else if (grid[index] != 0) {
gridCopy[index] = 10;
}
}
}
for (let j = 0 ; j < 9 ; j ++) {
let accRow = 0;
let accColumn = 0;
let accSector = 0;
for (let i = 0 ; i < 9 ; i++) {
let indexForColumn = j + i*9;
let indexForRow = i + j*9;
let indexForSector = sectorMainIndexes[j] + sectorIterationIndexes[i];
accRow += gridCopy[indexForRow];
accColumn += gridCopy[indexForColumn];
accSector += gridCopy[indexForSector];
}
if (accRow == 80) {
for (let i = 0 ; i < 9 ; i++) {
let index = i + j*9;
if (gridCopy[index] == 0) foundNumbers.push( { index , num } );
}
}
if (accColumn == 80) {
for (let i = 0 ; i < 9 ; i++) {
let index = j + i*9;
if (gridCopy[index] == 0) foundNumbers.push( { index , num } );
}
}
if (accSector == 80) {
for (let i = 0 ; i < 9 ; i++) {
let index = sectorMainIndexes[j] + sectorIterationIndexes[i];
if (gridCopy[index] == 0) foundNumbers.push( { index , num } );
}
}
}
}
if (foundNumbers.length == 0) return;
foundNumbers.forEach(found => grid[found.index] = found.num );
lookForNumbers();
}
function lookForMissingNumbers() {
let foundNumbers = [];
for (let j = 0; j < 9 ; j++) {
let rowNumbers = [];
let colNumbers = [];
let secNumbers = [];
for (let i = 0; i < 9 ; i++) {
let indexForRow = i + j*9;
let indexForColumn = j + i*9;
let indexForSector = sectorMainIndexes[j] + sectorIterationIndexes[i];
rowNumbers.push(grid[indexForRow]);
colNumbers.push(grid[indexForColumn]);
secNumbers.push(grid[indexForSector]);
}
// for rows...
let rowMissingNumbers = [];
let rowMissingIndexes = [];
// for columns...
let colMissingNumbers = [];
let colMissingIndexes = [];
// for sector...
let secMissingNumbers = [];
let secMissingIndexes = [];
for (let i = 0; i < 9 ; i++) {
if ( rowNumbers[i] == 0 ) rowMissingIndexes.push(i);
if ( !rowNumbers.includes(i+1) ) rowMissingNumbers.push(i+1);
if ( colNumbers[i] == 0 ) colMissingIndexes.push(i);
if ( !colNumbers.includes(i+1) ) colMissingNumbers.push(i+1);
if ( secNumbers[i] == 0 ) secMissingIndexes.push(i);
if ( !secNumbers.includes(i+1) ) secMissingNumbers.push(i+1);
}
// for row...
for (let col of rowMissingIndexes) {
let numbers = rowMissingNumbers.map(x => x);
let currentSector = Math.trunc(col/3)*3 + Math.trunc(j/3)*3 * 9;
for (let k = numbers.length - 1 ; k >= 0 ; k --) {
for (let i = 0; i < 9 ; i++) {
let indexForColumn = col + i*9;
let indexForSector = currentSector + sectorIterationIndexes[i];
if (grid[indexForColumn] == numbers[k] || grid[indexForSector] == numbers[k]) {
numbers.splice(k,1);
break;
}
}
}
if (numbers.length == 1) foundNumbers.push( { index: col+j*9 , num: numbers[0] } );
}
// for columns...
for (let row of colMissingIndexes) {
let numbers = colMissingNumbers.map(x => x);
let currentSector = Math.trunc(j/3)*3 + Math.trunc(row/3)*3 * 9;
for (let k = numbers.length - 1 ; k >= 0 ; k --) {
for (let i = 0; i < 9 ; i++) {
let indexForRow = i + row*9;
let indexForSector = currentSector + sectorIterationIndexes[i];
if (grid[indexForRow] == numbers[k] || grid[indexForSector] == numbers[k]) {
numbers.splice(k,1);
break;
}
}
}
if (numbers.length == 1) foundNumbers.push( { index: j+row*9 , num: numbers[0] } );
}
// for sector...
for (let indexInSector of secMissingIndexes) {
let numbers = secMissingNumbers.map(x => x);
let globalIndex = sectorMainIndexes[j] + sectorIterationIndexes[indexInSector];
let currentRow = Math.trunc(globalIndex/9);
let currentCol = globalIndex%9;
for (let k = numbers.length - 1 ; k >= 0 ; k --) {
for (let i = 0; i < 9 ; i++) {
let indexForRow = i + currentRow*9;
let indexForColumn = currentCol + i*9;
if (grid[indexForRow] == numbers[k] || grid[indexForColumn] == numbers[k]) {
numbers.splice(k,1);
break;
}
}
}
if (numbers.length == 1) foundNumbers.push( { index: globalIndex , num: numbers[0] } );
}
}
if (foundNumbers.length == 0) return;
foundNumbers.forEach(found => grid[found.index] = found.num);
lookForMissingNumbers();
}
function isValidSolution() {
for (let j = 0 ; j < 9 ; j++) {
let accColumn = 0;
let accRow = 0;
for (let i = 0 ; i < 9 ; i++) {
let indexForColumn = j + i*9;
let indexForRow = i + j*9;
accColumn += grid[indexForColumn];
accRow += grid[indexForRow];
}
if (accColumn!=45 || accRow!=45) {
return false;
}
}
return true;
}
function wellPlaced() {
let wellPlaced = true;
for (let j = 0; j < 9 ; j++) {
let rowNumbers = [];
let colNumbers = [];
let secNumbers = [];
for (let i = 0; i < 9 ; i++) {
let indexForRow = i + j*9;
let indexForColumn = j + i*9;
let indexForSector = sectorMainIndexes[j] + sectorIterationIndexes[i];
rowNumbers.push(grid[indexForRow]);
colNumbers.push(grid[indexForColumn]);
secNumbers.push(grid[indexForSector]);
}
for (let i = 1; i <= 9 ; i++) {
if (rowNumbers.filter(num => num===i).length > 1 ||
colNumbers.filter(num => num===i).length > 1 ||
secNumbers.filter(num => num===i).length > 1 ) {
wellPlaced = false;
return wellPlaced;
}
}
}
return wellPlaced;
}
function guessNumbers() {
for (let j = 0; j < 9 ; j++) {
let rowNumbers = [];
let colNumbers = [];
let secNumbers = [];
for (let i = 0; i < 9 ; i++) {
let indexForRow = i + j*9;
let indexForColumn = j + i*9;
let indexForSector = sectorMainIndexes[j] + sectorIterationIndexes[i];
rowNumbers.push(grid[indexForRow]);
colNumbers.push(grid[indexForColumn]);
secNumbers.push(grid[indexForSector]);
}
// for rows...
let rowMissingNumbers = [];
let rowMissingIndexes = [];
// for columns...
let colMissingNumbers = [];
let colMissingIndexes = [];
// for sector...
let secMissingNumbers = [];
let secMissingIndexes = [];
for (let i = 0; i < 9 ; i++) {
if ( rowNumbers[i] == 0 ) rowMissingIndexes.push(i);
if ( !rowNumbers.includes(i+1) ) rowMissingNumbers.push(i+1);
if ( colNumbers[i] == 0 ) colMissingIndexes.push(i);
if ( !colNumbers.includes(i+1) ) colMissingNumbers.push(i+1);
if ( secNumbers[i] == 0 ) secMissingIndexes.push(i);
if ( !secNumbers.includes(i+1) ) secMissingNumbers.push(i+1);
}
// for row...
for (let col of rowMissingIndexes) {
for (let number of rowMissingNumbers) {
let gridBackup = grid.map(x => x);
let index = col + j*9;
grid[index] = number;
noGuessingMethods();
if (isValidSolution()) {
return;
} else {
grid = gridBackup;
}
}
}
// for column...
for (let row of colMissingIndexes) {
for (let number of colMissingNumbers) {
let gridBackup = grid.map(x => x);
let index = j + row*9;
grid[index] = number;
noGuessingMethods();
if (isValidSolution()) {
return;
} else {
grid = gridBackup;
}
}
}
// for sector...
for (let indexInSector of secMissingIndexes) {
for (let number of secMissingNumbers) {
let gridBackup = grid.map(x => x);
let index = sectorMainIndexes[j] + sectorIterationIndexes[indexInSector];
grid[index] = number;
noGuessingMethods();
if (isValidSolution()) {
return;
} else {
grid = gridBackup;
}
}
}
}
}
function noGuessingMethods() {
lookForNumbers();
lookForMissingNumbers();
}
function solve() {
noGuessingMethods();
if (!isValidSolution()) {
guessNumbers();
}
}
//
//
//
function renderGrid() {
const gridObject = document.querySelector(".grid");
let content = "";
let backGroundColor;
let fontColor;
let numberToShow;
for (let row = 0 ; row < 9 ; row++) {
for (let col = 0 ; col < 9 ; col++) {
let index = col + row*9;
let sectorIndex = Math.trunc(col/3)*3 + Math.trunc(row/3)*3 * 9;
if ( [0,2,4,6,8].map(x => sectorMainIndexes[x]).includes(sectorIndex) ) backGroundColor = "white";
else backGroundColor = "#D4E6F1";
if (grid[index] != 0 ) numberToShow = grid[index];
else numberToShow = "";
if (originalGrid[index] == 0) fontColor = "rgb(157,157,157)";
else fontColor = "#151515";
content += `
<div class="cell" style="background-color: ${backGroundColor}; color: ${fontColor};">
${numberToShow}
</div>
`;
}
}
gridObject.innerHTML = content;
}
function showMessage(message) {
const messageBox = document.querySelector(".message-box");
messageBox.textContent = message;
}
let generateBtn = document.querySelector(".button.generate");
generateBtn.addEventListener("click",() => {
cont = 0;
do {
grid = originalGrid.map(x => x);
cont++;
generateSudoku();
solve();
} while (!isValidSolution() && cont < 100);
if (isValidSolution()) {
showMessage(`Your sudoku is ready`);
renderGrid();
console.log(cont);
} else {
showMessage(`We couldn't generate a sudoku :(`);
}
});
renderGrid();
showMessage("Ready to start :]")
//
//
//
function addToGrid(arr) {
let cont = 0;
for (let i = 0; i < 81 ; i++) {
if (grid[i] == 0) {
grid[i] = arr[cont];
cont ++;
}
if ( cont == arr.length) return;
}
}
function addToGridVertical (arr,colIndex) {
let cont = 0;
for (let i = 0; i < 9 ; i++) {
let globalIndex = colIndex + i*9
if (grid[globalIndex] == 0) {
grid[globalIndex] = arr[cont];
cont ++;
}
if ( cont == arr.length) return;
}
}
function getNumbersInColumn(col) {
let numbersInCol = [];
for (let i = 0 ; i < 9 ; i ++) {
let index = col + i*9;
if (grid[index] != 0) numbersInCol.push(grid[index]);
}
return numbersInCol;
}
function getNumbersInRow(row) {
let numbersInRow = [];
for (let i = 0 ; i < 9 ; i ++) {
let index = i + row*9;
if (grid[index] != 0) numbersInRow.push(grid[index]);
}
return numbersInRow;
}
function getMissingNumbers(arr) {
let missingNumbers = [];
allNumbers.forEach(num => {
if (!arr.includes(num)) missingNumbers.push(num);
});
return missingNumbers;
}
function generateSudoku() {
// // // // //
/* Stage 1 */
// // // // //
let gridBackup;
// firstRow
let firstRow = allNumbers.map(x => x).sort(() => Math.random() - 0.5);
addToGrid(firstRow);
// secondRow
let secondRowP1, secondRowP2, secondRowP3;
gridBackup = grid.map(x => x);
let cont = 0;
do {
grid = gridBackup.map(x => x);
secondRowP1 = firstRow.slice(3,9).sort(() => Math.random() - 0.5).slice(0,3);
secondRowP2 = getMissingNumbers( secondRowP1.concat(firstRow.slice(3,6)) ).sort(() => Math.random() - 0.5).slice(0,3);
secondRowP3 = getMissingNumbers( secondRowP1.concat(secondRowP2) ).sort(() => Math.random() - 0.5);
addToGrid(secondRowP1);
addToGrid(secondRowP2);
addToGrid(secondRowP3);
cont++;
} while (!wellPlaced());
// third row
let thirdRowP1 = getMissingNumbers( secondRowP1.concat(firstRow.slice(0,3)) ).sort(() => Math.random() - 0.5);
let thirdRowP2 = getMissingNumbers( secondRowP2.concat(firstRow.slice(3,6)) ).sort(() => Math.random() - 0.5);
let thirdRowP3 = getMissingNumbers( secondRowP3.concat(firstRow.slice(6,9)) ).sort(() => Math.random() - 0.5);
addToGrid(thirdRowP1);
addToGrid(thirdRowP2);
addToGrid(thirdRowP3);
// firstColumn
let firstColumnP1 = getNumbersInColumn(0);
let firstColumnP2P3 = getMissingNumbers( firstColumnP1 ).sort(() => Math.random() - 0.5);
addToGridVertical(firstColumnP2P3,0);
// secondColumn
let secondColumnP1 = getNumbersInColumn(1);
let secondColumnP2, secondColumnP3;
gridBackup = grid.map(x => x);
cont = 0;
do {
grid = gridBackup.map(x => x);
secondColumnP2 = getMissingNumbers( secondColumnP1.concat(firstColumnP2P3.slice(0,3)) ).sort(() => Math.random() - 0.5).slice(0,3);
secondColumnP3 = getMissingNumbers( secondColumnP1.concat(secondColumnP2) ).sort(() => Math.random() - 0.5);
addToGridVertical(secondColumnP2,1);
addToGridVertical(secondColumnP3,1);
cont++;
} while (!wellPlaced());
// thirdColumn
let thirdColumnP2, thirdColumnP3;
gridBackup = grid.map(x => x);
cont = 0;
do {
grid = gridBackup.map(x => x);
thirdColumnP2 = getMissingNumbers( secondColumnP2.concat(firstColumnP2P3.slice(0,3)) ).sort(() => Math.random() - 0.5);
thirdColumnP3 = getMissingNumbers( secondColumnP3.concat(firstColumnP2P3.slice(3,6)) ).sort(() => Math.random() - 0.5);
addToGridVertical(thirdColumnP2,2);
addToGridVertical(thirdColumnP3,2);
cont++;
} while (!wellPlaced());
// // // // //
/* Stage 2 */
// // // // //
// fourth row
let row4col4 = getMissingNumbers( getNumbersInRow(3).concat( getNumbersInColumn(3) ) );
row4col4 = row4col4.sort(() => Math.random() - 0.5).slice(0,1);
addToGridVertical(row4col4,3);
let row4col5 = getMissingNumbers( getNumbersInRow(3).concat( getNumbersInColumn(4) ) );
row4col5 = row4col5.sort(() => Math.random() - 0.5).slice(0,1);
addToGridVertical(row4col5,4);
let row4col6 = getMissingNumbers( getNumbersInRow(3).concat( getNumbersInColumn(5) ) );
row4col6 = row4col6.sort(() => Math.random() - 0.5).slice(0,1);
addToGridVertical(row4col6,5);
// seventh row
let row7col4 = getMissingNumbers( getNumbersInRow(6).concat( getNumbersInColumn(3) ) );
row7col4 = row7col4.sort(() => Math.random() - 0.5).slice(0,1);
grid[3+6*9] = row7col4[0];
let row7col5 = getMissingNumbers( getNumbersInRow(6).concat( getNumbersInColumn(4) ) );
row7col5 = row7col5.sort(() => Math.random() - 0.5).slice(0,1);
grid[4+6*9] = row7col5[0];
}