-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.js
460 lines (396 loc) · 9.65 KB
/
solver.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
function circle(radius, name)
{
this.radius=radius;
this.name=name;
this.setRadius=setRadius;
}
function setRadius(newRadius)
{
this.radius = newRadius;
}
function CSpace()
{
this.x = 0; this.y = 0; this.sqId = 0; this.number = 0; this.availables = 0;
this.could_be = new Array(10);//[false, false, false, false, false, false, false, false, false, false];
for(z = 0; z < 10; z++)
this.could_be[z] = false;
this.getX = getX;
this.getY = getY;
this.getSquare = getSquare;
this.getNum = getNum;
this.getAvailableCount = getAvailableCount;
this.getPossible = getPossible;
this.setX = setX;
this.setY = setY;
this.setSquare = setSquare;
this.setNum = setNum;
this.setAvailableCount = setAvailableCount;
this.setPossible = setPossible;
}
function getX()
{
return this.x;
}
function getY()
{
return this.y;
}
function getSquare()
{
return this.sqId;
}
function getNum()
{
return this.number;
}
function getAvailableCount()
{
return this.availables;
}
function getPossible(num)
{
return this.could_be[num];
}
function setX(newX)
{
this.x = newX;
}
function setY(newY)
{
this.y = newY;
}
function setSquare(newSqId)
{
this.sqId = newSqId;
}
function setNum(newNum)
{
this.number = newNum;
}
function setAvailableCount(newAvail)
{
this.availables = newAvail;
}
function setPossible(num, state)
{
this.could_be[num] = state;
}
function CBoard()
{
this.spaces = new Array(81);
for(c = 0; c < 81; c++)
this.spaces[c] = new CSpace();
for(c = 0; c < 9; c++)
for(i = 0; i < 9; i++)
{
spaceId = c * 9 + i;
this.spaces[spaceId].setNum(0);
for(k = 1; k < 10; k++)
this.spaces[spaceId].setPossible(k, true);
this.spaces[spaceId].setX(c);
this.spaces[spaceId].setY(i);
this.spaces[spaceId].setSquare(3);
if(i < 6)
this.spaces[spaceId].setSquare(2);
if(i < 3)
this.spaces[spaceId].setSquare(1);
if(c > 2)
this.spaces[spaceId].setSquare(this.spaces[spaceId].getSquare() + 3);
if(c > 5)
this.spaces[spaceId].setSquare(this.spaces[spaceId].getSquare() + 3);
}
this.solved = false; this.stuck = false; this.impossible = false; this.count = 0;
this.assignNum= assignNum
this.getCount= getCount
this.isStuck= isStuck
this.isSolved =isSolved
this.isImpossible= isImpossible
this.availability_check= availability_check
this.checkSolvedOrStuck= checkSolvedOrStuck
this.easysolves= easysolves
this.equals= equals
this.guess =guess
this.impossibility_check= impossibility_check
this.read = read;
this.print = print;
}
function print()
{
for(d = 0; d < 81; d++)
if(this.spaces[d].getNum() != 0)
document.getElementById("i"+(d+1)).value = this.spaces[d].getNum();
else
document.getElementById("i"+(d+1)).value = "";
}
function read()
{
for(f = 0; f < 81; f++)
this.assignNum(f,document.getElementById("i"+(f+1)).value);
}
function assignNum(space, num)
{
this.spaces[space].setNum(num);
}
function getCount() {
return this.count;
}
function isStuck() {
return this.stuck;
}
function isSolved() {
return this.solved;
}
function isImpossible() {
return this.impossible;
}
function availability_check()
{
for(c = 0; c < 81; c++)
{
if(this.spaces[c].getNum() != 0)
for(i = 0; i < 10; i++)
this.spaces[c].setPossible(i, false);
for(i = 0; i < 81; i++)
{
if(c == i)
continue;
if( (this.spaces[c].getX() == this.spaces[i].getX() || this.spaces[c].getY() == this.spaces[i].getY() || this.spaces[c].getSquare() == this.spaces[i].getSquare()) && this.spaces[i].getNum() != 0 && c != i)
this.spaces[c].setPossible(this.spaces[i].getNum(), false);
}
if(this.spaces[c].getNum() == 0)
{
this.spaces[c].setAvailableCount(0);
for(k = 1; k < 10; k++)
if(this.spaces[c].getPossible(k))
this.spaces[c].setAvailableCount(this.spaces[c].getAvailableCount() + 1);
}
}
}
function checkSolvedOrStuck()
{
tcount = 0;
this.solved = true;
for(c = 0; c < 81; c++)
{
if(this.spaces[c].getNum() == 0)
this.solved = false;
else
tcount ++;
}
if(tcount == this.count)
this.stuck = true;
this.count = tcount;
}
function easysolves()
{
first_available = 0, second_available = 0;
for(var c = 0; c < 81; c++)
{
available_count = 0;
for(k = 1; k < 10; k++)
if(this.spaces[c].getPossible(k) == true)
{
available_count += 1;
if(available_count == 1)
first_available = k;
if(available_count == 2)
second_available = k;
}
if(available_count == 1)
for(k = 1; k < 10; k++)
if(this.spaces[c].getPossible(k) == true)
{
this.spaces[c].setNum(k);
this.availability_check();
}
if(available_count == 2)
{
for(i = 0; i < 81; i++)
{
if(i == c)
continue;
if(this.spaces[i].getAvailableCount() == 2 && this.spaces[i].getPossible(first_available) && this.spaces[i].getPossible(second_available))
{
if(this.spaces[i].getX() == this.spaces[c].getX())
for(k = 0; k < 81; k++)
{
if(k == i || k == c)
continue;
if(this.spaces[k].getX() == this.spaces[c].getX())
{
this.spaces[k].setPossible(first_available, false);
this.spaces[k].setPossible(second_available, false);
}
}
if(this.spaces[i].getY() == this.spaces[c].getY())
for(k = 0; k < 81; k++)
{
if(k == i || k == c)
continue;
if(this.spaces[k].getY() == this.spaces[c].getY())
{
this.spaces[k].setPossible(first_available, false);
this.spaces[k].setPossible(second_available, false);
}
}
if(this.spaces[i].getSquare() == this.spaces[c].getSquare())
for(k = 0; k < 81; k++)
{
if(k == i || k == c)
continue;
if(this.spaces[k].getSquare() == this.spaces[c].getSquare())
{
this.spaces[k].setPossible(first_available, false);
this.spaces[k].setPossible(second_available, false);
}
}
}
}
}
if(this.spaces[c].getNum() == 0)
for(k = 1; k < 10; k++)
{
if(this.spaces[c].getPossible(k) == true)
{
oneleftX = true;
oneleftY = true;
oneleftSq = true;
for(i = 0; i < 81; i++)
{
if(i == c)
continue;
if(this.spaces[i].getX() == this.spaces[c].getX() && this.spaces[i].getPossible(k) == true)
oneleftX = false;
if(this.spaces[i].getY() == this.spaces[c].getY() && this.spaces[i].getPossible(k) == true)
oneleftY = false;
if(this.spaces[i].getSquare() == this.spaces[c].getSquare() && this.spaces[i].getPossible(k) == true)
oneleftSq = false;
}
if(oneleftX || oneleftY || oneleftSq)
{
this.spaces[c].setNum(k);
this.availability_check();
}
}
}
}
}
function equals(right)
{
for(c = 0; c < 81; c++)
{
this.spaces[c].setNum(right.spaces[c].getNum());
this.spaces[c].setAvailableCount(right.spaces[c].getAvailableCount());
this.spaces[c].setSquare(right.spaces[c].getSquare());
this.spaces[c].setX(right.spaces[c].getX());
this.spaces[c].setY(right.spaces[c].getY());
for(i = 1; i < 10; i++)
{
this.spaces[c].setPossible(i, right.spaces[c].getPossible(i));
}
}
}
function guess()
{
guess1 = Math.floor(Math.random()*81);
while(this.spaces[guess1].getNum() != 0)
guess1 = Math.floor(Math.random()*81);
guess2 = Math.floor(Math.random()*9)+1;
while(this.spaces[guess1].getPossible(guess2) == false)
guess2 = Math.floor(Math.random()*9)+1;
this.spaces[guess1].setNum(guess2);
}
function impossibility_check()
{
for(c = 0; c < 81; c++)
{
zcount = 0;
if(this.spaces[c].getNum() == 0)
{
for(i = 1; i < 10; i++)
if(this.spaces[c].getPossible(i))
zcount ++;
if(zcount == 0)
this.impossible = true;
}
}
}
function solve(puzzle){
guessing = new CBoard();
puzzle.print();
while(!puzzle.isSolved() && !puzzle.isStuck())
{
puzzle.availability_check();
puzzle.easysolves();
puzzle.checkSolvedOrStuck();
puzzle.impossibility_check();
if(puzzle.isImpossible()){
document.getElementById("first").innerHTML=("this is impossible");
return 0;
}
//PRINT(puzzle);
//cin >> i;
}
if(puzzle.isStuck())
{
resetCount = 0;
//count = 0;
guessing.equals(puzzle);
guessing.guess();
//cout << "guessing...\n";
while(!guessing.isSolved())
{
guessing.availability_check();
guessing.easysolves();
guessing.checkSolvedOrStuck();
guessing.impossibility_check();
if(guessing.isImpossible())
{
resetCount += 1;
guessing.equals(puzzle);
guessing.guess();
}
if(guessing.isStuck())
guessing.guess();
if(resetCount > 50){
document.getElementById("first").innerHTML=("I can't seem to solve this");
return 0;
}
}
//cout << "\n\n";
}
if(puzzle.isSolved())
puzzle.print();
if(guessing.isSolved()){
guessing.print();
puzzle.equals(guessing);
}
document.getElementById("first").innerHTML=("Solved!");
//cin >> i;
}
function setUp()
{
katze = new CBoard();
brot = false;
while(!brot){
katze.checkSolvedOrStuck();
for(var g = 0; g < 25; g++){
katze.guess();
katze.availability_check();
//katze.easysolves();
katze.checkSolvedOrStuck();
katze.impossibility_check();
}
brot = true;
}
katze.print();
}
function main()
{
bob = new CBoard();
bob.read();
solve(bob);
//document.getElementById("first").innerHTML=(bob.getCount());
//fred.setRadius(document.getElementById("i0").value);
//document.getElementById("first").innerHTML=(fred.radius + " this is gaaaayyy\n");
}