JS Sudoku Solver

Here it is, plugin a puzzle, hit try. If it doesn't work, hit Try again. If it doesn't work after a few times, hit possibilities, put that in the comments, and I'll see if I can sort it out. Happy sudoku-ruining

If you want, you can embed this as an iFrame
http://www.issackelly.com/sudoku.html, but please, backlink here.

And the Code...

Actually, you're probably best to view source, as the lines aren't properly broken here.

<script type="text/javascript" src="http://jquery.com/src/jquery-latest.pack.js"></script> <script type="text/javascript"> given = new Array(); possibilities = new Array(); var i,j,k; function doGo() { initThis(); for(var z=0;z<99;z++) { attempt(); possibilitiesToGivens(); //oneInBox(); possibilitiesToGivens(); givensClearPossibilities(); oneInRowCol(); possibilitiesToGivens(); } givenToBoard(); } function initThis() { for(i = 0; i < 9; i++) { possibilities[i] = new Array(); given[i] = new Array(); for(j = 0; j < 9; j++) { possibilities[i][j] = new Array(); for(k = 1; k < 10; k++) { possibilities[i][j][k] = true; } if(isNaN(parseInt($("#a"+i+'x'+j).val()))) { given[i][j] = 0; } else { given[i][j] = parseInt($("#a"+i+'x'+j).val()); for(var l=1; l<10; l++) possibilities[i][j][l] = false; } } } } function attempt() { //brute out impossible combinations for(i = 0; i < 9; i++) { for(j = 0; j < 9; j++) { if(given[i][j] > 0) { filterRowCol(i,j); filterBox(i,j); } } } } function oneInBox() { for(i=0;i<9;i++) { for(j=0;j<9;j++) { if(given[i][j] == 0) { if((i==0) || (i==1) || (i==2)) { iLow = 0; iHigh = 3; } else if((i==3) || (i==4) || (i==5)) { iLow = 3; iHigh = 6; } else { iLow = 6; iHigh = 9; } if((j==0) || (j==1) || (j==2)) { jLow = 0; jHigh = 3; } else if((j==3) || (j==4) || (j==5)) { jLow = 3; jHigh = 6; } else { jLow = 6; jHigh = 9; } var trues = 0,trueL=10,trueM=10,trueN=10; for(n=1;n<10;n++) { if(possibilities[i][j][n]) { for(var l=iLow;l<iHigh;l++) { for(var m=jLow;m<jHigh;m++) { if(possibilities[l][m][n]) { trues++; } if(trues == 1) { given[i][j] = n; givensClearPossibilities(); //filterRowCol(i,j); } } } } } } } } } function oneInRowCol() { for(i=0;i<9;i++) { for(j=0;j<9;j++) { if(given[i][j] == 0) //we haven't found it yet { for(n=1;n<10;n++) { trues = 0; truess = 0; if(possibilities[i][j][n]) { for(m=0;m<9;m++) //for this row { if(possibilities[i][m][n]) { trues++; } } if(trues == 1) { given[i][j] = n; } for(l=0;l<9;l++) //for this row { if(possibilities[l][j][n]) { truess++; } } if(truess == 1) { given[i][j] = n; } } } } } } } function givensClearPossibilities() { for(i=0;i<9;i++) { for(j=0;j<9;j++) { if(given[i][j] > 0) { for(k=1;k<10;k++) { possibilities[i][j][k] == false; } } } } } function filterRowCol(i,j) { val = given[i][j]; for(var l=0; l<9; l++) { possibilities[i][l][val] = false; possibilities[l][j][val] = false; } } function filterBox(i,j) { var iLow,jLoq,iHigh,jHigh; //boxNum = findBox(i,j); val = given[i][j]; if((i==0) || (i==1) || (i==2)) { iLow = 0; iHigh = 3; } else if((i==3) || (i==4) || (i==5)) { iLow = 3; iHigh = 6; } else { iLow = 6; iHigh = 9; } if((j==0) || (j==1) || (j==2)) { jLow = 0; jHigh = 3; } else if((j==3) || (j==4) || (j==5)) { jLow = 3; jHigh = 6; } else { jLow = 6; jHigh = 9; } for(var l=iLow;l<iHigh;l++) { for(var m=jLow;m<jHigh;m++) { possibilities[l][m][val] = false; } } } function possibilitiesToGivens() //turns one possibility into a given. { var l,m,n,trues=0; for(l=0;l<9;l++) { for(m=0;m<9;m++) { if(given[l][m] == 0) { for(n=1;n<10;n++) { if(possibilities[l][m][n]) trues++; } if(trues == 1) { for(n=1;n<10;n++) { if(possibilities[l][m][n]) given[l][m] = n; } } } trues = 0; } } } function givenToBoard() { for(i=0;i<9;i++) { for(j=0;j<9;j++) { if(given[i][j] > 0) $("#a"+i+'x'+j).val(given[i][j]); } } } function displayPossibilities() { var l,m,n,trues =''; for(l=0;l<9;l++) { for(m=0;m<9;m++) { if(given[l][m] == 0) { trues += l+ 'x'+m+' - '; for(n=1;n<10;n++) { if(possibilities[l][m][n] == true) trues += n+','; } } else { trues += l+'x'+m+' - '+given[l][m]; } trues += " : "; } trues+= '<br/>'; } $("#possibilities").html(trues); } </script> <table> <tr><td><input style="width:26px" style="width:26px" id="a0x0" /></td><td><input style="width:26px" id="a0x1" /></td><td><input style="width:26px" id="a0x2" /></td><td rowspan="11" style="border-width:1px; border-color:black;"></td><td><input style="width:26px" id="a0x3" /></td><td><input style="width:26px" id="a0x4" /></td><td><input style="width:26px" id="a0x5" /></td><td rowspan="11" style="border-width:1px; border-color:black;"></td><td><input style="width:26px" id="a0x6" /></td><td><input style="width:26px" id="a0x7" /></td><td><input style="width:26px" id="a0x8" /></td></tr> <tr><td><input style="width:26px" id="a1x0" /></td><td><input style="width:26px" id="a1x1" /></td><td><input style="width:26px" id="a1x2" /></td><td><input style="width:26px" id="a1x3" /></td><td><input style="width:26px" id="a1x4" /></td><td><input style="width:26px" id="a1x5" /></td><td><input style="width:26px" id="a1x6" /></td><td><input style="width:26px" id="a1x7" /></td><td><input style="width:26px" id="a1x8" /></td></tr> <tr><td><input style="width:26px" id="a2x0" /></td><td><input style="width:26px" id="a2x1" /></td><td><input style="width:26px" id="a2x2" /></td><td><input style="width:26px" id="a2x3" /></td><td><input style="width:26px" id="a2x4" /></td><td><input style="width:26px" id="a2x5" /></td><td><input style="width:26px" id="a2x6" /></td><td><input style="width:26px" id="a2x7" /></td><td><input style="width:26px" id="a2x8" /></td></tr> <tr><td colspan="11" style="border-width:1px; border-color:black;"></td></tr> <tr><td><input style="width:26px" id="a3x0" /></td><td><input style="width:26px" id="a3x1" /></td><td><input style="width:26px" id="a3x2" /></td><td><input style="width:26px" id="a3x3" /></td><td><input style="width:26px" id="a3x4" /></td><td><input style="width:26px" id="a3x5" /></td><td><input style="width:26px" id="a3x6" /></td><td><input style="width:26px" id="a3x7" /></td><td><input style="width:26px" id="a3x8" /></td></tr> <tr><td><input style="width:26px" id="a4x0" /></td><td><input style="width:26px" id="a4x1" /></td><td><input style="width:26px" id="a4x2" /></td><td><input style="width:26px" id="a4x3" /></td><td><input style="width:26px" id="a4x4" /></td><td><input style="width:26px" id="a4x5" /></td><td><input style="width:26px" id="a4x6" /></td><td><input style="width:26px" id="a4x7" /></td><td><input style="width:26px" id="a4x8" /></td></tr> <tr><td><input style="width:26px" id="a5x0" /></td><td><input style="width:26px" id="a5x1" /></td><td><input style="width:26px" id="a5x2" /></td><td><input style="width:26px" id="a5x3" /></td><td><input style="width:26px" id="a5x4" /></td><td><input style="width:26px" id="a5x5" /></td><td><input style="width:26px" id="a5x6" /></td><td><input style="width:26px" id="a5x7" /></td><td><input style="width:26px" id="a5x8" /></td></tr> <tr><td colspan="11" style="border-width:1px; border-color:black;"></td></tr> <tr><td><input style="width:26px" id="a6x0" /></td><td><input style="width:26px" id="a6x1" /></td><td><input style="width:26px" id="a6x2" /></td><td><input style="width:26px" id="a6x3" /></td><td><input style="width:26px" id="a6x4" /></td><td><input style="width:26px" id="a6x5" /></td><td><input style="width:26px" id="a6x6" /></td><td><input style="width:26px" id="a6x7" /></td><td><input style="width:26px" id="a6x8" /></td></tr> <tr><td><input style="width:26px" id="a7x0" /></td><td><input style="width:26px" id="a7x1" /></td><td><input style="width:26px" id="a7x2" /></td><td><input style="width:26px" id="a7x3" /></td><td><input style="width:26px" id="a7x4" /></td><td><input style="width:26px" id="a7x5" /></td><td><input style="width:26px" id="a7x6" /></td><td><input style="width:26px" id="a7x7" /></td><td><input style="width:26px" id="a7x8" /></td></tr> <tr><td><input style="width:26px" id="a8x0" /></td><td><input style="width:26px" id="a8x1" /></td><td><input style="width:26px" id="a8x2" /></td><td><input style="width:26px" id="a8x3" /></td><td><input style="width:26px" id="a8x4" /></td><td><input style="width:26px" id="a8x5" /></td><td><input style="width:26px" id="a8x6" /></td><td><input style="width:26px" id="a8x7" /></td><td><input style="width:26px" id="a8x8" /></td></tr> </table> <button id="doGo" onclick="doGo()">Try!</button><button id="showPossibles" onclick="displayPossibilities()">Show Possibilities</button><button id="clear" onclick="$(\"input\").val('')">Clear</button> <div id="possibilities">&nbsp;</div>


Comments and Messages

I won't ever give out your email address. I don't publish comments but if you'd like to write to me then you could use this form.

Issac Kelly