Copy Code code as follows:
<p><input value= "Get New SuDoKu" type= "button" onclick= "Onloadtable ()" id= "Refreshbutton"/></p>
<table border= "1" style= "border-color:red;" id= "maintable" >
<tbody>
</tbody>
</table>
<script type= "Text/javascript" language= "JavaScript" >//<! [cdata[
var sudokuarray = [[],[],[],[],[],[],[],[],[],[]];
function onloadtable ()
{
Todo
{
Sudokuarray = Getnewarray ();
Sudokuarray = Getsudokuarray (Sudokuarray);
}
while (!checkarray (Sudokuarray))
document.getElementById ("Result"). Innerhtml=checkarray (Sudokuarray);
var table = document.getElementById ("maintable");
Table.removechild (Table.firstchild);
for (Var i=0;i<9;i++)
{
Table.insertrow (i);
for (Var j=0;j<9;j++)
{
Table.rows[i].insertcell (j);
table.rows[i].cells[j].innerhtml = Sudokuarray[i][j];//math.round (Math.random () *8+1);
SetStyle (I,j,table.rows[i].cells[j]);
}
}
}
function Getsudokuarray (Filledarray)
{
var failed = false;
for (Var i=0;i<9;i++)
{
for (Var j=0;j<9;j++)
{
var temparray = Getavaildablearray (i, J, Filledarray);
if (Temparray.length > 0)
{
FILLEDARRAY[I][J] = Temparray[math.round (Math.random () * (temparray.length-1))];
}
Else
{
Window.location.reload ();
}
}
}
return filledarray;
}
function Checkarray (array)
{
for (Var i=0;i<9;i++)
{
for (Var j=0;j<9;j++)
{
if (array[i][j] = = undefined)
{
return false;
}
}
}
return true;
}
function Getavaildablearray (RowIndex, CellIndex, array)
{
var availdablearray = [1,2,3,4,5,6,7,8,9];
for (Var n=0;n<9;n++)
{
if (!isnan (Array[rowindex][n]) && array[rowindex][n]!= undefined)
{
Availdablearray[array[rowindex][n]-1] = NaN;
}
if (!isnan (Array[n][cellindex]) && Array[n][cellindex]!= undefined)
{
Availdablearray[array[n][cellindex]-1] = NaN;
}
}
var rowstartindex = Math.floor (ROWINDEX/3);
var cellstartindex = Math.floor (CELLINDEX/3);
for (Var x=rowstartindex*3;x<rowstartindex+3;x++)
{
for (Var y=cellstartindex*3;y<cellstartindex+3;y++)
{
if (!isnan (array[x][y)) | | | array[x][y]!= undefined)
{
Availdablearray[array[x][y]-1] = NaN;
}
}
}
var returnarray = [];
for (Var m=0;m<9;m++)
{
if (!isnan (Availdablearray[m]))
{
Returnarray.push (Availdablearray[m]);
}
}
return returnarray;
}
function Getnewarray ()
{
return [[],[],[],[],[],[],[],[],[],[]];
}
function SetStyle (Rowindex,cellindex,cell)
{
Cell.height= "20";
Cell.width= "20";
if (cellindex%3==2)
{
Cell.style.borderrightcolor= "Red";
}
if (cellindex%3==0)
{
Cell.style.borderleftcolor= "Red";
}
if (rowindex%3==2)
{
Cell.style.borderbottomcolor= "Red";
}
if (rowindex%3==0)
{
Cell.style.bordertopcolor= "Red";
}
}
]]></script>