JavaScript Sudoku Sudoku Puzzle generation code _javascript Tips

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.