拼图 <script> <br>var currPos = 9; <br>function move(event){ <br>switch(event.keyCode){ <br>case 37 : <br>// 左键 <br>if (currPos % 3 != 0){ <br>var currTd = document.getElementById("numTd_" currPos); <br>var nextTd = document.getElementById("numTd_" (currPos 1)); <br>var temp = nextTd.innerHTML; <br>nextTd.innerHTML = " "; <br>currTd.innerHTML = temp; <br>currPos ; <br>} <br>break; <br>case 38 : <br>// 上键 <br>if (currPos < 7){ <BR>var currTd = document.getElementById("numTd_" currPos); <BR>var nextTd = document.getElementById("numTd_" (currPos 3)); <BR>var temp = nextTd.innerHTML; <BR>nextTd.innerHTML = " "; <BR>currTd.innerHTML = temp; <BR>currPos = 3; <BR>} <BR>break; <BR>case 39 : <BR>// 右键 <BR>if (currPos % 3 != 1){ <BR>var currTd = document.getElementById("numTd_" currPos); <BR>var nextTd = document.getElementById("numTd_" (currPos - 1)); <BR>var temp = nextTd.innerHTML; <BR>nextTd.innerHTML = " "; <BR>currTd.innerHTML = temp; <BR>currPos -- ; <BR>} <BR>break; <BR>case 40 : <BR>// 下键 <BR>if (currPos > 3){ <br>var currTd = document.getElementById("numTd_" currPos); <br>var nextTd = document.getElementById("numTd_" (currPos - 3)); <br>var temp = nextTd.innerHTML; <br>nextTd.innerHTML = " "; <br>currTd.innerHTML = temp; <br>currPos -= 3; <br>} <br>break; <br>default : <br>break; <br>} <br><br>if (isWin()){ <br>alert("恭喜你,过关了!"); <br>initNums(); <br>} <br><br>} <br><br>function isWin(){ <br>for (i = 1; i < 9; i ){ <BR>var numTd = document.getElementById("numTd_" i); <BR>var numDiv = numTd.getElementsByTagName("div"); <BR>if (i != numTd.innerText){ <BR>return false; <BR>} <BR>} <BR>return true; <br><br>} <br><br>function initNums(){ <BR>var numArr = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9); <BR>var newnewNumArr = new Array(); <br><br>do{ <BR>var tempStr = ""; <BR>for(i in numArr){ <BR>var flag = true; <BR>do{ <BR>tempNum = numArr[parseInt(Math.random() * 100) % 9]; <BR>if (tempStr.search(tempNum) == -1){ <BR>newNumArr[i] = tempNum; <BR>tempStr = tempNum; <BR>flag = false; <BR>} <BR>} <BR>while(flag); <BR>} <BR>}while(inverNum(newNumArr) % 2 == 0); <br><br><BR>var len = newNumArr.length; <BR>for(j = 0; j < len; j ){ <BR>if (newNumArr[j] != 9){ <BR>document.getElementById("numTd_" (j 1)).innerHTML = "<div id='numDiv_" newNumArr[j] "' class='numDiv'>" newNumArr[j] "</div>"; <br>} <br>else{ <br>document.getElementById("numTd_" (j 1)).innerHTML = " "; <br>currPos = j 1; <br>} <br>} <br><br>// document.getElementById("output").innerText = newNumArr; <br>} <br><br>function inverNum(numArr){ <br>var len = numArr.length; <br>var count = 0; <br>for(i = 0; i < len - 1; i ){ <BR>for(j = i 1; j < len; j ){ <BR>if (numArr[j] > numArr[i]){ <br>count ; <br>} <br>} <br>} <br>// alert("逆序数: " count); <br>return count; <br>} <br></script>