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


Posted in Javascript onOctober 29, 2008
<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>
Javascript 相关文章推荐
JavaScript回调(callback)函数概念自我理解及示例
Jul 04 Javascript
js实现按一下删除键删除整个单词附demo
Sep 05 Javascript
jquery $(document).ready()和window.onload的区别浅析
Feb 04 Javascript
JS获取IE版本号与HTML设置IE文档模式的方法
Oct 09 Javascript
Bootstrap CSS组件之分页(pagination)和翻页(pager)
Dec 17 Javascript
BootStrap table删除指定行的注意事项(笔记整理)
Feb 05 Javascript
几行js代码实现自适应
Feb 24 Javascript
JS二叉树的简单实现方法示例
Apr 05 Javascript
vue用addRoutes实现动态路由的示例
Sep 15 Javascript
优雅的将ElementUI表格变身成树形表格的方法步骤
Apr 11 Javascript
uni-app 组件里面获取元素宽高的实现
Dec 27 Javascript
vue Treeselect 树形下拉框:获取选中节点的ids和lables操作
Aug 15 Javascript
JavaScript函数、方法、对象代码
Oct 29 #Javascript
js身份证验证超强脚本
Oct 26 #Javascript
javascript引导程序
Oct 26 #Javascript
javascript笔试题目附答案@20081025_jb51.net
Oct 26 #Javascript
Div自动滚动到末尾的代码
Oct 26 #Javascript
Firefox getBoxObjectFor getBoundingClientRect联系
Oct 26 #Javascript
js判断变量是否空值的代码
Oct 26 #Javascript
You might like
php图片处理函数获取类型及扩展名实例
2014/11/19 PHP
tagName的使用,留一笔
2006/06/26 Javascript
javascript原始值和对象引用实例分析
2015/04/25 Javascript
nodejs创建web服务器之hello world程序
2015/08/20 NodeJs
Javascript实现快速排序(Quicksort)的算法详解
2015/09/06 Javascript
Javascript的表单验证-提交表单
2016/03/18 Javascript
JavaScript中的Object对象学习教程
2016/05/20 Javascript
node.js学习之base64编码解码
2016/10/21 Javascript
JavaScript严格模式详解
2017/01/16 Javascript
Angular1.x自定义指令实例详解
2017/03/01 Javascript
JS中SetTimeout和SetInterval使用初探
2017/03/23 Javascript
微信小程序 es6-promise.js封装请求与处理异步进程
2017/06/12 Javascript
JS中offset和匀速动画详解
2018/02/06 Javascript
vue 组件 全局注册和局部注册的实现
2018/02/28 Javascript
JavaScript实现随机点名程序
2020/03/25 Javascript
浅谈React中组件逻辑复用的那些事儿
2020/05/21 Javascript
JavaScript大数相加相乘的实现方法实例
2020/10/18 Javascript
[51:36]EG vs VP 2018国际邀请赛淘汰赛BO3 第一场 8.24
2018/08/25 DOTA
Django自定义插件实现网站登录验证码功能
2017/04/19 Python
python机器学习之神经网络实现
2018/10/13 Python
python for循环输入一个矩阵的实例
2018/11/14 Python
python实现公司年会抽奖程序
2019/01/22 Python
django框架实现模板中获取request 的各种信息示例
2019/07/01 Python
python通过安装itchat包实现微信自动回复收到的春节祝福
2020/01/19 Python
css3 实现滚动条美化效果的实例代码
2021/01/06 HTML / CSS
HTML5网页音乐播放器的示例代码
2017/11/09 HTML / CSS
介绍一下Linux中的链接
2016/06/05 面试题
党员2014两会学习心得体会
2014/03/17 职场文书
知识竞赛主持词
2014/03/26 职场文书
大学竞选班干部演讲稿
2014/08/21 职场文书
超市创业计划书
2014/09/15 职场文书
党员批评与自我批评发言材料
2014/10/14 职场文书
入队仪式主持词
2015/07/04 职场文书
初中语文教学随笔
2015/08/15 职场文书
学校标语口号大全
2015/12/26 职场文书
党风廉洁教育心得体会
2016/01/20 职场文书