Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.71 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script>
  4.  
  5. ///////////////////INITIAL DATA FOR DE-DIAGONALIZED GRID//////////////////
  6.  
  7. dgd=[
  8. [1,2,3,7,8,9,4,5,6],
  9. [7,8,9,4,5,6,1,2,3],
  10. [4,5,6,1,2,3,7,8,9],
  11. [6,1,2,3,7,8,9,4,5],
  12. [9,4,5,6,1,2,3,7,8],
  13. [3,7,8,9,4,5,6,1,2],
  14. [5,6,1,2,3,7,8,9,4],
  15. [8,9,4,5,6,1,2,3,7],
  16. [2,3,7,8,9,4,5,6,1]
  17. ];
  18.  
  19. //////////////////////////END OF DATA//////////////////////////////////////
  20.  
  21.  
  22.  
  23. /////////////////////DRAWS THE SUDOKU GRID/////////////////////////////////
  24.  
  25. function drawDiagoduko(arr) {
  26. for (i=0;i<9;i++) {
  27. for (j=0;j<9;j++) document.write(arr[i][j]+" ");
  28. document.write("<br>");
  29. }
  30. document.write("<br>");
  31. }
  32.  
  33. ////////////////////////END OF DRAW ROUTINE////////////////////////////////
  34.  
  35.  
  36.  
  37. ////////////////////CHECK FOR DIAGONALS BEING UNALIGNED////////////////////
  38.  
  39. function checkDiagonals(arr) {
  40. for (i=0;i<8;i++) {
  41. j=-1;
  42. while (j++<7) {
  43. if (arr[i][j]==arr[i+1][j+1]) {alert("Diagonal \\ at "+(j+1)+", "+(i+1));break;}
  44. if (arr[i][j+1]==arr[i+1][j]) {alert("Diagonal \/ at "+(j+2)+", "+(i+1));break;}
  45. }}}
  46.  
  47. function checkDiagonalsQ(arr) {
  48. retVal=true;
  49. for (i=0;i<8;i++) {
  50. j=-1;
  51. while (j++<7) {
  52. if (arr[i][j]==arr[i+1][j+1]) {retVal=false;break;}
  53. if (arr[i][j+1]==arr[i+1][j]) {retVal=false;break;}
  54. }}
  55. return retVal;
  56. }
  57.  
  58. //////////////////////END OF DIAGONAL CHECKS/////////////////////////////////
  59.  
  60.  
  61.  
  62.  
  63. /////////////////BASIC CHECKS FOR ROWS, COLUMNS AND BLOCKS///////////////////
  64.  
  65.  
  66. function checkSudoku(arr) {
  67. e=new Array();
  68. ec=0;
  69. checkHorz(arr);
  70. checkVert(arr);
  71. checkBlock(arr);
  72. if (ec>0) alert(e.join("")+ec);
  73. checkXoox(arr);
  74. }
  75.  
  76. function checkHorz(arr2) {
  77. a=new Array();
  78. for (i=0;i<9;i++) {
  79. for (j=0;j<9;j++) a[j]=arr2[i][j];
  80. b=a.sort(function(a,b) {return a-b;});
  81. for (j=0;j<8;j++) if (b[j]==b[j+1]) e[ec++]="Horizontal Row : "+(i+1)+"\n";
  82. }}
  83.  
  84. function checkVert(arr2) {
  85. a=new Array();
  86. for (i=0;i<9;i++) {
  87. for (j=0;j<9;j++) a[j]=arr2[j][i];
  88. b=a.sort(function(a,b) {return a-b;});
  89. for (j=0;j<8;j++) if (b[j]==b[j+1]) e[ec++]="Vertical Column : "+(i+1)+"\n";
  90. }}
  91.  
  92. function checkBlock(arr2) {
  93. a=new Array();
  94. for (i=0;i<3;i++)
  95. for (j=0;j<3;j++) {
  96. for (k=0;k<3;k++) for (l=0;l<3;l++) a[k*3+l]=arr2[i*3+k][j*3+l];
  97. b=a.sort(function(a,b) {return a-b;});
  98. for (d=0;d<8;d++) if (b[d]==b[d+1]) e[ec++]="Block : "+(i+1)+" , "+(j+1)+"\n";
  99. }}
  100.  
  101. function checkXoox(arr2) {
  102. x=new Array();
  103. xc=0;
  104. for (i=0;i<8;i++)
  105. for (j=0;j<8;j++)
  106. for (k=i+1;k<9;k++)
  107. for (m=j+1;m<9;m++)
  108. if (arr2[i][j]==arr2[k][m] && arr2[i][m]==arr2[k][j]) {
  109. x[xc++]="Xoox at "+(i+1)+", "+(j+1)+" with "+(k+1)+", "+(m+1)+"\n";}
  110. if (xc>0) alert(x.join("")+xc);
  111. }
  112.  
  113.  
  114. ///////////////////////////END BASIC CHECKS////////////////////////////////
  115.  
  116.  
  117.  
  118. //////////////////////BASIC SUDOKU PUZZLE MAKER////////////////////////////
  119.  
  120. function makeGame() {
  121. success=false;
  122. while (!success) {
  123. ds=new Array();
  124. for (i=0;i<9;i++) ds[i]=dgd[i];
  125.  
  126. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  127. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  128. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  129. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  130. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  131. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  132. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  133. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  134. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  135. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  136. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  137. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  138. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  139. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  140. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  141. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  142. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  143. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  144. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  145. ds=digitSwap(Math.floor(Math.random()*9)+1,Math.floor(Math.random()*9)+1,ds);
  146.  
  147. if (Math.random()<=0.5) ds=flipOverH(ds);
  148. if (Math.random()<=0.5) ds=flipOverV(ds);
  149.  
  150. if (Math.random()<=0.15) ds=rotate(ds);
  151. if (Math.random()<=0.25) ds=rotate(ds);
  152. if (Math.random()<=0.35) ds=rotate(ds);
  153. if (Math.random()<=0.45) ds=rotate(ds);
  154. if (Math.random()<=0.55) ds=rotate(ds);
  155. if (Math.random()<=0.65) ds=rotate(ds);
  156. if (Math.random()<=0.75) ds=rotate(ds);
  157. if (Math.random()<=0.85) ds=rotate(ds);
  158. if (Math.random()<=0.95) ds=rotate(ds);
  159.  
  160. if (Math.random()<=0.5) ds=flipOverH(ds);
  161. if (Math.random()<=0.5) ds=flipOverV(ds);
  162.  
  163. if (Math.random()<=0.5) ds=swapRow(ds,0,1);
  164. if (Math.random()<=0.5) ds=swapRow(ds,7,8);
  165. if (Math.random()<=0.5) ds=swapColumn(ds,0,1);
  166. if (Math.random()<=0.5) ds=swapColumn(ds,7,8);
  167.  
  168. success=checkDiagonalsQ(ds);
  169. }
  170.  
  171. drawDiagoduko(ds);
  172. checkDiagonals(ds);
  173. checkSudoku(ds);
  174. return ds;
  175. }
  176.  
  177. function selectCells(arr) {
  178. c=new Array();
  179. for (i=0;i<9;i++) c[i]=["_","_","_","_","_","_","_","_","_"];
  180.  
  181. size=40; // number of initials cell given
  182.  
  183. gc=0;
  184. while (gc<size) {
  185. p1=Math.floor(Math.random()*9);
  186. p2=Math.floor(Math.random()*9);
  187. if (c[p1][p2]=="_") {gc++;c[p1][p2]=ds[p1][p2];}
  188. }
  189. return c;
  190. }
  191.  
  192. //////////////////////END OF SUDOKU MAKER/////////////////////////////////
  193.  
  194.  
  195.  
  196. ////////////////////////SUDOKU GRID MANIPULATIONS/////////////////////////
  197.  
  198. function digitSwap(n1,n2,arr) {
  199. a=new Array();
  200. for (i=0;i<9;i++) a[i]=arr[i];
  201. for (i=0;i<9;i++) for (j=0;j<9;j++) if (a[i][j]==n1) a[i][j]=0;
  202. for (i=0;i<9;i++) for (j=0;j<9;j++) if (a[i][j]==n2) a[i][j]=n1;
  203. for (i=0;i<9;i++) for (j=0;j<9;j++) if (a[i][j]==0) a[i][j]=n2;
  204. return a;
  205. }
  206.  
  207. function flipOverH(arr) {
  208. arr.reverse();
  209. return arr;
  210. }
  211.  
  212. function flipOverV(arr) {
  213. for (i=0;i<9;i++) arr[i].reverse();
  214. return arr;
  215. }
  216.  
  217. function rotate(arr) {
  218. a=new Array();
  219. b=new Array();
  220. for (i=0;i<9;i++) a[i]=arr[i];
  221. for (i=0;i<9;i++) b[i]=[0,0,0,0,0,0,0,0,0];
  222. for (i=0;i<9;i++) for (j=0;j<9;j++) b[j][8-i]=a[i][j];
  223. arr=b;
  224. return arr;
  225. }
  226.  
  227. function swapRow(arr,r1,r2) {
  228. a=new Array();
  229. b=new Array();
  230. for (i=0;i<9;i++) a[i]=arr[r1][i];
  231. for (i=0;i<9;i++) b[i]=arr[r2][i];
  232. for (i=0;i<9;i++) arr[r1][i]=b[i];
  233. for (i=0;i<9;i++) arr[r2][i]=a[i];
  234. return arr;
  235. }
  236.  
  237. function swapColumn(arr,c1,c2) {
  238. a=new Array();
  239. b=new Array();
  240. for (i=0;i<9;i++) a[i]=arr[i][c1];
  241. for (i=0;i<9;i++) b[i]=arr[i][c2];
  242. for (i=0;i<9;i++) arr[i][c1]=b[i];
  243. for (i=0;i<9;i++) arr[i][c2]=a[i];
  244. return arr;
  245. }
  246.  
  247. ////////////////////END OF SUDOKU GRID MANIPULATIONS//////////////////////
  248.  
  249.  
  250. /////////////////////////MAIN CODE////////////////////////////////////////
  251.  
  252. gameCore=makeGame();
  253. game=selectCells(gameCore);
  254.  
  255. /////////////////////////END OF MAIN CODE////////////////////////////////
  256.  
  257.  
  258.  
  259. ////////////////////////FORMAT PUZZLE ROUTINES///////////////////////////
  260.  
  261. for (i=0;i<9;i++) {
  262. for (j=0;j<9;j++)
  263. document.write("<input type=\"text\" size=1 value="+(game[i][j]=="_"?" ":game[i][j])+">");
  264. document.write("<br>");
  265. }
  266. document.write("<br>");
  267.  
  268.  
  269. ////////////////////END OF FORMAT PUZZLE ROUTINES////////////////////////
  270.  
  271.  
  272.  
  273. ///////// //////////// /////////////// ///////////
  274.  
  275.  
  276.  
  277. ///////////////////////////SOLUTION ROUTINES//////////////////////////////
  278.  
  279.  
  280.  
  281. /////////////////////////COMPLETELY UN-SOLVED GRID///////////////////////
  282.  
  283. s=new Array();
  284. for (i=0;i<9;i++) {
  285. s[i]=new Array();
  286. for (j=0;j<9;j++) s[i][j]=[1,2,3,4,5,6,7,8,9];
  287. }
  288.  
  289. /////////////////////END OF COMPLETELY UN-SOLVED GRID////////////////////
  290.  
  291.  
  292.  
  293. ////////////////////////FILL IN WITH 'GAME' DATA/////////////////////////
  294.  
  295. for (i=0;i<9;i++)
  296. for (j=0;j<9;j++) if (game[i][j]!="_") s[i][j]=[game[i][j]];
  297.  
  298. ////////////////////END OF FILL IN WITH 'GAME' DATA//////////////////////
  299.  
  300.  
  301.  
  302. /////////////////////////////SOLUTION FUNCTIONS//////////////////////////
  303.  
  304. function checkLine(ln) {
  305. lc=0;
  306. for (i=0;i<9;i++) if (s[ln][i].length>1) {lc++;sp=i;}
  307. if (lc>1) return false;
  308. if (lc==0) return false;
  309. s[ln][sp]=gameCore[ln][sp];
  310. return true;
  311. }
  312.  
  313. function checkRow(rw) {
  314. lc=0;
  315. for (i=0;i<9;i++) if (s[i][rw].length>1) {lc++;sp=i;}
  316. if (lc>1) return false;
  317. if (lc==0) return false;
  318. s[sp][rw]=gameCore[sp][rw];
  319. return true;
  320. }
  321.  
  322. function checkGrid(gx,gy) {
  323. lc=0;
  324. 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;}
  325. if (lc>1) return false;
  326. if (lc==0) return false;
  327. s[gy*3+spj][gx*3+spi]=gameCore[gy*3+spj][gx*3+spi];
  328. return true;
  329. }
  330.  
  331. function zapCell(zx,zy) {
  332. x=new Array();
  333. xc=0;
  334. c=false;
  335. if (s[zy][zx].length>1) {
  336. for (i=0;i<9;i++) if (i!=zy && s[zy][i].length==1) {s[zy][zx][s[zy][i]-1]=0;c=true;}
  337. for (i=0;i<9;i++) if (i!=zx && s[i][zx].length==1) {s[zy][zx][s[i][zx]-1]=0;c=true;}
  338. gx=(zx-zx%3)/3;gy=(zy-zy%3)/3;
  339. for (i=0;i<3;i++) for (j=0;j<3;j++)
  340. 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;}
  341.  
  342. if (zx>0 && zy>0 && zx<8 && zy<8)
  343. for (i=-1;i<2;i+=2) for (j=-1;j<2;j+=2)
  344. if (s[zy+j][zx+i].length==1) {s[zy][zx][s[zy+j][zx+i]-1]=0;c=true;}
  345. }
  346.  
  347. zc=0;
  348. nd=0;
  349. for (i=0;i<9;i++) if (s[zy][zx][i]==0) zc++; else nd=i+1;
  350.  
  351. if (zc==8) {
  352. s[zy][zx]=[nd];
  353. x[xc++]="Found "+(zx+1)+", "+(zy+1)+" = "+nd;
  354. }
  355. //if (xc>0) alert(x.join(""));
  356. return c>0;
  357. }
  358.  
  359. ////////////////////////END OF SOLUTION FUNCTIONS////////////////////////
  360.  
  361. dc=true;
  362. while(dc==true) {
  363. dc=false;
  364. for (j=0;j<9;j++) if (checkLine(j)) dc=true;
  365. for (j=0;j<9;j++) if (checkRow(j)) dc=true;
  366. for (k=0;k<3;k++) for (l=0;l<3;l++) if (checkGrid(k,l)) dc=true;
  367. for (k=0;k<9;k++) for (l=0;l<9;l++) zapCell(k,l);
  368. }
  369.  
  370. drawDiagoduko(s);
  371.  
  372. for (i=0;i<9;i++) {
  373. for (j=0;j<9;j++)
  374. document.write("<input type=\"text\" size=1 value="+(s[i][j].length>1?" ":s[i][j])+">");
  375. document.write("<br>");
  376. }
  377. document.write("<br>");
  378.  
  379.  
  380. </script>
  381. </head>
  382. <body>
  383. <button onclick="location.reload();">New Puzzle</button>
  384. </body>
  385. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement