-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku.js
214 lines (186 loc) · 6.15 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
/*
* Sudoku is html/javascript code that lets you solve a sudoku puzzle.
* Copyright (C) 2020 Arun Kunchithapatham
*
* This file is part of Sudoku.
*
* Sudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Sudoku. If not, see <http://www.gnu.org/licenses/>.
*/
class Sudoku {
constructor(size, initialValues) {
this.size = size;
this.base = Math.floor(Math.sqrt(this.size));
this.initialValues = initialValues;
this.squares = [];
this.rows = [];
this.cols = [];
this.blks = [];
this.dfsCounter = 0;
this.numSolutions = 0;
for (let i = 0; i < this.size; i++) {
this.rows[i] = [];
this.cols[i] = [];
this.blks[i] = [];
}
for(let idx = 0; idx < this.size * this.size; idx++) {
let rowidx = Math.floor(idx/this.size);
let colidx = idx%this.size;
let blkidx = Math.floor(rowidx/this.base)*this.base +
Math.floor(colidx/this.base);
let val = parseInt(this.initialValues[idx]);
let sqtype = "FIXED";
if (isNaN(val)) {
val = 0;
sqtype = "FREE";
}
let sq = new SudokuSqr(idx, rowidx, colidx, blkidx, sqtype,
this.rows[rowidx], this.cols[colidx], this.blks[blkidx], val);
this.squares[idx] = sq;
this.rows[rowidx].push(sq);
this.cols[colidx].push(sq);
this.blks[blkidx].push(sq);
}
for (let idx = 0; idx < this.size * this.size; idx++) {
this.squares[idx].calcChoices();
}
}
isValid() {
for(let i = 0; i < this.size; i++) {
let rowVals = {};
for (let j = 0; j < this.size; j++) {
let val = this.rows[i][j].val;
if (val != 0 && val in rowVals) {
return false;
}
rowVals[val] = 1;
}
}
for(let i = 0; i < this.size; i++) {
let colVals = {};
for (let j = 0; j < this.size; j++) {
let val = this.cols[i][j].val;
if (val != 0 && val in colVals) {
return false;
}
colVals[val] = 1;
}
}
for(let i = 0; i < this.size; i++) {
let blkVals = {};
for (let j = 0; j < this.size; j++) {
let val = this.blks[i][j].val;
if (val != 0 && val in blkVals) {
return false;
}
blkVals[val] = 1;
}
}
return true;
}
getString() {
let str = "";
for(let idx = 0; idx < this.size * this.size; idx++) {
let val = this.squares[idx].val;
if (val == 0) {
val = ".";
}
str += val;
}
return str;
}
solve() {
this.dfsCounter = 0;
this.numSolutions = 0;
this.tryDFS(0, this.getNextSquare(0));
if (this.numSolutions == 0) {
console.log("Unsolvable!");
return false;
}
else if (this.numSolutions == 1) {
return true;
}
else {
console.log("Invalid Sudoku! Multiple solutions found!");
return false;
}
}
tryDFS(i, sq) {
this.dfsCounter += 1;
if (i == this.size * this.size) {
this.numSolutions += 1;
return true;
}
if (null == sq || sq.val != 0) {
return this.tryDFS(i+1, this.getNextSquare(i+1));
}
else {
if (Object.keys(sq.choices).length == 0) {
return false;
}
else {
let solved = false;
let choices = Object.keys(sq.choices);
choices = choices
.map((a) => ({sort: Math.random(), value: a}))
.sort((a, b) => a.sort - b.sort)
.map((a) => a.value);
for (let c = 0; c < choices.length; c++) {
sq.setValue(choices[c]);
if (this.tryDFS(i+1, this.getNextSquare(i+1))) {
return true;
}
else {
sq.unsetValue();
}
}
return solved;
}
}
}
getNextSquare(i) {
let candidates = this.squares.filter((sq) => sq.val == 0)
.sort((a,b) => (Object.keys(a.choices).length -
Object.keys(b.choices).length));
return candidates[0];
}
getFilledCount() {
let filledCount = 0;
for (let i = 0; i < this.size * this.size; i++) {
if (this.squares[i].type == "FIXED") filledCount += 1;
}
return filledCount;
}
reset() {
for (let i = 0; i < this.size * this.size; i++) {
if (this.squares[i].type == "FREE") {
this.squares[i].setValue(0);
this.squares[i].calcChoices();
}
}
}
print() {
for(let i = 0; i < this.size; i++) {
let rowStr = "";
for(let j = 0; j < this.size; j++) {
let idx = i * this.size + j;
let val = this.squares[idx].val;
if (val == 0) {
val = ".";
}
rowStr += val + " ";
}
console.log(rowStr);
}
}
}