javascript sudoku 生成
<html> <body> <input type="text" style="border-style: none" /> <input type="button" id="refreshButton" value="Get New SuDoKu" onclick="onLoadTable()" /> <table style="border-color: Red" border="1px" id="mainTable"> </table> <script language="javascript" type="text/javascript"> var sudokuArray = [[],[],[],[],[],[],[],[],[],[]]; function onLoadTable() { do { sudokuArray = getNewArray(); sudokuArray = getSudokuArray(sudokuArray); } while(!checkArray(sudokuArray)) document.getElementById("result").innerHTML=checkArray(sudokuArray); sudokuArray = setGameArray(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 = setNumberInTable(sudokuArray[i][j]);//Math.round(Math.random()*8+1); setStyle(i,j,table.rows[i].cells[j]); } } } function setNumberInTable(num) { if(isNaN(num)) { return "<input type='text' value='' maxlength='1' size='1'/>"; } else { return "<input type='text' value='"+num+"' style='background-color:red' maxlength='1' size='1'/>"; } } 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))]; } } } return filledArray; } function checkArray(array) { for(var i=0;i<9;i++) { for(var j=0;j<9;j++) { if(array[i][j] == undefined||isNaN(array[i][j])) { 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 && n!=cellIndex) { availdableArray[array[rowIndex][n]-1] = NaN; } if(!isNaN(array[n][cellIndex]) && array[n][cellIndex] != undefined && n!=rowIndex) { availdableArray[array[n][cellIndex]-1] = NaN; } } var rowStartIndex = Math.floor(rowIndex/3)*3; var cellStartIndex = Math.floor(cellIndex/3)*3; for(var x=rowStartIndex;x<rowStartIndex+3;x++) { for(var y=cellStartIndex;y<cellStartIndex+3;y++) { //alert(x.toString()+" "+ y.toString()+ " " +array[x][y]); if((!isNaN(array[x][y])||array[x][y] != undefined) && !(x==rowIndex&&y==cellIndex)) { 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 setGameArray(array) { for(var i=0;i<9;i++) { for(var j=0;j<9;j++) { if(getAvaildableArray(i,j,array).length==1) { array[i][j] = NaN; } } } return array; } 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> <span id="result">Hello</span> </body> </html>
Hello