2015-11-21 39 views
-1

我需要擴大這個代碼,使其所以我有9×9板,當我把一些nunbers它,然後按它完成與行和列自動完成數字

<meta charset="utf-8"> 
<style> 
    input { font-size: 20px; width: 30px; text-align: center; } 
</style> 
<h1 id="a"></h1> 
<button type="button" id="b" onClick="uzup()">uzupe�nij</button> 
唯一編號的9x9的按鈕
for(i=1; i<6; i++) { 
    document.getElementById('a').innerHTML += '<input id="a' + i + '" maxlength="1" pattern="^[1-5]$">'; 
} 
function uzup() { 
    for(i=1;i<6;i++){ 
    w = document.getElementById('a'+i).value; 
    if(w == '') { 
     w = Number(w); 
     for(j=1;j<6;j++){ 
     jest = false; 
     for(k=1;k<6;k++){ 
      w = document.getElementById('a'+k).value; 
      if(w != ''){ 
      if(Number(w) == j) jest = true; 
      } 
     } 
     if(!jest) { 
      document.getElementById('a'+i).value = j; break; 
     } 
     } 
    } 
    } 
} 

回答

0

我不知道我是否正確。有這樣的輸入

1 3 _ 
4 _ 5 
5 _ 2 

你想得到像這樣的結果嗎?

1 3 4 
4 1 5 
5 4 2 

是正確的嗎?

我試圖用最明顯和清晰的方式來做到這一點(真正的代碼會在大約10%的代碼行中完成,但會有一個精心設計的Perl腳本的可讀性;-),我希望你能理解流程沒有廣泛的評論。

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=utf-8"> 
    <title>Nine By Nine</title> 
    <script type="text/javascript"> 

    function clearTable(){ 
     for(var i = 0; i<9; i++){ 
     document.getElementById("c"+i).style.border = "1px solid grey"; 
     document.getElementById("i"+i).value = ""; 
     } 
    } 

    // Some checks & balances omitted! 
    function fillUp(){ 
     var m = [],d,c,r; 

     var gotoError = function(idx){ 
     document.getElementById("c"+idx).style.border = "1px solid red"; 
     }; 

     var checkField = function(n, row, col){ 
      for(var i=0;i<3;i++){ 
       if(i !== col && m[row * 3 + i] === n) 
       return false; 
      } 
      for(var i=0;i<3;i++){ 
       if(i !== row && m[col + 3 * i] === n) 
       return false; 
      } 
      return true; 
     }; 

     var resetTable = function(){ 
     for(var i = 0; i<9; i++){ 
      m[i] = -1; 
      document.getElementById("c"+i).style.border = "1px solid grey"; 
     } 
     }; 

     resetTable();   

     for(var i = 0; i<9; i++){ 
     var val = document.getElementById("i"+i).value; 
     if(val){ 
      d = parseInt(val); 
      if(isNaN(d)){ 
      gotoError(i); 
      return; 
      } 
      c = i%3; 
      r = Math.floor(i/3); 
      if(!checkField(d,r,c)){ 
      gotoError(i); 
      return; 
      } 
      m[i] = d; 
     } 
     } 
     for(var i=0;i<9;i++){ 
     if(m[i] === -1){ 
      for(var j = 1;j<6;j++){ 
      c = i%3; 
      r = Math.floor(i/3); 
      if(checkField(j,r,c)){ 
       document.getElementById("i"+i).value = j; 
       m[i] = j 
       break; 
      } 
      } 
     } 
     } 
    } 
    window.addEventListener('submit', function(ev) { 
     ev.preventDefault(); 
     fillUp(); 
    }, false) 
    </script> 
    <style type="text/css"> 
    table { 
      padding: 1em 1em 1em 1em; 
      background-color: lightgreen; 
      } 
    td { 
     border: 1px solid grey; 
     padding: 5px 5px 5px 5px; 
     margin: 5px 5px 5px 5px; 
     background-color: lighgrey; 
     } 
    input { 
      width: 1.1em; 
      height: 1.1em; 
      text-align:center; 
      } 
    </style> 
</head> 
<body> 
    <form id="form0"> 
    <table id="table0"> 
    <tr id="r0"> 
     <td id="c0"><input id="i0" maxlength="1" pattern="^[1-5]$"></td> 
     <td id="c1"><input id="i1" maxlength="1" pattern="^[1-5]$"></td> 
     <td id="c2"><input id="i2" maxlength="1" pattern="^[1-5]$"></td> 
    </tr> 
    <tr id="r1"> 
     <td id="c3"><input id="i3" maxlength="1" pattern="^[1-5]$"></td> 
     <td id="c4"><input id="i4" maxlength="1" pattern="^[1-5]$"></td> 
     <td id="c5"><input id="i5" maxlength="1" pattern="^[1-5]$"></td> 
    </tr> 
    <tr id="r2"> 
     <td id="c6"><input id="i6" maxlength="1" pattern="^[1-5]$"></td> 
     <td id="c7"><input id="i7" maxlength="1" pattern="^[1-5]$"></td> 
     <td id="c8"><input id="i8" maxlength="1" pattern="^[1-5]$"></td> 
    </tr> 
    </table> 
    <button id="bt0" type="button" onclick="fillUp()">Fill Up</button> 
    <button id="bt1" type="button" onclick="clearTable()">Clear Table</button> 
    <form> 
</body> 
</html>