Heim >Web-Frontend >js-Tutorial >JS写的数字拼图小游戏代码[学习参考]_javascript技巧

JS写的数字拼图小游戏代码[学习参考]_javascript技巧

WBOY
WBOYOriginal
2016-05-16 18:59:251418Durchsuche
复制代码 代码如下:





拼图


<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] + "</script>
";
}
else{
document.getElementById("numTd_" + (j + 1)).innerHTML = " ";
currPos = j + 1;
}
}

// document.getElementById("output").innerText = newNumArr;
}

function inverNum(numArr){
var len = numArr.length;
var count = 0;
for(i = 0; i for(j = i + 1; j if (numArr[j] > numArr[i]){
count ++ ;
}
}
}
// alert("逆序数: "+count);
return count;
}







































Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn