-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver
More file actions
385 lines (292 loc) · 9.71 KB
/
solver
File metadata and controls
385 lines (292 loc) · 9.71 KB
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
<html>
<head>
<script>
///////////////////INITIAL DATA FOR DE-DIAGONALIZED GRID//////////////////
dgd=[
[1,2,3,7,8,9,4,5,6],
[7,8,9,4,5,6,1,2,3],
[4,5,6,1,2,3,7,8,9],
[6,1,2,3,7,8,9,4,5],
[9,4,5,6,1,2,3,7,8],
[3,7,8,9,4,5,6,1,2],
[5,6,1,2,3,7,8,9,4],
[8,9,4,5,6,1,2,3,7],
[2,3,7,8,9,4,5,6,1]
];
//////////////////////////END OF DATA//////////////////////////////////////
/////////////////////DRAWS THE SUDOKU GRID/////////////////////////////////
function drawDiagoduko(arr) {
for (i=0;i<9;i++) {
for (j=0;j<9;j++) document.write(arr[i][j]+" ");
document.write("<br>");
}
document.write("<br>");
}
////////////////////////END OF DRAW ROUTINE////////////////////////////////
////////////////////CHECK FOR DIAGONALS BEING UNALIGNED////////////////////
function checkDiagonals(arr) {
for (i=0;i<8;i++) {
j=-1;
while (j++<7) {
if (arr[i][j]==arr[i+1][j+1]) {alert("Diagonal \\ at "+(j+1)+", "+(i+1));break;}
if (arr[i][j+1]==arr[i+1][j]) {alert("Diagonal \/ at "+(j+2)+", "+(i+1));break;}
}}}
function checkDiagonalsQ(arr) {
retVal=true;
for (i=0;i<8;i++) {
j=-1;
while (j++<7) {
if (arr[i][j]==arr[i+1][j+1]) {retVal=false;break;}
if (arr[i][j+1]==arr[i+1][j]) {retVal=false;break;}
}}
return retVal;
}
//////////////////////END OF DIAGONAL CHECKS/////////////////////////////////
/////////////////BASIC CHECKS FOR ROWS, COLUMNS AND BLOCKS///////////////////
function checkSudoku(arr) {
e=new Array();
ec=0;
checkHorz(arr);
checkVert(arr);
checkBlock(arr);
if (ec>0) alert(e.join("")+ec);
checkXoox(arr);
}
function checkHorz(arr2) {
a=new Array();
for (i=0;i<9;i++) {
for (j=0;j<9;j++) a[j]=arr2[i][j];
b=a.sort(function(a,b) {return a-b;});
for (j=0;j<8;j++) if (b[j]==b[j+1]) e[ec++]="Horizontal Row : "+(i+1)+"\n";
}}
function checkVert(arr2) {
a=new Array();
for (i=0;i<9;i++) {
for (j=0;j<9;j++) a[j]=arr2[j][i];
b=a.sort(function(a,b) {return a-b;});
for (j=0;j<8;j++) if (b[j]==b[j+1]) e[ec++]="Vertical Column : "+(i+1)+"\n";
}}
function checkBlock(arr2) {
a=new Array();
for (i=0;i<3;i++)
for (j=0;j<3;j++) {
for (k=0;k<3;k++) for (l=0;l<3;l++) a[k*3+l]=arr2[i*3+k][j*3+l];
b=a.sort(function(a,b) {return a-b;});
for (d=0;d<8;d++) if (b[d]==b[d+1]) e[ec++]="Block : "+(i+1)+" , "+(j+1)+"\n";
}}
function checkXoox(arr2) {
x=new Array();
xc=0;
for (i=0;i<8;i++)
for (j=0;j<8;j++)
for (k=i+1;k<9;k++)
for (m=j+1;m<9;m++)
if (arr2[i][j]==arr2[k][m] && arr2[i][m]==arr2[k][j]) {
x[xc++]="Xoox at "+(i+1)+", "+(j+1)+" with "+(k+1)+", "+(m+1)+"\n";}
if (xc>0) alert(x.join("")+xc);
}
///////////////////////////END BASIC CHECKS////////////////////////////////
//////////////////////BASIC SUDOKU PUZZLE MAKER////////////////////////////
function makeGame() {
success=false;
while (!success) {
ds=new Array();
for (i=0;i<9;i++) ds[i]=dgd[i];
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
if (Math.random()<=0.5) ds=flipOverH(ds);
if (Math.random()<=0.5) ds=flipOverV(ds);
if (Math.random()<=0.15) ds=rotate(ds);
if (Math.random()<=0.25) ds=rotate(ds);
if (Math.random()<=0.35) ds=rotate(ds);
if (Math.random()<=0.45) ds=rotate(ds);
if (Math.random()<=0.55) ds=rotate(ds);
if (Math.random()<=0.65) ds=rotate(ds);
if (Math.random()<=0.75) ds=rotate(ds);
if (Math.random()<=0.85) ds=rotate(ds);
if (Math.random()<=0.95) ds=rotate(ds);
if (Math.random()<=0.5) ds=flipOverH(ds);
if (Math.random()<=0.5) ds=flipOverV(ds);
if (Math.random()<=0.5) ds=swapRow(ds,0,1);
if (Math.random()<=0.5) ds=swapRow(ds,7,8);
if (Math.random()<=0.5) ds=swapColumn(ds,0,1);
if (Math.random()<=0.5) ds=swapColumn(ds,7,8);
success=checkDiagonalsQ(ds);
}
drawDiagoduko(ds);
checkDiagonals(ds);
checkSudoku(ds);
return ds;
}
function selectCells(arr) {
c=new Array();
for (i=0;i<9;i++) c[i]=["_","_","_","_","_","_","_","_","_"];
size=40; // number of initials cell given
gc=0;
while (gc<size) {
p1=Math.floor(Math.random()*9);
p2=Math.floor(Math.random()*9);
if (c[p1][p2]=="_") {gc++;c[p1][p2]=ds[p1][p2];}
}
return c;
}
//////////////////////END OF SUDOKU MAKER/////////////////////////////////
////////////////////////SUDOKU GRID MANIPULATIONS/////////////////////////
function digitSwap(n1,n2,arr) {
a=new Array();
for (i=0;i<9;i++) a[i]=arr[i];
for (i=0;i<9;i++) for (j=0;j<9;j++) if (a[i][j]==n1) a[i][j]=0;
for (i=0;i<9;i++) for (j=0;j<9;j++) if (a[i][j]==n2) a[i][j]=n1;
for (i=0;i<9;i++) for (j=0;j<9;j++) if (a[i][j]==0) a[i][j]=n2;
return a;
}
function flipOverH(arr) {
arr.reverse();
return arr;
}
function flipOverV(arr) {
for (i=0;i<9;i++) arr[i].reverse();
return arr;
}
function rotate(arr) {
a=new Array();
b=new Array();
for (i=0;i<9;i++) a[i]=arr[i];
for (i=0;i<9;i++) b[i]=[0,0,0,0,0,0,0,0,0];
for (i=0;i<9;i++) for (j=0;j<9;j++) b[j][8-i]=a[i][j];
arr=b;
return arr;
}
function swapRow(arr,r1,r2) {
a=new Array();
b=new Array();
for (i=0;i<9;i++) a[i]=arr[r1][i];
for (i=0;i<9;i++) b[i]=arr[r2][i];
for (i=0;i<9;i++) arr[r1][i]=b[i];
for (i=0;i<9;i++) arr[r2][i]=a[i];
return arr;
}
function swapColumn(arr,c1,c2) {
a=new Array();
b=new Array();
for (i=0;i<9;i++) a[i]=arr[i][c1];
for (i=0;i<9;i++) b[i]=arr[i][c2];
for (i=0;i<9;i++) arr[i][c1]=b[i];
for (i=0;i<9;i++) arr[i][c2]=a[i];
return arr;
}
////////////////////END OF SUDOKU GRID MANIPULATIONS//////////////////////
/////////////////////////MAIN CODE////////////////////////////////////////
gameCore=makeGame();
game=selectCells(gameCore);
/////////////////////////END OF MAIN CODE////////////////////////////////
////////////////////////FORMAT PUZZLE ROUTINES///////////////////////////
for (i=0;i<9;i++) {
for (j=0;j<9;j++)
document.write("<input type=\"text\" size=1 value="+(game[i][j]=="_"?" ":game[i][j])+">");
document.write("<br>");
}
document.write("<br>");
////////////////////END OF FORMAT PUZZLE ROUTINES////////////////////////
///////// //////////// /////////////// ///////////
///////////////////////////SOLUTION ROUTINES//////////////////////////////
/////////////////////////COMPLETELY UN-SOLVED GRID///////////////////////
s=new Array();
for (i=0;i<9;i++) {
s[i]=new Array();
for (j=0;j<9;j++) s[i][j]=[1,2,3,4,5,6,7,8,9];
}
/////////////////////END OF COMPLETELY UN-SOLVED GRID////////////////////
////////////////////////FILL IN WITH 'GAME' DATA/////////////////////////
for (i=0;i<9;i++)
for (j=0;j<9;j++) if (game[i][j]!="_") s[i][j]=[game[i][j]];
////////////////////END OF FILL IN WITH 'GAME' DATA//////////////////////
/////////////////////////////SOLUTION FUNCTIONS//////////////////////////
function checkLine(ln) {
lc=0;
for (i=0;i<9;i++) if (s[ln][i].length>1) {lc++;sp=i;}
if (lc>1) return false;
if (lc==0) return false;
s[ln][sp]=gameCore[ln][sp];
return true;
}
function checkRow(rw) {
lc=0;
for (i=0;i<9;i++) if (s[i][rw].length>1) {lc++;sp=i;}
if (lc>1) return false;
if (lc==0) return false;
s[sp][rw]=gameCore[sp][rw];
return true;
}
function checkGrid(gx,gy) {
lc=0;
for (i=0;i<3;i++) for (j=0;j<3;j++) if (s[gy*3+j][gx*3+i].length>1) {lc++;spi=i;spj=j;}
if (lc>1) return false;
if (lc==0) return false;
s[gy*3+spj][gx*3+spi]=gameCore[gy*3+spj][gx*3+spi];
return true;
}
function zapCell(zx,zy) {
x=new Array();
xc=0;
c=false;
if (s[zy][zx].length>1) {
for (i=0;i<9;i++) if (i!=zy && s[zy][i].length==1) {s[zy][zx][s[zy][i]-1]=0;c=true;}
for (i=0;i<9;i++) if (i!=zx && s[i][zx].length==1) {s[zy][zx][s[i][zx]-1]=0;c=true;}
gx=(zx-zx%3)/3;gy=(zy-zy%3)/3;
for (i=0;i<3;i++) for (j=0;j<3;j++)
if (i!=zx%3 && j!=zy%3 && s[gy+j][gx+i].length==1) {s[zy][zx][s[gy+j][gx+i]-1]=0;c=true;}
if (zx>0 && zy>0 && zx<8 && zy<8)
for (i=-1;i<2;i+=2) for (j=-1;j<2;j+=2)
if (s[zy+j][zx+i].length==1) {s[zy][zx][s[zy+j][zx+i]-1]=0;c=true;}
}
zc=0;
nd=0;
for (i=0;i<9;i++) if (s[zy][zx][i]==0) zc++; else nd=i+1;
if (zc==8) {
s[zy][zx]=[nd];
x[xc++]="Found "+(zx+1)+", "+(zy+1)+" = "+nd;
}
//if (xc>0) alert(x.join(""));
return c>0;
}
////////////////////////END OF SOLUTION FUNCTIONS////////////////////////
dc=true;
while(dc==true) {
dc=false;
for (j=0;j<9;j++) if (checkLine(j)) dc=true;
for (j=0;j<9;j++) if (checkRow(j)) dc=true;
for (k=0;k<3;k++) for (l=0;l<3;l++) if (checkGrid(k,l)) dc=true;
for (k=0;k<9;k++) for (l=0;l<9;l++) zapCell(k,l);
}
drawDiagoduko(s);
for (i=0;i<9;i++) {
for (j=0;j<9;j++)
document.write("<input type=\"text\" size=1 value="+(s[i][j].length>1?" ":s[i][j])+">");
document.write("<br>");
}
document.write("<br>");
</script>
</head>
<body>
<button onclick="location.reload();">New Puzzle</button>
</body>
</html>