javascript sudoku 数独智力游戏生成代码
作者:bea
代码如下: <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> &
代码如下:
<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()
{
do
{
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>
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- Tinymce+jQuery.Validation使用产生的BUG
- javascript delete 使用示例代码
- JavaScript 格式字符串的应用
- js DataSet数据源处理代码
- javascript 变态的节点集合
- javascript 节点遍历函数
- javascript 类型判断代码分析
- js chrome浏览器判断代码
- javascript 跨浏览器的事件系统
- javascript 深拷贝
- JavaScript 学习笔记二 字符串拼接
- jQuery 美元符冲突的解决方法
- 分享十五个最佳jQuery 幻灯插件和教程
- Jquery 1.42 checkbox 全选和反选代码
- 网络图片延迟加载实现代码 超越jquery控件
- JQuery的一些小应用收集
- Visual Studio中的jQuery智能提示设置方法
- Jquery 滑入滑出效果实现代码
- Jquery ajax不能解析json对象,报Invalid JSON错误的原因和解决方法