时间:2021-05-26
复制代码 代码如下:
<html>
<head>
<title>拼图</title>
<style>
td.numTd{
width : 20px ;
height : 20px ;
}
div.numDiv{
width : 100% ;
height : 100% ;
background-color : #000 ;
color : #FFF ;
text-align : center ;
vertical-align : middle ;
}
</style>
<script>
var currPos = 9;
function move(event){
switch(event.keyCode){
case 37 :
// 左键
if (currPos % 3 != 0){
var currTd = document.getElementById("numTd_" + currPos);
var nextTd = document.getElementById("numTd_" + (currPos + 1));
var temp = nextTd.innerHTML;
nextTd.innerHTML = " ";
currTd.innerHTML = temp;
currPos ++ ;
}
break;
case 38 :
// 上键
if (currPos < 7){
var currTd = document.getElementById("numTd_" + currPos);
var nextTd = document.getElementById("numTd_" + (currPos + 3));
var temp = nextTd.innerHTML;
nextTd.innerHTML = " ";
currTd.innerHTML = temp;
currPos += 3;
}
break;
case 39 :
// 右键
if (currPos % 3 != 1){
var currTd = document.getElementById("numTd_" + currPos);
var nextTd = document.getElementById("numTd_" + (currPos - 1));
var temp = nextTd.innerHTML;
nextTd.innerHTML = " ";
currTd.innerHTML = temp;
currPos -- ;
}
break;
case 40 :
// 下键
if (currPos > 3){
var currTd = document.getElementById("numTd_" + currPos);
var nextTd = document.getElementById("numTd_" + (currPos - 3));
var temp = nextTd.innerHTML;
nextTd.innerHTML = " ";
currTd.innerHTML = temp;
currPos -= 3;
}
break;
default :
break;
}
if (isWin()){
alert("恭喜你,过关了!");
initNums();
}
}
function isWin(){
for (i = 1; i < 9; i ++ ){
var numTd = document.getElementById("numTd_" + i);
var numDiv = numTd.getElementsByTagName("div");
if (i != numTd.innerText){
return false;
}
}
return true;
}
function initNums(){
var numArr = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9);
var newnewNumArr = new Array();
do{
var tempStr = "";
for(i in numArr){
var flag = true;
do{
tempNum = numArr[parseInt(Math.random() * 100) % 9];
if (tempStr.search(tempNum) == -1){
newNumArr[i] = tempNum;
tempStr += tempNum;
flag = false;
}
}
while(flag);
}
}while(inverNum(newNumArr) % 2 == 0);
var len = newNumArr.length;
for(j = 0; j < len; j ++ ){
if (newNumArr[j] != 9){
document.getElementById("numTd_" + (j + 1)).innerHTML = "<div id='numDiv_"+newNumArr[j]+"' class='numDiv'>" + newNumArr[j] + "</div>";
}
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 < len - 1; i ++ ){
for(j = i + 1; j < len; j ++ ){
if (numArr[j] > numArr[i]){
count ++ ;
}
}
}
// alert("逆序数: "+count);
return count;
}
</script>
</head>
<body onkeyup="move(event);" onload="initNums();">
<table align="center">
<tr>
<td id="numTd_1" class="numTd">
</td>
<td id="numTd_2" class="numTd">
</td>
<td id="numTd_3" class="numTd">
</td>
</tr>
<tr>
<td id="numTd_4" class="numTd">
</td>
<td id="numTd_5" class="numTd">
</td>
<td id="numTd_6" class="numTd">
</td>
</tr>
<tr>
<td id="numTd_7" class="numTd">
</td>
<td id="numTd_8" class="numTd">
</td>
<td id="numTd_9" class="numTd">
</td>
</tr>
</table>
<table>
<tr>
<td id="output"></td>
</tr>
</table>
</body>
</html>
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了js实现拼图小游戏的具体代码,供大家参考,具体内容如下一、js拼图是什么?用js做得小游戏二、使用步骤1、先创建div盒子div,body{
本文实例讲述了java实现的简单猜数字游戏代码。分享给大家供大家参考。以下是Java语言写的一个猜数字小游戏引用片段:importjava.text.Simpl
2008年的时候,在学习Javahowtoprogram第五版的时候,写过一个猜数字小游戏,是用Applet写的;现在,我要用Swing重写这个小游戏,同时,加
小熊维尼拼图jQuery代码实现拼图小游戏,鼠标选中拼块,用上下左右键移动拼块。html代码\(╯-╰)/哎呦,走不通啦!.row{display:-webki
本文为大家分享了使用C语言编写一个简单的拼图游戏,供大家参考,具体内容如下制作一款拼图小游戏#include//引用图形库头文件#include#include