Array.prototype.shuffle=function(){for(var c,b,a=this.length;a;c=Math.floor(Math.random()*a),b=this[--a],this[a]=this[c],this[c]=b);};function isPossibleX(c,b,a){for(var e=0;9>e;e++)if(Math.abs(c[b][e])==a)return!1;return!0}function isPossibleY(c,b,a){for(var e=0;9>e;e++)if(Math.abs(c[e][b])==a)return!1;return!0}function isPossibleBlock(c,b,a,e){b=3>b?0:6>b?3:6;for(var d=a=3>a?0:6>a?3:6;d<a+3;d++)for(var f=b;f<b+3;f++)if(Math.abs(c[d][f])==e)return!1;return!0}function getNextPossibleNumber(c,b,a,e){for(;0<e.length;){var d=e.shift();if(isPossibleX(c,a,d)&&isPossibleY(c,b,d)&&isPossibleBlock(c,b,a,d))return d}return-1}function generateSolution(c,b){if(80<b)return c;for(var a=b%9,e=Math.floor(b/9),d=[],f=1;9>=f;f++)d.push(f);for(d.shuffle();0<d.length;){f=getNextPossibleNumber(c,a,e,d);if(-1==f)return null;c[e][a]=f;f=generateSolution(c,b+1);if(null!=f)return f;c[e][a]=0}}function getMaxBlacks(c){switch(c){case 0:return 36;case 1:return 48;case 2:return 60;default:return 60}}function generateGame(c,b,a){var e=getMaxBlacks(b);if("undefined"==typeof a){a=[];for(var d=[],f=0;27>f;f++)d.push(f);d.shuffle();a=a.concat(d.slice(0,Math.round(e/3)));d=[];for(f=27;54>f;f++)d.push(f);d.shuffle();a=a.concat(d.slice(0,Math.round(e/3)));d=[];for(f=54;81>f;f++)d.push(f);d.shuffle();a=a.concat(d.slice(0,Math.round(e/3)));return generateGame(c,b,a)}for(;0<a.length&&!(b=a.shift(),c[Math.floor(b/9)][b%9]=0,e--,0>=e););return c}function Sudoku(c){for(var b=Array(9),a=0;a<b.length;a++)b[a]=[0,0,0,0,0,0,0,0,0];for(var e=generateSolution(b,0),b=Array(9),a=0;a<e.length;a++){b[a]=Array(9);for(var d=0;d<e[a].length;d++)b[a][d]=e[a][d]}this.game=generateGame(b,c);this.difficultyLevel=c;this.record={seconds:0,errors:0};this.victory=!1}onmessage=function(c){for(var b=0;b<c.data.length;b++)postMessage(new Sudoku(c.data[b]))};