【散分】花了一晚上写了个JavaScript小游戏

wujinjian2008n 2009-12-02 11:39:41
在 ie7,火狐,谷歌 都测试通过。
源码:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>wujinjian</title>
<script type="text/javascript">
//11 个单元格,每个单元格的大小就 等于 地图的大小(mapWH)/mapSize
var mapSize=11;
//地图的大小
var mapWH=440;
//记录对方的ID
var computerID;
//这个方向是否可走
var isPath=true;
//记录四方位上距离对方的距离
var up=0;
var left=0;
var right=0;
var down=0;
//障碍物的最多个数(可重叠)
var za=3;

window.onerror=function()
{
alert("异常!点击确定重新开始");
window.location.href=window.location.href;
};

function createMap()
{
var x=Math.round(Math.random()*(mapSize-1)); //行
var y=Math.round(Math.random()*(mapSize-1)); //列

if(x==0)
x=x+1;
else if(x==(mapSize-1))
x=x-1;
if(y==0)
y=y+1;
else if(y==(mapSize-1))
y=y-1;

//var x=7;
//var y=2;

computerID=x+"_"+y;

var tabobj=document.createElement("table");
tabobj.style.width=mapWH+"px";
tabobj.style.height=mapWH+"px";

tabobj.border="1";

var tbodyobj=document.createElement("tbody");

for(var i=0;i<mapSize;i++)
{
var trobj=document.createElement("tr");

for(var j=0;j<mapSize;j++)
{
var tdobj=document.createElement("td");
tdobj.style.border="rgb(128,128,255) solid 1px";
tdobj.id=i+"_"+j
tdobj.onclick=tdClick;

if(i+"_"+j==computerID)
{
tdobj.style.backgroundColor="red";
}

var txt=document.createTextNode(" ");
tdobj.appendChild(txt);

trobj.appendChild(tdobj);
}

tbodyobj.appendChild(trobj);
}

tabobj.appendChild(tbodyobj);

document.getElementById("map_div").appendChild(tabobj);

//默认随机障碍物
for(var i=0;i<za;i++)
{
var _id=Math.round(Math.random()*(mapSize-1)) +"_"+ Math.round(Math.random()*(mapSize-1));
if(document.getElementById(_id).style.backgroundColor=="")
document.getElementById(_id).style.backgroundColor="gray";
}

for(var i=0;i<mapSize;i++)
{
document.getElementById(i+"_"+(mapSize-1)).style.border="rgb(223,223,223) solid 1px";
document.getElementById((mapSize-1)+"_"+i).style.border="rgb(223,223,223) solid 1px";
document.getElementById(i+"_0").style.border="rgb(223,223,223) solid 1px";
document.getElementById("0_"+i).style.border="rgb(223,223,223) solid 1px";
}
}

function tdClick()
{
if(this.style.backgroundColor=="")
{
this.style.backgroundColor="gray";

up=0;
left=0;
right=0;
down=0;

computerXZ();
}
}

function computerXZ()
{
var xy=computerID.split("_");
var x=xy[0]-0;
var y=xy[1]-0;

//中心位置
var mid=(mapSize-1)/2;

//左上角
if(x<=mid && y<=mid)
{
//向上
if(x<=y)
{
//向上不通,向左走 //false 表示是判断,true 表示行走
if(!computerUp(x,y,false))
{
//向左不通,向右走
if(!computerLeft(x,y,false))
{
//向右不通,向下走
if(!computerRight(x,y,false))
{
//向下不通,向下走(往最长的方向走)
if(!computerDown(x,y,false))
{

direction(up,left,right,down,x,y)
}
}
}
}
}
else //向左
{
if(!computerLeft(x,y,false))
{
if(!computerUp(x,y,false))
{
if(!computerDown(x,y,false))
{
if(!computerRight(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
}
//右上角
else if(x<=mid && y>=mid)
{
if(x<=(mapSize-1-y)) //向上
{
if(!computerUp(x,y,false))
{
if(!computerRight(x,y,false))
{
if(!computerLeft(x,y,false))
{
if(!computerDown(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
else //向右
{
if(!computerRight(x,y,false))
{
if(!computerUp(x,y,false))
{
if(!computerDown(x,y,false))
{
if(!computerLeft(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
}
//右下角
else if(x>=mid && y>=mid)
{
if(x>=y) //向下
{
if(!computerDown(x,y,false))
{
if(!computerRight(x,y,false))
{
if(!computerLeft(x,y,false))
{
if(!computerUp(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
else //向右
{
if(!computerRight(x,y,false))
{
if(!computerDown(x,y,false))
{
if(!computerUp(x,y,false))
{
if(!computerLeft(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
}
//左下角
else if(x>=mid && y<=mid)
{
if((mapSize-1-x)<=y) //向下
{
if(!computerDown(x,y,false))
{
if(!computerLeft(x,y,false))
{
if(!computerRight(x,y,false))
{
if(!computerUp(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
else //向左
{
if(!computerLeft(x,y,false))
{
if(!computerDown(x,y,false))
{
if(!computerUp(x,y,false))
{
if(!computerRight(x,y,false))
{
direction(up,left,right,down,x,y)
}
}
}
}
}
}

}
...全文
21676 860 打赏 收藏 转发到动态 举报
写回复
用AI写文章
860 条回复
切换为时间正序
请发表友善的回复…
发表回复
灵魂火焰 2012-10-08
  • 打赏
  • 举报
回复
初始的块块作用很大啊~~哈哈~~18步~~
酱油打的飞起 2012-09-22
  • 打赏
  • 举报
回复
LZ厉害
nanst 2012-08-27
  • 打赏
  • 举报
回复
支持,谢谢楼主
cherish1forever 2012-08-15
  • 打赏
  • 举报
回复
101层
挺不错的。
Mokaffe 2012-06-19
  • 打赏
  • 举报
回复
对角线走,堵不住....
watermarkcamera 2012-04-27
  • 打赏
  • 举报
回复
很有意思
xibostar 2012-04-27
  • 打赏
  • 举报
回复
我现在才刚刚会建windows窗口~~
GHM201203 2012-04-02
  • 打赏
  • 举报
回复
楼主好强大。
nt_zxh 2012-03-29
  • 打赏
  • 举报
回复
6步没玩出过,7步玩出了好多次,有弱智的漏洞
nt_zxh 2012-03-29
  • 打赏
  • 举报
回复
玩了好几盘7步了
zbhlheroy 2012-02-14
  • 打赏
  • 举报
回复
不知道该说什么
zhy77117178 2012-01-13
  • 打赏
  • 举报
回复
学习各位高手思路!
mudonfield 2012-01-05
  • 打赏
  • 举报
回复
玩了一下,有点技巧,代码的智能程度还不够。接分
zorrolg 2011-12-26
  • 打赏
  • 举报
回复
34步才捉住
ntzhizhu 2011-12-23
  • 打赏
  • 举报
回复
呵呵,我也赢了一把
zangluyao 2011-12-23
  • 打赏
  • 举报
回复
强悍······支持····玩了几局·现几乎每局必赢了哈哈··
cfan365 2011-12-16
  • 打赏
  • 举报
回复
Mtoo 2011-12-11
  • 打赏
  • 举报
回复
没赢一局
yfl819586803 2011-10-26
  • 打赏
  • 举报
回复
lz强大
mpfishere 2011-10-26
  • 打赏
  • 举报
回复
LZ强人
要是可以规避一下让红色不能初始化到外围两层就好了
加载更多回复(829)

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧