-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsudoku.js
1320 lines (1114 loc) · 36 KB
/
sudoku.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright https://www.RedDragonWebDesign.com/
// Permission required to use or copy code. All rights reserved.
`use strict`;
class SudokuBoard {
constructor() {
this.blankBoard = SudokuBoard.getBlankBoard();
this.board = SudokuBoard.getBlankBoard();
this.originalBoard = SudokuBoard.getBlankBoard();
}
static getBlankBoard() {
return [
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0]
];
}
// This is only meant for use by solveRecursively. Faster than parseString
// Everything else should use parseString. parseString performs more data validation.
setBoard(board) {
this.board = board;
}
static cloneBoard(board) {
let array = SudokuBoard.getBlankBoard();
for ( let i = 0; i < 9; i++ ) {
for ( let j = 0; j < 9; j++ ) {
array[i][j] = board[i][j];
}
}
return array;
// return Helper.deepCopyArray(board);
}
// returns if board changed or not
parseString(boardString, setOriginalBoard = true) {
const oldBoard = SudokuBoard.cloneBoard(this.board);
if ( ! boardString ) {
return false;
}
if ( ! boardString.match(/^[0-9*_.]{81}$/m) ) {
return false;
}
// TODO: foreach getBoardSquares
for ( let row = 0; row < 9; row++ ) {
for ( let column = 0; column < 9; column++ ) {
let char = boardString.charAt(row*9+column);
if ( char === `*` || char === `_` || char === `.` )
{
char = 0;
}
this.board[row][column] = parseInt(char);
}
}
if ( ! this.puzzleIsValid() ) {
this.board = SudokuBoard.cloneBoard(oldBoard);
return false;
}
if ( setOriginalBoard ) {
this.setOriginalBoard(this.board);
}
return true;
}
getBoard() {
return this.board;
}
getString() {
let str = ``;
for ( let row = 0; row < 9; row++ ) {
for ( let col = 0; col < 9; col++ ) {
str += this.board[row][col];
}
}
return str;
}
// making this its own method to help with debugging
setOriginalBoard(obj) {
this.originalBoard = SudokuBoard.cloneBoard(obj);
}
restartPuzzle() {
this.board = SudokuBoard.cloneBoard(this.originalBoard);
}
makeMove(row, col, value) {
if ( value === `` ) {
value = 0;
}
this.board[row][col] = parseInt(value);
}
numberInBox(candidate, square) {
const row = square.getRow();
const col = square.getCol();
// find top left corner of box
const rowOffset = Math.floor(row/3)*3;
const colOffset = Math.floor(col/3)*3;
// iterate around 3x3 area
for ( let i = 0 + rowOffset; i <= 2 + rowOffset; i++ ) {
for ( let j = 0 + colOffset; j <= 2 + colOffset; j++ ) {
const value = this.board[i][j];
if ( value === candidate ) {
return true;
}
}
}
return false;
}
getSquaresInRow(row) {
let squares = [];
for ( let i = 0; i < 9; i++ ) {
const value = this.board[row][i];
squares.push(new SudokuSquare(row, i, value));
}
return squares;
}
getSquaresInColumn(col) {
let squares = [];
for ( let i = 0; i < 9; i++ ) {
const value = this.board[i][col];
squares.push(new SudokuSquare(i, col, value));
}
return squares;
}
getAllSquares() {
let squares = [];
for ( let i = 0; i < 9; i++ ) {
for ( let j = 0; j < 9; j++ ) {
const value = this.board[i][j];
squares.push(new SudokuSquare(i, j, value));
}
}
return squares;
}
getSquare(row, col) {
return new SudokuSquare(row, col, this.board[row][col]);
}
getAllBoxes() {
let squares = [];
for ( let i = 0; i <= 2; i++ ) {
for ( let j = 0; j <= 2; j++ ) {
const value = this.board[i*3][j*3];
squares.push(new SudokuSquare(i*3, j*3, value));
}
}
return squares;
}
// TODO: consider splitting the below code into a SudokuSolver class
// I haven't done it yet because I'd have to pass a board variable around. That's a lot of code re-writing. Not sure it's worth it.
puzzleIsValid() {
try {
this.fullHouse(false, false);
} catch {
return false;
}
return true;
}
isLegalMove(row, col, value, checkForNonNumbers = true) {
value = parseInt(value);
// check for non numbers
// Regex is very expensive. Only check this for user input.
if ( checkForNonNumbers ) {
if ( ! value.toString().match(/^[1-9]$/m) ) {
return false;
}
}
// check row
// TODO: foreach getRowSquares
for ( let i = 0; i < 9; i++ ) {
if ( value === this.board[row][i] ) {
return false;
}
}
// check column
// TODO: foreach getColumnSquares
for ( let i = 0; i < 9; i++ ) {
if ( value === this.board[i][col] ) {
return false;
}
}
// check 3x3 grid
// TODO: foreach getBoxSquares
const rowOffset = Math.floor(row/3)*3;
const colOffset = Math.floor(col/3)*3;
for ( let i = 0 + rowOffset; i <= 2 + rowOffset; i++ ) {
for ( let j = 0 + colOffset; j <= 2 + colOffset; j++ ) {
if ( value === this.board[i][j] ) {
return false;
}
}
}
return true;
}
solveLogically() {
this.solveUsingAllTechniques();
if ( this.puzzleIsSolved() ) {
return true;
} else {
return false;
}
}
getHint() {
return this.solveUsingAllTechniques(true);
}
solveUsingAllTechniques(hintMode = false) {
let hintSquare;
let lastHintSquare = false;
// Need to keep looping, trying the techniques from easiest to hardest.
// If a technique solves a square, restart the loop.
// If no techniques can solve the current square, end the loop.
while ( true ) {
// TODO: have every square store its candidates, like sudokuwiki.org does
hintSquare = this.fullHouse(hintMode);
if ( hintSquare && hintMode ) {
return hintSquare;
} else if ( hintSquare != lastHintSquare ) {
lastHintSquare = hintSquare;
continue;
}
hintSquare = this.boxXray(hintMode);
if ( hintSquare && hintMode ) {
return hintSquare;
} else if ( hintSquare != lastHintSquare ) {
lastHintSquare = hintSquare;
continue;
}
// rowXray
// columnXray
// add other more complicated techniques here
break;
}
return hintSquare;
}
// Possibilities {1:true, 2:true, 3:true, 4:true, 5:true, 6:true, 7:true, 8:true, 9:true}
static squareIsSolved(possibilities) {
let trueCount = 0;
for ( let i = 1; i <= 9; i++ ) {
if ( possibilities[i] ) {
trueCount++;
}
if ( trueCount >= 2 ) {
return false;
}
}
if ( trueCount === 1 ) {
return true;
}
return false;
}
// If 8 of 9 squares are filled in, fill in 9th square.
fullHouse(hintMode = false, modifyBoard = true) {
let possibilities;
let emptyCol;
let emptyRow;
// check row
for ( let row = 0; row < 9; row++ ) {
// bool array [true, true, true] is faster than list [1, 2, 3]
possibilities = {1:true, 2:true, 3:true, 4:true, 5:true, 6:true, 7:true, 8:true, 9:true};
emptyCol = 0;
for ( let col = 0; col < 9; col++ ) {
const value = this.board[row][col];
if ( value === 0 ) {
emptyCol = col;
continue;
} else if ( possibilities[value] ) {
possibilities[value] = false;
} else {
this.throwDuplicateNumberError();
}
}
if ( SudokuBoard.squareIsSolved(possibilities) ) {
if ( hintMode ) {
return new SudokuSquare(row, emptyCol);
} else if ( modifyBoard ) {
this.board[row][emptyCol] = SudokuBoard.getTrueKey(possibilities);
}
}
}
// check column
for ( let col = 0; col < 9; col++ ) {
possibilities = {1:true, 2:true, 3:true, 4:true, 5:true, 6:true, 7:true, 8:true, 9:true};
emptyRow = 0;
for ( let row = 0; row < 9; row++ ) {
const value = this.board[row][col];
if ( value === 0 ) {
emptyRow = row;
continue;
} else if ( possibilities[value] ) {
possibilities[value] = false;
} else {
this.throwDuplicateNumberError();
}
}
if ( SudokuBoard.squareIsSolved(possibilities) ) {
if ( hintMode ) {
return new SudokuSquare(emptyRow, col);
} else if ( modifyBoard ) {
this.board[emptyRow][col] = SudokuBoard.getTrueKey(possibilities);
}
}
}
// check 3x3 grid
for ( let row = 0; row < 9; row+=3 ) {
for ( let col = 0; col < 9; col+=3 ) {
possibilities = {1:true, 2:true, 3:true, 4:true, 5:true, 6:true, 7:true, 8:true, 9:true};
emptyRow = 0;
emptyCol = 0;
const rowOffset = Math.floor(row/3)*3;
const colOffset = Math.floor(col/3)*3;
// iterate around 3x3 area
for ( let i = 0 + rowOffset; i <= 2 + rowOffset; i++ ) {
for ( let j = 0 + colOffset; j <= 2 + colOffset; j++ ) {
const value = this.board[i][j];
if ( value === 0 ) {
emptyRow = i;
emptyCol = j;
continue;
} else if ( possibilities[value] ) {
possibilities[value] = false;
} else {
this.throwDuplicateNumberError();
}
}
}
if ( SudokuBoard.squareIsSolved(possibilities) ) {
if ( hintMode ) {
return new SudokuSquare(emptyRow, emptyCol);
} else if ( modifyBoard ) {
this.board[emptyRow][emptyCol] = SudokuBoard.getTrueKey(possibilities);
}
}
}
}
}
// finds some hidden singles, using the cross hatching technique
// Try numbers 1-9. See if there are 2 row boxXrays, 2 column boxXrays, and that number is not present within 3x3.
boxXray(hintMode = false) {
// foreach 3x3 box on the board (method will grab the top left square of the box)
const boxes = this.getAllBoxes();
for ( let square of boxes ) {
// foreach candidate 1-9
for ( let candidate = 1; candidate <= 9; candidate++ ) {
// is number already in 3x3 box? continue;
if ( this.numberInBox(candidate, square) ) {
continue;
}
// boolean 3x3 array to track which cells in the box can possibly contain our candidate. default true.
let tests = [
[true, true, true],
[true, true, true],
[true, true, true],
];
// any spot in bool 3x3 that already has a number, set to false
tests = this.markOccupiedSquares(tests, square);
// foreach 3 rows
// if a row has that number, set false to 3 squares in that row
tests = this.markRowXrays(tests, square, candidate);
// foreach 3 cols
// if a col has that number, set false to 3 squares in that col
tests = this.markColXrays(tests, square, candidate);
// does bool array have exactly 1 true? if so, square solved
// write the value to this.board
const solvedSquare = this.getSolvedSquare(tests, square, candidate);
if ( solvedSquare ) {
const solvedRow = solvedSquare.getRow();
const solvedCol = solvedSquare.getCol();
if ( ! hintMode ) {
this.board[solvedRow][solvedCol] = candidate;
}
return solvedSquare;
}
}
}
}
getSolvedSquare(tests, square, candidate) {
let count = 0;
// Keep in mind that $square is the top left square of the box, and not the $solvedSquare.
let solvedRow = square.getRow();
let solvedCol = square.getCol();
for ( let i = 0; i <= 2; i++ ) {
for ( let j = 0; j <= 2; j++ ) {
if ( tests[i][j] ) {
count++;
// Figure out square that is solved. Store it in $box_row, $box_col
solvedRow += i;
solvedCol += j;
}
}
}
if ( count === 1 ) {
return new SudokuSquare(solvedRow, solvedCol, candidate);
} else {
return false;
}
}
throwDuplicateNumberError() {
window.alert(`Bad puzzle. A duplicate number was found.`);
// Using throw here because JS doesn`t appear to have the PHP equivalent of break 2 to break out of nested loops and functions
throw `Bad puzzle. A duplicate number was found.`;
}
// If the 3 rows going through this box have the candidate number, mark the squares in those rows as false.
markRowXrays(tests, square, candidate) {
let testResults = tests;
// figure out 3 rows to test
const row = square.getRow();
const rowOffset = Math.floor(row/3)*3;
// foreach 3 rows
for ( let i = 0 + rowOffset; i <= 2 + rowOffset; i++ ) {
// foreach 9 columns
for ( let j = 0; j < 9; j++ ) {
const value = this.board[i][j];
// if candidate
if ( value === candidate ) {
// set correct test_result row squares (3 squares) to false
testResults[i - rowOffset][0] = false;
testResults[i - rowOffset][1] = false;
testResults[i - rowOffset][2] = false;
}
}
}
return testResults;
}
markColXrays(tests, square, candidate) {
let testResults = tests;
// figure out 3 cols to test
const col = square.getCol();
const colOffset = Math.floor(col/3)*3;
// foreach 3 cols
for ( let i = 0 + colOffset; i <= 2 + colOffset; i++ ) {
// foreach 9 rows
for ( let j = 0; j < 9; j++ ) {
const value = this.board[j][i];
// if candidate
if ( value === candidate ) {
// set correct test_result col squares (3 squares) to false
testResults[0][i - colOffset] = false;
testResults[1][i - colOffset] = false;
testResults[2][i - colOffset] = false;
}
}
}
return testResults;
}
markOccupiedSquares(tests, square) {
let testResults = tests;
const row = square.getRow();
const col = square.getCol();
// find top left corner of box
const rowOffset = Math.floor(row/3)*3;
const colOffset = Math.floor(col/3)*3;
// iterate around 3x3 area
for ( let i = 0 + rowOffset; i <= 2 + rowOffset; i++ ) {
for ( let j = 0 + colOffset; j <= 2 + colOffset; j++ ) {
const value = this.board[i][j];
if ( value ) {
testResults[i - rowOffset][j - colOffset] = false;
}
}
}
return testResults;
}
getLegalMoves() {
let numberOfSolutionsSoFar = 0;
let solutionsChecked = 0;
solutionsChecked++;
let legalMoves = new LegalMoves();
const currentSudoku = this;
if ( numberOfSolutionsSoFar > 500 ) {
return legalMoves;
}
if ( ! this.puzzleIsValid() ) {
return legalMoves;
}
if ( this.puzzleIsSolved() ) {
return legalMoves;
}
// foreach boardsquare
for ( let square of currentSudoku.getAllSquares() ) {
// if square is empty
if ( square.getValue() === 0 ) {
// for each possible number 1-9
for ( let i = 1; i <= 9; i++ ) {
const row = square.getRow();
const col = square.getCol();
if ( currentSudoku.isLegalMove(row, col, i) ) {
// create new Sudoku
let nextSudoku = new SudokuBoard();
nextSudoku.parseString(currentSudoku.getString());
// make move
nextSudoku.makeMove(row, col, i);
if ( nextSudoku.puzzleIsSolved() ) {
numberOfSolutionsSoFar++;
}
legalMoves.addMove(nextSudoku.getString());
}
}
}
}
return legalMoves;
}
puzzleIsSolved() {
for ( let i = 0; i < 9; i++ ) {
for ( let j = 0; j < 9; j++ ) {
if ( this.board[i][j] === 0 ) {
return false;
}
}
}
return true;
}
getAllSolutions() {
if ( ! this.puzzleIsValid() ) {
this.throwDuplicateNumberError();
return 0;
}
if ( this.puzzleIsSolved() ) {
return 1;
}
const initialRecursionTracker = new RecursionTracker();
const initialSudoku = new SudokuBoard();
initialSudoku.setBoard(SudokuBoard.cloneBoard(this.board));
// Solve using simple logical techniques first. Filling in a couple of the first squares really helps speed up recursion for certain difficult puzzles.
const solvedEarly = initialSudoku.solveLogically();
initialRecursionTracker.setSudokuToCheck(initialSudoku);
if ( solvedEarly ) {
initialRecursionTracker.addSolution(initialSudoku);
return initialRecursionTracker;
}
const finalRecursionTracker = this.solveRecursively(initialRecursionTracker);
return finalRecursionTracker;
}
solveRecursively(recursionTracker) {
// Benchmark History, RECURSION_LIMIT = 50,000, board = Testing Solution Count 2, DESKTOP-PC, Chrome
// 2508 ms initially
// 2186 ms added/refactored getTrueKey
// 1519 ms added/refactored cloneBoard
// 789 ms added/refactored squareIsSolved
// 298 ms added/refactored setBoard
// 170 ms commented out RegEx in get_legal_move
// 0.4ms added return after for loop in solveRecursively
// 0.1ms tries to logical solve once before trying to recursive solve
// The best way to get speed is to use a combination of iteration (simple logical solving techniques) and recursion. The iteration cuts down on the amount of recursion needed significantly, and recursion checks the rest.
const RECURSION_LIMIT = 1000000000;
const SOLUTION_LIMIT = 500;
if ( recursionTracker.getSolutionCount() >= SOLUTION_LIMIT ) {
return recursionTracker;
}
if ( recursionTracker.getBoardsCheckedCount() > RECURSION_LIMIT ) {
recursionTracker.markEarlyExit();
return recursionTracker;
}
const currentSudoku = recursionTracker.getSudokuToCheck();
// foreach boardsquare
for ( let square of currentSudoku.getAllSquares() ) {
// if square is empty
if ( square.getValue() === 0 ) {
// for each possible number 1-9
for ( let i = 1; i <= 9; i++ ) {
if ( recursionTracker.getBoardsCheckedCount() > RECURSION_LIMIT ) {
recursionTracker.markEarlyExit();
return recursionTracker;
}
const row = square.getRow();
const col = square.getCol();
if ( currentSudoku.isLegalMove(row, col, i, false) ) {
recursionTracker.incrementBoardsChecked();
// create new Sudoku
let nextSudoku = new SudokuBoard();
const board = SudokuBoard.cloneBoard(currentSudoku.board);
nextSudoku.setBoard(board);
// make move
nextSudoku.makeMove(row, col, i);
// propagate forced-moves
if ( ! nextSudoku.propagate() ) {
continue;
}
if ( nextSudoku.puzzleIsSolved() ) {
recursionTracker.addSolution(nextSudoku);
recursionTracker.incrementBoardsChecked();
} else {
recursionTracker.setSudokuToCheck(nextSudoku);
recursionTracker = this.solveRecursively(recursionTracker);
}
}
}
// This line is super important. I didn't have it and the solver was slow as molasses until I figured this out.
// We should find ONE blank square, find a legal move, then make a recursive solve call using the new board.
// The tree should be expanding DOWNWARDS, not SIDEWAYS.
// This line will prune all the sideways calculations except for trying legal moves in one empty square.
// Find that ONE blank square, try 1-9, then get out.
return recursionTracker;
}
}
return recursionTracker;
}
static getTrueKey(array) {
let count = 0;
let trueKey = false;
for ( let key in array ) {
if ( array[key] ) {
trueKey = key;
count++;
}
}
if ( count === 1 ) {
return parseInt(trueKey);
} else {
return false;
}
}
makeEasyPuzzle() {
// Optimization history. Run loop 100 times with no early exit.
// 900-1450ms
// 306- 381ms getLegalMovesForSquare (bitwise) instead of try 100 random numbers
// 188- 245ms makeSolvedPuzzle method: 2x "for" loops instead of "for of" loop
let newBoard;
let triedSoFar = 0;
while ( true ) {
// for ( let i = 0; i < 100; i++ ) {
newBoard = new SudokuBoard();
newBoard.makeSolvedPuzzle();
newBoard.deleteSquares(47); // 47
triedSoFar++;
const recursionTracker = new RecursionTracker();
recursionTracker.setSudokuToCheck(newBoard);
const numberOfSolutions = this.solveRecursively(recursionTracker).getSolutionCount();
const newBoardCopy = new SudokuBoard();
newBoardCopy.parseString(newBoard.getString());
const easyToSolve = newBoardCopy.solveLogically();
// Check for these two "bad" conditions. If true, continue looking for puzzles.
// Recursive solve has multiple solutions
// Simple solver cannot solve it. Difficulty level too hard for the user.
if (numberOfSolutions === 1 && easyToSolve ) {
break;
}
}
console.log(triedSoFar + ` puzzles generated before finding a puzzle that has only 1 solution AND is very easy.`);
this.parseString(newBoard.getString());
}
deleteSquares(howMany) {
let deletedSoFar = 0;
while ( deletedSoFar < howMany ) {
const row = Helper.getRandomInteger(0,8);
const col = Helper.getRandomInteger(0,8);
if ( this.board[row][col] ) {
this.board[row][col] = 0;
deletedSoFar++;
}
}
}
makeSolvedPuzzle() {
do {
newBoard:
for ( let row = 0; row < 9; row++ ) {
for ( let col = 0; col < 9; col++ ) {
const legalMoves = this.getLegalMovesForSquare(row, col);
// Empty arrays are not falsey in JavaScript. Have to be verbose here.
if ( legalMoves.length === 0 ) {
this.restartPuzzle();
break newBoard;
}
const index = Helper.getRandomInteger(0, legalMoves.length-1);
const value = legalMoves[index];
this.makeMove(row, col, value);
}
}
} while ( ! this.puzzleIsSolved() );
}
getLegalMovesForSquare(row, col) {
if ( this.board[row][col] ) {
return false;
}
// binary representation of candidates
// 987654321X
// 1 means the # IS a possible candidate, 0 means the # is NOT a possible candidate
let candidates = 0b1111111110;
for ( let i = 0; i < 9; i++ ) {
// If i is in row/col/house, set candidate from 1 (possible candidate) to 0 (not possible candidate)
// check row
candidates &= ~(1 << this.board[row][i]);
// check column
candidates &= ~(1 << this.board[i][col]);
// check house
// 1 2 3
// 4 5 6
// 7 8 9
const houseTopLeftRow = Math.floor(row/3)*3;
const houseTopLeftCol = Math.floor(col/3)*3;
const houseRow = houseTopLeftRow + Math.floor(i/3);
const houseCol = houseTopLeftCol + (i % 3);
candidates &= ~(1 << this.board[houseRow][houseCol]);
}
let candidatesArray = [];
for ( let i = 1; i <= 9; i++ ) {
if ( candidates & 1 << i ) {
candidatesArray.push(i);
}
}
return candidatesArray;
}
// TODO: rename
// Credit to harold at codereview.stackexchange.com for this code.
// https://codereview.stackexchange.com/questions/239935/javascript-sudoku-recursive-solver
// By taking a bitwise approach, it sped up the solve routine considerably.
propagate() {
// For each row, column and block,
// get a mask indicating which values are already present in it.
// 0b987654321X
// block order is top top top middle middle middle bottom bottom bottom
let rowmask = new Int32Array(9);
let colmask = new Int32Array(9);
let blockmask = new Int32Array(9);
for ( let i = 0; i < 9; i++ ) {
for ( let j = 0; j < 9; j++ ) {
rowmask[i] |= 1 << this.board[i][j];
colmask[j] |= 1 << this.board[i][j];
// | 0 forces integer, no remainder. JavaScript defaults to float, and quietly rounds decimal array indexes.
blockmask[(i / 3 | 0) * 3 + (j / 3 | 0)] |= 1 << this.board[i][j];
}
}
// For each cell, get a mask indicating which values are valid to fill into it.
// Excludes zero, as zero is the lack of a value.
// For a filled cell, the only value it can have is the value it already has.
// For empty cells, the possible values are values that are not already used in the same row/column/block.
let cellmask = new Int32Array(81);
for ( let i = 0; i < 9; i++ ) {
for ( let j = 0; j < 9; j++ ) {
var mask = rowmask[i] | colmask[j] | blockmask[(i / 3 | 0) * 3 + (j / 3 | 0)];
// invert to take the *unused* values
// 0x3FE = 0011_1111_1110 (bits 1 to 9 are set)
cellmask[i * 9 + j] = ~mask & 0x3FE;
if ( this.board[i][j] !== 0 ) {
cellmask[i * 9 + j] = 1 << this.board[i][j];
}
}
}
var changed = false;
do {
changed = false;
// hidden single - only candidate in that house/row/col
for ( let i = 0; i < 9; i++ ) {
var m1 = 0;
var m2 = 0;
for ( let j = 0; j < 9; j++ ) {
var m = cellmask[i * 9 + j];
m2 |= m1 & m;
m1 |= m;
}
for ( let j = 0; j < 9; j++ ) {
var m = cellmask[i * 9 + j];
m &= ~m2;
if ( m !== 0 ) {
cellmask[i * 9 + j] = m & -m;
}
}
}
for ( let j = 0; j < 9; j++ ) {
var m1 = 0;
var m2 = 0;
for ( let i = 0; i < 9; i++ ) {
var m = cellmask[i * 9 + j];
m2 |= m1 & m;
m1 |= m;
}
for ( let i = 0; i < 9; i++ ) {
var m = cellmask[i * 9 + j];
m &= ~m2;
if ( m !== 0 ) {
cellmask[i * 9 + j] = m & -m;
}
}
}
// naked singles - only candidate left in that particular cell
// we just filled a cell with the value 'move'
// remove that as a possible value from cells in
// the same row/column/block
for ( let i = 0; i < 9; i++ ) {
for ( let j = 0; j < 9; j++ ) {
let mask = cellmask[i * 9 + j];
if ( this.board[i][j] !== 0 ) {
continue;
}
if ( mask === 0 ) {
return false;
}
if ( this.isSingleSetBit(mask) ) {
let move = this.getSetBitPosition(mask);
this.makeMove(i, j, move);
changed = true;
// we just filled a cell with the value 'move'
// remove that as a possible value from cells in
// the same row/column/block
for ( let k = 0; k < 9; k++ ) {
cellmask[i * 9 + k] &= ~(1 << move);
cellmask[k * 9 + j] &= ~(1 << move);
}
for ( let k = 0; k < 3; k++ ) {
for ( let l = 0; l < 3; l++ ) {
cellmask[((i / 3 | 0) * 3 + k) * 9 + (j / 3 | 0) * 3 + l] &= ~(1 << move);
}
}
}
}
}
} while (changed);
return true;
}
isSingleSetBit(x) {
return x !== 0 && (x & -x) === x;
}
getSetBitPosition(x) {
for ( let i = 0; i < 31; i++ ) {
if ((x & (1 << i)) !== 0)
return i;
}
return -1;
}
}
class RecursionTracker {
constructor() {
this.numberOfSolutions = 0;
this.solutionList = [];
this.sudokuToCheck = null;
this.boardsChecked = 0;
this.earlyExit = false;
}
getSolutionCount() {
return this.solutionList.length;
}
getSolutionList() {
return this.solutionList;
}
getInfoString() {
let string = ``;
string += Helper.addCommasToNumber(this.getBoardsCheckedCount()) + ` Boards Checked Recursively\r\n`;
if ( this.solutionList.length >= 500 ) {
string += `At Least 500 Solutions. Exited Early.\r\n`;
} else if ( this.solutionList.length === 1 ) {
string += `1 Solution Found\r\n`;
} else {