-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.min.js
31 lines (31 loc) · 6.81 KB
/
sudoku.min.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
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).Sudoku=t()}(this,(function(){"use strict"
var e=function(){function e(e){this.value=e}return e.prototype.get=function(){return this.value},e.prototype.set=function(e){this.value=e},e.prototype.isSolid=function(){return this.solid},e.prototype.wasSolved=function(){return this.solved},e}(),t=function(){function e(e){this.value=e,this.changeable=!1}return e.prototype.isChangeable=function(){return this.changeable},e.prototype.get=function(){return this.value.get()},e.prototype.set=function(e){this.value.set(e)},e.prototype.isEmpty=function(){return 0===this.get()},e}(),r=function(e,t){return(r=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)}
function n(e,t){function n(){this.constructor=e}r(e,t),e.prototype=null===t?Object.create(t):(n.prototype=t.prototype,new n)}var o=function(){function e(){this.queue=[]}return e.prototype.set=function(e,t){this.queue[e]=t},e.prototype.has=function(e){return null!=this.queue[e]},e.prototype.setRow=function(e,t,r,n,o,i,a,u,s){this.queue[0]=e,this.queue[1]=t,this.queue[2]=r,this.queue[3]=n,this.queue[4]=o,this.queue[5]=i,this.queue[6]=a,this.queue[7]=u,this.queue[8]=s},e.prototype.get=function(e){return this.queue[e]},e.prototype.shuffleRow=function(){var e,t,r=this.queue.length
if(r)for(;--r;)t=Math.floor(Math.random()*(r+1)),e=this.queue[t].get(),this.queue[t].set(this.queue[r].get()),this.queue[r].set(e)
return this.queue},e.prototype.fillRandom=function(){for(var e=0;e<9;e++)this.queue[e].set(e+1),8===e&&(this.queue=this.shuffleRow())},e.prototype.valid=function(e){return this.queue.every((function(t){return t.get()!==e}))},e}(),i=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return n(t,e),t}(o),a=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return n(t,e),t}(o),u=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return n(t,e),t}(o),s=function(){function r(){}return r.prototype.generateTableArray=function(){for(var r=[],n=0;n<81;n++){var o=new e(0),i=new t(o)
r.push(i)}return r},r.prototype.copyTableArray=function(r){for(var n=[],o=0;o<81;o++){var i=new e(r.real[o].get()),a=new t(i)
n.push(a)}return n},r.prototype.clear=function(e){for(var t=0;t<81;t++)e[0].set(0)},r}()
function l(e){var t=e%9,r=Math.floor(e/9),n=Math.ceil((t+1)/3)-1,o=Math.ceil((r+1)/3)-1,i=t-3*n,a=r-3*o
return{x:t,y:r,boxX:n,boxY:o,innerX:i,innerY:a,boxArrayIndex:3*o+n,boxIndex:3*a+i,index:e}}function f(e,t){var r=Math.floor(e/3),n=e%3,o=Math.floor(t/3),i=t%3,a=3*n+i,u=3*r+o
return{x:a,y:u,boxX:n,boxY:r,innerX:i,innerY:o,boxArrayIndex:e,boxIndex:t,index:9*u+a}}function h(e,t){var r=e%9,n=Math.floor(e/9)
return{x:r,y:n,boxArrayIndex:t?r:n,boxIndex:t?n:r,index:e}}function c(e,t,r){return{x:r?e:t,y:r?t:e,boxArrayIndex:e,boxIndex:t,index:r?9*t+e:9*e+t}}function d(e,t){return c(e,t,!1)}function x(e,t){return c(e,t,!0)}function v(e){return h(e,!1)}function y(e){return h(e,!0)}var p=function(){function e(e){this.rows=[],this.columns=[],this.castes=[],this.real=[],this.generator=new s,void 0!==e&&void 0!==e.real?this.real=this.generator.copyTableArray(e):this.real=this.generator.generateTableArray(),this.generate()}return e.prototype.clear=function(){this.generator.clear(this.real)},e.prototype.generate=function(){for(var e=0;e<9;e++){this.rows[e]=new i
var t=9*e
this.rows[e].setRow(this.real[t],this.real[t+1],this.real[t+2],this.real[t+3],this.real[t+4],this.real[t+5],this.real[t+6],this.real[t+7],this.real[t+8])}for(var r=0;r<9;r++){this.columns[r]=new a
t=r
this.columns[r].setRow(this.real[t],this.real[t+9],this.real[t+18],this.real[t+27],this.real[t+36],this.real[t+45],this.real[t+54],this.real[t+63],this.real[t+72])}for(var n=0;n<9;n++)for(var o=0;o<9;o++){var s=l(t=9*n+o)
this.castes[s.boxArrayIndex]||(this.castes[s.boxArrayIndex]=new u),this.castes[s.boxArrayIndex].set(s.boxIndex,this.real[t])}},e}(),b=function(){function e(){}return e.full=function(e,t){for(var r=[],n=0;n<9;n++)for(var o=0;o<9;o++)e.rows[n].get(o).isEmpty()&&r.push(d(n,o))
return t(e,r)},e.SolveCheck=function(t,r){var n,o,i=e.SolveIteration(t,r)
try{for(var a=function(e){var t="function"==typeof Symbol&&e[Symbol.iterator],r=0
return t?t.call(e):{next:function(){return e&&r>=e.length&&(e=void 0),{value:e&&e[r++],done:!e}}}}(r),u=a.next();!u.done;u=a.next()){var s=u.value
t.real[s.index].set(0)}}catch(e){n={error:e}}finally{try{u&&!u.done&&(o=a.return)&&o.call(a)}finally{if(n)throw n.error}}return!i.double&&i.valid},e.SolveIteration=function(t,r){for(var n=r[0].index,o=v(n),i=y(n),a=l(n),u={valid:!1,double:!1},s=1,f=t.rows[o.boxArrayIndex].get(o.boxIndex).get();;){if(t.rows[o.boxArrayIndex].valid(s)&&t.columns[i.boxArrayIndex].valid(s)&&t.castes[a.boxArrayIndex].valid(s))if(u.valid){if((h=e.SolveIteration(t,r.slice(1))).double||h.valid)return u.double=!0,u}else{if(t.rows[o.boxArrayIndex].get(o.boxIndex).set(s),r.length>1){var h=e.SolveIteration(t,r.slice(1))
if(u.valid=h.valid,h.double)return u.double=!0,u}else u.valid=!0
t.rows[o.boxArrayIndex].get(o.boxIndex).set(f)}if(!(s<9))return u;++s}},e.Generation=function(t,r){for(var n=r[0].index,o=v(n),i=y(n),a=l(n),u=!1,s=Math.round(8*Math.random())+1,f=s,h=!0;!u;)if(t.rows[o.boxArrayIndex].valid(s)&&t.columns[i.boxArrayIndex].valid(s)&&t.castes[a.boxArrayIndex].valid(s)&&(t.rows[o.boxArrayIndex].get(o.boxIndex).set(s),u=!(r.length>1)||e.Generation(t,r.slice(1))),!u){if(s<9&&(s!==f||h))++s
else{if(s===f&&!h)return t.rows[o.boxArrayIndex].get(o.boxIndex).set(0),!1
s=1}h=!1}return u},e}(),I=function(){function e(){}return e.EraseRandomIteration=function(e){for(var t=0;t<81;t++){if(Math.round(1*Math.random())){var r=e.real[t].get()
e.real[t].set(0),b.full(e,b.SolveCheck)||e.real[t].set(r)}}},e.EraseEveryIteration=function(e){for(var t=0;t<81;t++){var r=e.real[t].get()
e.real[t].set(0),b.full(e,b.SolveCheck)||e.real[t].set(r)}},e.createHardFillable=function(e){return this.EraseRandomIteration(e),this.EraseEveryIteration(e),!0},e.createFillable=function(e,t,r,n){void 0===r&&(r=10)
for(var o=0,i=0,a=new Set;o<t;){for(var u=Math.round(80*Math.random()),s=Math.round(1*Math.random());a.has(u)&&a.size<81;)s?u<80?u++:u=0:u>0?u--:u=80
if(a.add(u),!e.real[u].isEmpty()){var l=e.real[u].get()
if(e.real[u].set(0),e.real[u].changeable=!0,b.full(e,b.SolveCheck))i=0,o++
else if(e.real[u].set(l),e.real[u].changeable=!1,++i===r)return!1}}return!0},e.HARDEST=81,e.MIDDLE=50,e.EASY=25,e.Mode={HARDEST:64,MIDDLE:50,EASY:35},e.Checkover={CLEAN:40,NORMAL:20,FAST:5},e}()
return function(){function r(){}return r.Value=e,r.Box=t,r.Row=i,r.Column=a,r.Caste=u,r.Perspective=o,r.Table=p,r.Backtrack=b,r.Erase=I,r.Util={indexToCasteData:l,casteToIndexData:f,indexToRowData:v,rowToIndexData:d,indexToColumnData:y,columnToIndexData:x},r}()}))