-
Notifications
You must be signed in to change notification settings - Fork 12
/
sudoku.js
410 lines (390 loc) · 12.1 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
var T = Array.from(new Array(9), () => new Array(9).fill(0));
var Tref = Array.from(new Array(9), () => new Array(9));
var Tsol = Array.from(new Array(9), () => new Array(9).fill(0));
var digits = new Array(10);
var hyp = false;
var hyps = []
//var coeff = [ 0, 1, 9, 81, 729, 6561, 59049, 531441, 4782969];
var curX = 0;
var curY = 0;
var col1 = "#B00";
var col2 = "#0A85FF";
function shuffle(array) {
let counter = array.length;
while (counter > 0) {
let index = Math.floor(Math.random() * counter);
counter--;
let temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
function randomOrderCells() {
var i,j;
var arr = []
for (i=0;i<9;i++) {
for (j=0;j<9;j++) {
arr.push([i,j]);
}
}
return shuffle(arr);
}
// Set cell (y,x) with value n (0 for empty cell)
function setCell(y, x, n) {
if (n==0) Tref[y][x].innerHTML = "";
else Tref[y][x].innerHTML = n.toString();
}
function updateGrid() {
var i,j;
for(i=0;i<9;i++) {
for(j=0;j<9;j++) {
setCell(i,j,T[i][j]);
Tref[i][j].style.color='';
Tref[i][j].style.backgroundColor='';
}
}
}
function init() {
var i, j;
var tbl = document.getElementById("grid");
for(i=0;i<9;i++) {
var r = tbl.insertRow(-1);
r.className = "gridRow";
for(j=0;j<9;j++) {
Tref[i][j] = r.insertCell(-1);
Tref[i][j].className = "gridCell";
if (i%3 == 0) Tref[i][j].className += " topBorder";
if (i%3 == 2) Tref[i][j].className += " bottomBorder";
if (j%3 == 0) Tref[i][j].className += " leftBorder";
if (j%3 == 2) Tref[i][j].className += " rightBorder";
var y = document.createAttribute("y");
var x = document.createAttribute("x");
var click = document.createAttribute("clickable");
click.value = 0;
y.value = i;
x.value = j;
Tref[i][j].setAttributeNode(y);
Tref[i][j].setAttributeNode(x);
Tref[i][j].setAttributeNode(click);
}
}
// click on cells
$("#grid").on("click", "td", function(e) {
clickCell(this);
e.stopPropagation();
});
// digits
for(i=0;i<10;i++)
digits[i] = document.getElementById("digit-"+String(i));
// Buttons
i = $('#digits').height();
j = $('#buttons1').height();
j = Math.floor( (i-j)/2 ) + 4;
$('#buttons1').height( i-j );
$('#buttons1').css("margin-top", String(j)+"px");
document.getElementById("digits").style.display = "none";
document.getElementById("but1").style.color="#000";
document.getElementById("but2").style.color="#B8B8B8";
document.getElementById("but3").style.color="#B8B8B8";
setTimeout(function() { getRandomGrid(96); }, 250);
}
function allowed(A, y,x) {
var i;
var res = [];
var arr = new Array(10).fill(true);
if (A[y][x] > 0) return res;
for(i=0;i<9;i++) arr[A[y][i]] = false;
for(i=0;i<9;i++) arr[A[i][x]] = false;
for(i=0;i<9;i++)
arr[ A[ y-(y%3) + Math.floor(i/3)][x-(x%3) + (i%3) ] ] = false;
for(i=1;i<10;i++)
if (arr[i]) res.push(i);
return res;
}
function bestHypothesis(A) {
var i,j,s;
var bSc = 10;
var bCoords = [9,9];
var bAll = [];
for(i=0;i<9;i++) {
for(j=0;j<9;j++) {
if (A[i][j] == 0) {
s = allowed(A, i,j);
n = s.length;
if (n<bSc) {
bSc = n;
bCoords = [i,j];
bAll = s;
}
}
}
}
return [ bAll, bCoords[0], bCoords[1] ];
}
function _findAcceptableGrid() {
var i;
var [all, y, x] = bestHypothesis(T);
if (y==9) return true;
if (all.length==0) return false; // invalid grid
all = shuffle(all);
for (i=0;i<all.length;i++) {
T[y][x] = all[i];
if(_findAcceptableGrid()) return true;
}
}
function findAcceptableGrid() {
var i,j;
for(i=0;i<9;i++) { for(j=0;j<9;j++) T[i][j] = 0; }
while(_findAcceptableGrid() != true) {
for(i=0;i<9;i++) { for(j=0;j<9;j++) T[i][j] = 0; }
}
for(i=0;i<9;i++) { for(j=0;j<9;j++) Tsol[i][j] = T[i][j]; }
}
function _findValidityClass(A, n) {
var i;
var sol = -1;
var [all, y, x] = bestHypothesis(A);
if (y==9) return 1;
if (all.length==0) return -1; // invalid grid
// if (all.length > 1) n++; // make a new hypothesis
for (i=0;i<all.length;i++) {
A[y][x] = all[i];
r = _findValidityClass(A, n);
A[y][x] = 0;
if (r >= 0) {
if (sol >= 0) return -2; // at least two solutions exist
// sol = r;
sol = all.length * r;
} else if (r==-2) return -2;
}
return sol;
}
function _getRandomGrid2(nlevel) {
var i, j, v, y1, x1, y2, x2, s;
var sc = -2;
var zeros = [];
var kept = [];
for(i=0;i<9;i++) { for(j=0;j<9;j++) kept.push([i,j]); }
findAcceptableGrid();
for(i=0;i<nlevel;i++) {
j = Math.floor(Math.random() * kept.length);
y1 = kept[j][0]; x1 = kept[j][1];
T[y1][x1] = 0;
v = _findValidityClass(T, 0);
if(v < 0) T[y1][x1] = Tsol[y1][x1];
else {
sc = v; zeros.push([y1,x1]);
kept[j] = kept[kept.length-1]; kept.pop();
}
j = Math.floor(Math.random() * kept.length);
y1 = kept[j][0]; x1 = kept[j][1];
s = Math.floor(Math.random() * zeros.length);
y2 = zeros[s][0]; x2 = zeros[s][1];
T[y1][x1] = 0; T[y2][x2] = Tsol[y2][x2];
v = _findValidityClass(T, 0);
if(v < sc)
{
T[y1][x1] = Tsol[y1][x1];
T[y2][x2] = 0;
}
else {
sc = v;
zeros[s] = [y1, x1];
kept[j] = [y2, x2];
}
}
return sc;
}
/*
function _getRandomGrid2(nlevel) {
var i, j, k, v, y1, x1, y2, x2, x3, y3, s;
var sc = -2;
var zeros = [];
var kept = [];
for(i=0;i<9;i++) { for(j=0;j<9;j++) kept.push([i,j]); }
findAcceptableGrid();
for(i=0;i<nlevel;i++) {
// first step
j = Math.floor(Math.random() * kept.length);
y1 = kept[j][0]; x1 = kept[j][1];
T[y1][x1] = 0;
v = _findValidityClass(T, 0);
if(v < 0) T[y1][x1] = Tsol[y1][x1];
else {
sc = v; zeros.push([y1,x1]);
kept[j] = kept[kept.length-1]; kept.pop();
}
// second step
j = Math.floor(Math.random() * kept.length);
k = Math.floor(Math.random() * (kept.length-1));
if (j==k) k = kept.length-1;
if (j<k) { s=j;j=k;k=s; }
y1 = kept[j][0]; x1 = kept[j][1];
y2 = kept[k][0]; x2 = kept[k][1];
s = Math.floor(Math.random() * zeros.length);
y3 = zeros[s][0]; x3 = zeros[s][1];
T[y1][x1] = 0; T[y2][x2] = 0; T[y3][x3] = Tsol[y3][x3];
v = _findValidityClass(T, 0);
if(v < sc)
{
T[y1][x1] = Tsol[y1][x1];
T[y2][x2] = Tsol[y2][x2];
T[y3][x3] = 0;
}
else {
sc = v;
zeros[s] = [y1, x1]; zeros.push([y2,x2]);
kept[j] = kept[kept.length-1]; kept.pop();
kept[k] = [y3, x3];
}
}
return sc;
}
*/
function _getRandomGrid(nlevel) {
console.log(_getRandomGrid2(nlevel));
updateGrid();
for(i=0;i<9;i++) {
for(j=0;j<9;j++) {
if (T[i][j] == 0) Tref[i][j].setAttribute("clickable", 1);
else Tref[i][j].setAttribute("clickable", 0);
}
}
// make clickable
$( "#waiting" ).popup( "close" )
//$.mobile.loading().hide();
hyp = false;
document.getElementById("but1").style.color="#000";
document.getElementById("but2").style.color="#B8B8B8";
document.getElementById("but3").style.color="#B8B8B8";
}
function getRandomGrid(nlevel) {
$( "#waiting" ).popup( "open" )
//$.mobile.loading().show();
setTimeout(function() { _getRandomGrid(nlevel); }, 0);
}
function elsewhere() {
Tref[curY][curX].style.backgroundColor = "";
document.getElementById("digits").style.display = "none";
document.getElementById("buttons1").style.display = "inline-block";
}
function clickCell(cell) {
var c = Number(cell.getAttribute("clickable"));
if (c==1) {
var y = Number(cell.getAttribute("y"));
var x = Number(cell.getAttribute("x"));
Tref[curY][curX].style.backgroundColor = "";
$( "#digits" ).off("click", "**");
curY = y;
curX = x;
cell.style.backgroundColor = "#BBB";
document.getElementById("digits").style.display = "inline-block";
document.getElementById("buttons1").style.display = "none";
var a = allowed(T, y, x);
var d = new Array(10).fill(false);
for(i=0;i<a.length;i++) d[a[i]] = true;
d[0] = true;
for(i=0;i<10;i++) {
if (d[i]) {
let v = i;
let col = (hyp)?col2:col1;
let h = hyp;
digits[i].style.color=col;
digits[i].style.borderColor=col;
$("#digits").on("click", "#digit-"+String(i), function(e) {
T[y][x] = v;
setCell(y, x, v);
Tref[y][x].style.color = col;
if (h) hyps.push(Tref[y][x]);
//e.stopPropagation();
});
} else {
digits[i].style.color="#B8B8B8";
digits[i].style.borderColor="#B8B8B8";
digits[i].style.cursor="pointer";
}
}
} else elsewhere();
}
function hypothesis1() {
if(!hyp) {
document.getElementById("but1").style.color="#B8B8B8";
document.getElementById("but2").style.color="#000";
document.getElementById("but3").style.color="#000";
hyp = true;
}
}
function hypothesis2() {
var i;
console.log(hyps);
for(i=0;i<hyps.length;i++) hyps[i].style.color = col1;
hyps = []
hyp = false;
document.getElementById("but1").style.color="#000";
document.getElementById("but2").style.color="#B8B8B8";
document.getElementById("but3").style.color="#B8B8B8";
}
function hypothesis3() {
var i;
console.log(hyps);
for(i=0;i<hyps.length;i++) {
hyps[i].innerHTML = "";
var y = Number(hyps[i].getAttribute("y"));
var x = Number(hyps[i].getAttribute("x"));
T[y][x] = 0;
}
hyps = []
hyp = false;
document.getElementById("but1").style.color="#000";
document.getElementById("but2").style.color="#B8B8B8";
document.getElementById("but3").style.color="#B8B8B8";
}
function restart() {
var i,j;
for(i=0;i<9;i++) {
for(j=0;j<9;j++) {
if(Number(Tref[i][j].getAttribute("clickable")) == 1) {
T[i][j] = 0;
setCell(i,j,0);
}
}
}
hyp = false;
document.getElementById("but1").style.color="#000";
document.getElementById("but2").style.color="#B8B8B8";
document.getElementById("but3").style.color="#B8B8B8";
}
function newRandomGrid(nlevel) {
$( "#newGrid" ).popup( "close" );
setTimeout(function() { getRandomGrid(nlevel); }, 250);
}
function solve() {
for(i=0;i<9;i++) {
for(j=0;j<9;j++) {
if (T[i][j]==0) {
T[i][j] = Tsol[i][j];
setCell(i,j, T[i][j]);
Tref[i][j].style.color = "#B8B8B8";
} else if (T[i][j] != Tsol[i][j]) {
T[i][j] = Tsol[i][j];
setCell(i,j, T[i][j]);
Tref[i][j].style.color = "#B8B8B8";
Tref[i][j].style.backgroundColor = "#FBB";
}
}
}
hyp = false;
document.getElementById("but1").style.color="#000";
document.getElementById("but2").style.color="#B8B8B8";
document.getElementById("but3").style.color="#B8B8B8";
}
function check() {
for(i=0;i<9;i++) {
for(j=0;j<9;j++) {
if ((T[i][j] != Tsol[i][j])&&(T[i][j] != 0)) {
Tref[i][j].style.backgroundColor = "#FBB";
}
}
}
}